PDA

View Full Version : [SOLVED] Pointers to Struct



lewisforlife
January 14th, 2010, 02:52 PM
Here is my code:


#include <stdio.h>

typedef struct
{
int a;
} MYSTRUCTURE;

void indirect (MYSTRUCTURE *ns)
{
MYSTRUCTURE ms = *ns;

ms.a = 10;
}

int main (int argc, char *argv [])
{
MYSTRUCTURE ms;

ms.a = 5;
printf ("%d\n", ms.a);

indirect (&ms);

printf ("%d\n", ms.a);

return 0;
}

I would expect this to output:

5
10

But it instead outputs

5
5

How can I make this work how I want it. My intent was to pass the structure defined in main () to indirect () and be able to change the values of the variables indirectly.

Zugzwang
January 14th, 2010, 02:55 PM
#include <stdio.h>

typedef struct
{
int a;
} MYSTRUCTURE;

void indirect (MYSTRUCTURE *ns)
{
ns->a = 10;
}

int main (int argc, char *argv [])
{
MYSTRUCTURE ms;

ms.a = 5;
printf ("%d\n", ms.a);

indirect (&ms);

printf ("%d\n", ms.a);

return 0;
}

The line "MYSTRUCTURE ms = *ns;" creates a copy of "*ns" and thus "ms.a = ..." only changes the value in this copy.

lewisforlife
January 14th, 2010, 03:25 PM
The line "MYSTRUCTURE ms = *ns;" creates a copy of "*ns" and thus "ms.a = ..." only changes the value in this copy.

I see what you are saying. I have tried:


*ns->a = 10;

and I get the following when compiling:



main.c: In function 'indirect':
main.c:10 error: invlaid type argument of 'unary *' (have 'int')


How can I make this work? I want to access all of the variables in "ms" which is defined in main (). I want to change the variables by pointers in other functions. I don't want "ms" to be defined globally outside of a function. Any help is much appreciated :D

dwhitney67
January 14th, 2010, 03:31 PM
Try


ns->a = 10;

/* OR */

(*ns).a = 10;

lewisforlife
January 14th, 2010, 03:35 PM
Try


ns->a = 10;

/* OR */

(*ns).a = 10;


LOL, things seem so simple in hindsight. I thought I tried everything. I guess I tried everything but your solution. This worked great. Thanks! :D

dwhitney67
January 14th, 2010, 03:39 PM
LOL, things seem so simple in hindsight. I thought I tried everything. I guess I tried everything but your solution. This worked great. Thanks! :D

You should have paid more attention to Zugzwang's post, which came before mine.

Anyhow, you are welcome.

lewisforlife
January 14th, 2010, 03:46 PM
#include <stdio.h>

typedef struct
{
int a;
} MYSTRUCTURE;

void indirect (MYSTRUCTURE *ns)
{
ns->a = 10;
}

int main (int argc, char *argv [])
{
MYSTRUCTURE ms;

ms.a = 5;
printf ("%d\n", ms.a);

indirect (&ms);

printf ("%d\n", ms.a);

return 0;
}

The line "MYSTRUCTURE ms = *ns;" creates a copy of "*ns" and thus "ms.a = ..." only changes the value in this copy.

Thanks zugswang, I missed your solution somehow. I really appreciate the help. Thread Solved!