0

I have a few months that i started programming in C, but I now find myself with a doubt, for example, let see the next example code:

    typedef struct
    {
        char *var1;
    }myFooStruct;

    myFooStruct struct1 [ 200 ];

my doubt is what would I get for **struct1, &struct1, *struct1, struct1, as I passed the struct to a function that takes a two-dimenssion pointer ( **myFooStruct ), I have basic knowledge about pointers 1-but I find myself confused with pointers to structs and 2-how can I modify the struct if I passed it as at parameter to a function

If there is another similar question post it here please, I could not find anything alike, if you know some lecture I could read is welcome too, thank you very much!!

2
  • You typically cannot legally dereference a struct. If you have myFooStruct struct0; then *struct0 is a syntax error. So therefore in your code **struct1 is also a syntax error Commented Sep 14, 2015 at 16:05
  • Thanks Chris!.... Ok guys, I had a hard time choosing the answer, I choose the one that I find more complete to question in my point of view, thank you all for your answers! Commented Sep 14, 2015 at 17:35

5 Answers 5

2

* is a dereference operator - think of it as meaning "the value contained at location xyz".

& is a reference operator - think of it as meaning "the location in memory of variable xyz".

Accordingly:

myFooStruct struct1 is a physical structure - this is the actual object.

&struct1 is equivalent to the location in memory of struct1 - this is usually an address (like 0xf0004782). You'll usually see this used when passing by reference (see Wikipedia for more info) or when assigning to a pointer (which literally points to a location in memory - get it?).

*struct1 dereferences struct1 - that is, it returns the value contained at location struct1. In the example you give, this is invalid, as struct1 is not a pointer to a location in memory.

**struct1 is tricky - it returns the value contained at the location that is contained within struct1. In other words: struct1 points to a certain location in memory. At that location is the address of another location in memory! Think of it as a scavenger hunt - you go to a location, find a clue, and follow that to another location.


As to how to access structs: think of a struct as a box. When you have the box in front of you, you simply need to open it up and look at what's inside. In C, we do this using the . operator:

char *my_var = struct1.var1

When you don't have the box in front of you - that is, you have a pointer to the struct - you need to access the location the box is at before you can look at what's inside. In C, we have a shortcut for this - the -> operator:

myFooStruct *pointer_to_struct1 = &struct1
char *my_var = pointer_to_struct1->var1
//NOTE: the previous line is equivalent to:
//  char *my_var = (*pointer_to_struct1).var1
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you very much! complete and clean answer!
2

Way 1 Using dynamic memory allocation. Generally used in linked list and all..

If you want to modify the struct in another function. first declare a pointer to a struct.

myFooStruct* struct1;

Allot memory for the struct

struct1 = malloc(sizeof(myFooStruct));

Send the address to the function

func1(struct1);

Receive it and access it to modify in the function.

void func(myFooStruct* struct1)
{
    (*struct1).member1 = ...; // whatever you wanna do
    ...

Way 2

Declare a struct.

myFooStruct struct1;

Send the address of the struct to the function

func1(&struct1);

Receive it and access it to modify in the function.

void func(myFooStruct* struct1)
{
    (*struct1).member1 = ...; // whatever you wanna do
    ...

4 Comments

Why do you want to dynamically allocate memory for struct? You do not have to do this to pass struct to another function
No that's fine. Just wanted to know if there is any reason to this :)
Thanks very much!, I didn't choose this answer only because you did not touch the **operand, but is very clear, thank you very much!
@Javi9207 No problem brother, its not about the points. Its about what you learn and how our effort helps you. :)
1

If you need to access myFooStruct from function, you can define single pointer: fn( myFooStruct * st ). The you call the function with fn( struct1 ) and change values st[N].var1 = .... Double pointer may be necessary if your object is pointer with allocated memory, not static array as yours.

Comments

1

struct1 is just a table and to be speciffic it's just pointer to a place in the memory.

*struct1 would be thing, that is pointed by struct1, so it's a first struct in a table of structs.

But **struct1 won't be any string. First of all you do not allocate memory for string and second string is member of this struct not struct itself. **struct is undefined behavior, nothing more.

&struct is a pointer to the table, so it's a pointer to the pointer, that points first struct in a table.

You have to decide on your own, what you want. If you want to pass table of your structs then the cleanest way would be:

void function(myFooStruct structTab[]);

Comments

1

1. You should pass a struct pointer to function to access struct inside it .

Declare a struct pointer -

myFooStruct *struct1;

Allocate memory for struct

And pass it to function which is declared as -

 type fn(myFooStruct *struct1){
     .....
     }

Call this function like this -

fn(struct1);

Access struct member like this -struct->member1

2. You can also pass what you have declared right now.

myFooStruct struct1[ 200 ];

define function as -

type fn(myFooStruct struct1[]){
.....
}

Access struct members like this - struct[i].member1.

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.