Functions, structs and pointers: Difference between revisions

From Applied Science
(Created page with "Pointers to struct provide a convenient way to access struct's members. The concept of pointers to struct is the same as pointer to any other variable: we first have to declare the variable and the pointer to it, then make the pointer point to that variable's memory address. The advantage of using pointers to structures is that in case we have a function that does something which requires data from a struct we don't have to pass all the struct's values to the function, j...")
 
No edit summary
Line 5: Line 5:
* If the pointer to struct is not pointing to a struct, trying to use it to access a struct's member will cause the program to crash;
* If the pointer to struct is not pointing to a struct, trying to use it to access a struct's member will cause the program to crash;
* Pointers to struct and structs must match each other's type to avoid errors.
* Pointers to struct and structs must match each other's type to avoid errors.
<div style="text-align:center;">
'''To understand the example below it's required to have a good understanding about pointers, functions and structs'''
</div>
* '''A function that swaps all values of all members of two structs of the same type:'''
<div class="code">
typedef struct Product Pro;<br />
struct Product {
char Type[1];
int  Year;
}
<span class="codecomment">/* Receives two addresses of two structs, A and B, and swaps the values of all members of both */</span>
void Swap (Pro *A, Pro *B) {<div style="margin-left:1.5em;">
Pro Temp, *C;<br />
C = &Temp;<br />
&#42;C = *A;<br />
&#42;A = *B;<br />
&#42;B = *C;
</div>
}
<span class="codecomment">/* Calling the function */</span><br />
Swap(&X, &Y);
</div>
<div style="margin-left:1.5em;">
It's the same example about swapping the values of two variables, this time done with structs and pointers to struct. Note that both structs must be of the same type, else program won't compile.
'''Note:''' in the above example, '''X''' and '''Y''' are of the same ''"Pro"'' type. When we call a function with memory addresses as arguments, the address itself doesn't hold information about the variable's type, so we must be careful to not mix up pointers to a struct of one type and memory addresses of structs of a different type.
</div>

Revision as of 01:47, 20 April 2022

Pointers to struct provide a convenient way to access struct's members. The concept of pointers to struct is the same as pointer to any other variable: we first have to declare the variable and the pointer to it, then make the pointer point to that variable's memory address. The advantage of using pointers to structures is that in case we have a function that does something which requires data from a struct we don't have to pass all the struct's values to the function, just a reference, a pointer which can directly access the struct's members.

Errors of logic:

  • If the pointer to struct is not pointing to a struct, trying to use it to access a struct's member will cause the program to crash;
  • Pointers to struct and structs must match each other's type to avoid errors.


To understand the example below it's required to have a good understanding about pointers, functions and structs


  • A function that swaps all values of all members of two structs of the same type:

typedef struct Product Pro;
struct Product { char Type[1]; int Year; }

/* Receives two addresses of two structs, A and B, and swaps the values of all members of both */

void Swap (Pro *A, Pro *B) {

Pro Temp, *C;
C = &Temp;
*C = *A;
*A = *B;
*B = *C;

}

/* Calling the function */
Swap(&X, &Y);

It's the same example about swapping the values of two variables, this time done with structs and pointers to struct. Note that both structs must be of the same type, else program won't compile.

Note: in the above example, X and Y are of the same "Pro" type. When we call a function with memory addresses as arguments, the address itself doesn't hold information about the variable's type, so we must be careful to not mix up pointers to a struct of one type and memory addresses of structs of a different type.