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!!
答案 0 :(得分: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
...
答案 1 :(得分: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
答案 2 :(得分: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.
答案 3 :(得分: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[]);
答案 4 :(得分: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
.