If T is some type specifier then a pointer to an object of the type T can be declared in any of the following ways
T*p;
T* p;
T *p;
T * p;
For example if T is int * then the pointer declaration can look like
int **p;
int ** p;
int * *p;
int * * p;
The same way you can declare a pointer to a structure
struct Card*p = &my_card;
struct Card* p = &my_card;
struct Card *p = &my_card;
struct Card * p = &my_card;
Pay attention to that you may write
T ( *p );
but you may not write
( T* ) p;
Also there exists another subtlety. If you will write for example
int* p1, p2
then the variable p1 has the type int * while the variable p2 has the type int instead of int *.
But if you will write
typedef int * T;
when in this declaration
T p1, p2;
the both variables have the type int *.
How can I declare a Pointer to a struct in C? - Stack Overflow
What is the difference between structs and pointer to structs?
why use pointer to struct and not use the struct directly (C) - Stack Overflow
Pointer to struct VS. nested struct
Videos
I am a newbie to C. I recently learned about pointers and am comfortable with int pointers. However, I am having a hard time with structures.For instance, if I give a pointer to a structure how does the pointer point to all the data stored in the struct? How would that even be possible?Let me give an example -
int digit = 5 int *ptr = &digit; // Let's assume &digit is 1001 // Therefore ptr is now 1001
However, a structure is a contigous block of memory. What would a pointer to it store?
If T is some type specifier then a pointer to an object of the type T can be declared in any of the following ways
T*p;
T* p;
T *p;
T * p;
For example if T is int * then the pointer declaration can look like
int **p;
int ** p;
int * *p;
int * * p;
The same way you can declare a pointer to a structure
struct Card*p = &my_card;
struct Card* p = &my_card;
struct Card *p = &my_card;
struct Card * p = &my_card;
Pay attention to that you may write
T ( *p );
but you may not write
( T* ) p;
Also there exists another subtlety. If you will write for example
int* p1, p2
then the variable p1 has the type int * while the variable p2 has the type int instead of int *.
But if you will write
typedef int * T;
when in this declaration
T p1, p2;
the both variables have the type int *.
It doesn't matter where you put the spaces. struct Card* p and struct Card *p are equally valid; it's just a matter of style as to which one you prefer.
I agree the second form is more common, but what's most important is that you use one form consistently throughout your code. Your boss / teacher / other developers may also have coding style standards that specify which form to use.
frighten grey forgetful stupendous light heavy paltry flowery groovy roll
This post was mass deleted and anonymized with Redact
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!
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.