Skip to content
thesarfo

Note

C: Pointer Types

Declaring pointer variables in C, and the type rules for assigning into them.

views 0
#include <stdio.h>
/* How do you make use of pointers. Firstly you store a pointer off in a variable so you can use it later. You can identify the "pointer-type" because there's an asterisk(*) before the variable name and after its type*/
int main(void){
int i; /* type int*/
int *p; /* type "pointer to an int" or "int-pointer" */
return 0;
}
/* When you do an assignment into a pointer variable(*p), the type of the right hand side of the assignment has to be the same type as the pointer variable. ie, you can only assign a fellow int to an int-pointer. Fortunately, when you take the address-of a variable, the resultant type is a pointer to that variable type, so assignments like the one below are perfect */
int another(void){
int i;
int *p; /* p is a pointer, but is uninitialized and points to garbage */
p = &i; /* p now points to i */
return 0;
}