Pointers in C – Easy way to remember

//          pointer_example.c
#include <stdio.h>
int main(void)
int k;                 // variable type declaration
k = 2;                // assignment statement
//          2 is rvalue (value in the right hand side of "=")
//          k is lvalue (value in the left hand side of "=")
int *ptr;              // pointer to store integer values
ptr = &k;           // address of k
//          *ptr is rvalue
//          ptr is lvalue
printf("value stored in k is %d\n", *ptr);
//          k is name of variable;  k is an unmodifiable lvalue
//          ptr is name of address; ptr is a modifiable lvalue
//          value stored in k is 2

/* The size of memory required to store an address depends on the system. Some computers might require special handling to hold a segment and offset under certain circumstances. The actual size required is not too important so long as we have a way of informing the compiler that what we want to store is an address. Such a variable is called a pointer variable.

1) To store a variable, we need to know how many bits/bytes would be required to hold the value.

2) To store address of a variable, we need not know the number of bits/bytes would be required to hold the address. It is machine dependent.

3) Type declaration for a pointer indicates what type of variable will be stored in that address.


/* Pointers vs Arrays

// In C a string is an array of characters terminated with a binary zero character

(written as slash0.JPG).

char my_string[40];

my_string[0] = ‘T’;

my_string[1] = ‘e’;

my_string[2] = ‘d’:

my_string[3] = ‘slash0.JPG‘;

// C permits

my_string[40] = “Ted”;

/* In the above example, my_string has 37 null characters in it, since the size of the array is 40 which is a constant. Here the lvalue is unmodifiable.

This can be avoided if a pointer is used instead. Here the lvalue is modifiable.


//	Pointers to structures
typedef struct student
 int age;
 int studentNumber;
 float averageGrade;
 char name[32];
int main()
 //a STUDENT variable
 STUDENT mary;
 //a pointer to a STUDENT variable
 STUDENT *pupil;
//assign some values
 mary.age = 14;
 mary.studentNumber = 5678;
 mary.averageGrade = 73;
//tell pupil to point at mary
 pupil = &mary;
//Notice in the examples below the use of -> instead of (*pupil).age
//we can now access members of the mary structure using the pupil pointer
 printf("Mary's student number is %d\n", pupil->studentNumber);
//we can also do assignment and all other normal variable operations
 pupil->averageGrade = 86.2;
printf("Mary's average grade is %f\n", mary.averageGrade);
return 0;

Download here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s