I'm trying to set ptr to point at the first element in an array of structs so that when I go back to my main function, I can mess with it (theres reasons why I can't use vArray[0] in the main).
With this code though, its only allowing me to access the structs members in the alg function. Once its return back to main, all of its elements are now null. (I'm thinking it has something to do with a pass-by-value/pass-by-reference problem). Any way to fix this?
void alg(struct vars v[], struct vars *ptr)
{
ptr = &vars[0];
printf("%s", ptr->value); //this works here
}
int main()
{
struct vars vArray[100]; //this has been filled earlier in the code
struct vars *ptr;
alg(vArray, ptr);
printf("%s", ptr->value); //but now this returns null here
}
varsin youralgfunction is not defined. If you want to use the first function argument use variablev. Thestruct varsis just name of the struct type and to use it by callingvarsinstead ofstruct varswrite:typedef struct vars vars;- but still it doesn't let you use&vars[0]inalg()function since you don't have there argument calledvars..