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
}
user1754267
  • 59
  • 1
  • 3
  • 1
    you need [Need of Pointer to pointer](http://stackoverflow.com/questions/18306935/need-of-pointer-to-pointer/18307020#18307020) to reflect change in function – Grijesh Chauhan Oct 03 '13 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`.. – Disconnect3d Oct 03 '13 at 18:58

3 Answers3

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.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
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
sujin
  • 2,813
  • 2
  • 21
  • 33
  • because double is data type in C so don't say it `double pointer` instead call `pointer to pointer` – Grijesh Chauhan Oct 03 '13 at 19:13
  • btw does your code run correct? because **your code is wrong**! Guess why? – Grijesh Chauhan Oct 03 '13 at 19:14
  • void alg(struct vars v[], struct vars **ptr). The signature of your function takes a single instance of the struct, rather than an array. – Jeremy West Oct 03 '13 at 19:22
  • 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. – Grijesh Chauhan Oct 03 '13 at 19:22
  • @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. – Grijesh Chauhan Oct 03 '13 at 19:25
0

In short use double pointer it will solve your problem

fahim
  • 23
  • 4