SiteBanner

Go slow on the topic of pointers.  'C' gives great flexibility in accessing our data (and WHERE and HOW that data is accessed).  With such power comes the opportunity to have unexpected program behavior, or quite cryptic bugs.  It behooves us to be extra careful.  In this installment, '&' and '*' are presented.

 

 

(right-click to download)

/* pgm8 source */

#include <stdio.h>

int main(void) {
char char_var;     // data will be held in this variable
char *char_pntr;  // AN ADDRESS will be held in this (pointer) variable

printf("\n");

char_var = 'Q';

printf("'char_var' contains: %c\n", char_var);
printf("The address of variable 'char_var': %p\n\n", &char_var);

char_pntr = &char_var;   // initialize char_pntr to HOLD THE memory ADDRESS of char_var

printf("'*char_pntr' contains: %c\n", *char_pntr);
printf("The address of variable 'char_pntr': %p\n\n", &char_pntr);

printf("\n");
return(0);
}

 

 So, the variable 'int days;' stores an integer.   The variable 'int *pnt_days;' stores the ADDRESS of an integer variable.  "pnt_days = &days;" assignes the ADDRESS of 'days' to the pointer 'pnt_days'.  You may notice that each time you run pgm8, the memory addresses change.  That's because the operating system - at any given moment - will assign program space & memory "on the fly".