The pointer operators

& (reference) and * (dereference)

Pointers are one of the more complicated subjects for beginners in learning C, and it is possible to write the vast majority of Energia sketches without ever encountering pointers. However for manipulating certain data structures, the use of pointers can simplify the code, and and knowledge of manipulating pointers is handy to have in one’s toolkit.

You can pass a variable by reference or by value. Passing by value is the basic form of variable passing in C/C++ and it means that when you assign one variable to another, you’re actually creating a copy of that variable.

int a = 1;
int b = a;
b += 1;
//now a == 1 and b == 2

Passing by reference is when you store a reference to one variable in another such that modifying the second variable modifies the value of the first. The reference is the physical memory location of the variable.

int a = 1;
int *b = &a;
*b += 1;
//now a == 2 (and b's value is a pointer to the location of a in memory)

You will need to use the & reference and * dereference operators to get the proper output you are looking for from the variable (memory location or stored value).

 

char ch = 'c'; 
char *chptr = &ch; 

int i = 20; 
int *intptr = &i; 

float f = 1.20000; 
float *fptr = &f; 

char *ptr = "I am a string"; 

printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr);

return 0;

Reference Home