简体   繁体   中英

why use pointer to struct and not use the struct directly (C)

I have two examples shown in the code below, which gives the same result. I see people do example 1 using pointer. But why not do example 2 and using the ax directly? is there any difference?

typedef struct
{
    uint8_t x;
    uint8_t y;
    uint8_t z;
} xyz_t;

xyz_t a;

void func1(xyz_t* b)
{
    printf("%d", b->x); //example 1
}

void func2()
{
    printf("%d", a.x); //example 2
}

int main()
{
    a.x = 10;
    func1(&a); //example 1
    func2();   //example 2
}

if your function was given as argument the struct without a pointer, its data would have to be copied to be used by the function which might take some time. Furthermore, if you want to modify the struct in the function and see its changes outside of the function's scope, your only option is to send it by pointer because you are giving the function the location in memory of the variable you want to modify and not a (local) copy. It is often preferred to send variables by value if they aren't big and you aren't modifying them because it makes the function's actions on its arguments clearer to the caller. If your function can only do an operation on a single variable, its use will be very limited. You could for instance use this same function with another xyz_t that the function can't directly access.

To answer your question rather directly, it's as if I asked you "why would I pass a variable as an argument to my function instead of just keeping it as a global variable?", but that's probably not the answer you're looking for, so here's some more insight on why pointers could be useful in this case:

Pointers are helpful because you can "move them around" more easily. Instead of having to copy over the whole stucture each time, you can just leave it where it is in memory and instead pass a pointer to it around.

Also, passing a pointer means you can modify "a" without having to copy it back, since the stucture resides in only one place in memory.

There are probably many more reasons that I've glossed over but these I think are the most important ones and the most relevant to your original question.

Have a nice day!

Since a is global in this case there is no relevant difference, in the first function you are passing a pointer to a , any chages made to a in the scope of the function will be reflected in a outside the scope of the function, even if it's not global.

The second function is acting direclty in the global variable so, the changes are obviously also reflected in a .

A relevant difference would be noted if you passed a by value, ie:

xyz_t a;

void func3(xyz_t b)
{
    printf("%d", b.x); //example 3 
}

//...
func3(a);
//...

Here you would be passing a by value, essentially a copy of a , any change made to a in the scope of the function would not really be to a but to a copy of it, and naturally it would not reflect in the original a .

Noting that you are not changing anything, only printing the value stored in a any of the three render the same results.

In this case, because you are not trying to change the variable it's a good practice to use const keyword to avoid any accidental change.

void func1(const xyz_t* b)

Passing by value also guards against accidental changes but it might make the program run slower if you are dealing with large data structures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM