This it's my first post, but I have few hours with a problem.
Basically I need pass a array to a function, but the array it's a variable of the one struct. Moreover, this array it's declare like a pointer.
Help me please. My code it's
------------------------------------------------------------------------------
------------------------------------------------------------------------------
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;
------------------------------------------------------------------------------
struct array{
int miembro1;
int miembro2;
};
------------------------------------------------------------------------------
void funcionPrueba(int *vector);
int i=0, n=20;
for(int i=0; i<n; i++)
cout<<"vector("<<i<<") "<<vector[i].miembro1<<"\n";
}
------------------------------------------------------------------------------
------------------------------------------------------------------------------
Sorry because not have color.
The error it's
------------------------------------------------------------------------------
experiment.cpp: In function ‘int main()’:
experiment.cpp:22:22: error: cannot convert ‘array’ to ‘int*’ for argument ‘1’ to ‘void funcionPrueba(int*)’
experiment.cpp: In function ‘void funcionPrueba(int*)’:
experiment.cpp:32:37: error: request for member ‘miembro1’ in ‘*(vector + ((long unsigned int)(((long unsigned int)i) * 4ul)))’, which is of non-class type ‘int’
------------------------------------------------------------------------------
Second: You are trying to pass to funcionPrueba() a struct but the function expects a pointer to an int. So decide: What does funcionPrueba() need: A pointer to an int or a struct? And then correct the code accordingly.
Thank webJose, the colors it's when I copy and paste from vi.
funcionPrueba() need some like array[i].miembro1. A array that it's a variable of a struct. The array I can declare like pointer or array but in neither case does me.
An int doesn't have a miembro1 member. So you must declare the function as void funcionPrueba(array *vector). Why? Because the data type "array" does have what you are looking for.