0

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
}
2
  • 1
    you need Need of Pointer to pointer to reflect change in function Commented Oct 3, 2013 at 18:55
  • The name vars in your alg function is not defined. If you want to use the first function argument use variable v. The struct vars is just name of the struct type and to use it by calling vars instead of struct vars write: typedef struct vars vars; - but still it doesn't let you use &vars[0] in alg() function since you don't have there argument called vars.. Commented Oct 3, 2013 at 18:58

3 Answers 3

2

You need "Need of Pointer to pointer" to reflect change in function alg() in main function.

Do it as:

void alg(struct vars v[], struct vars **ptr)
{
   (*ptr) = &vars[0];
   printf("%s", (*ptr)->value); //this works here
}

And call function main as: alg(vArray, &ptr);

Note: both ptr in main and ptr in function alg() are two different variable.

Btw for learning purpose it is good to write function otherwise you can simply do it in one line as: struct vars *ptr = vArray;. Note array name decays into address of first element in this expression.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to read about pointer to pointer.

Check this code:

#include <stdio.h>
#include <stdlib.h>

struct vars {
    int data;
};

void alg(struct vars *v, struct vars **ptr);

int main()
{
   struct vars *ptr;
   struct vars vArray;
   vArray.data = 10;

   alg(&vArray, &ptr);
   printf("main : %d\n", ptr->data); 
}

void alg(struct vars *v, struct vars **ptr)
{
   *ptr = v;
   printf("fun : %d\n", (*ptr)->data); 
}

output :

fun : 10
main : 10

5 Comments

because double is data type in C so don't say it double pointer instead call pointer to pointer
btw does your code run correct? because your code is wrong! Guess why?
void alg(struct vars v[], struct vars **ptr). The signature of your function takes a single instance of the struct, rather than an array.
Sujiin Your code calling undefined behavior (will mislead to OP) as you have assigned local structure variable address to ptr pointer in function that you access in main.
@JeremyWest No that is due to his example code what Sujiin wants to show is bit different, but mistake is he is access address of local variable in main(). Life/scopre of v (alg(struct vars v, ) is within function only.
0

In short use double pointer it will solve your problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.