Write a C program to create, initialize and demonstrate the use of pointers. How to access values and addresses using a pointer variable in C programming.
Required knowledge
Basic C programming, Pointers
Basic of pointers
Being a high level programming language, C is extremely powerful at low level programming.
Pointer is one of its tool that provide such low level memory handling. Data in memory is organized as a sequence of bytes. Where each byte is accessed through its unique address. These addresses ranges from zero (0) to some positive integer. Pointers in C programming provides an efficient way to handle the low level memory activities.
Like other variables, pointers also allows to declare variables of pointer types. Now, what does that mean? Like other variables we can declare a pointer variable. But what type of data does pointer variable contains? Unlike other variables pointer holds memory address of some other variable. We will later see the use of these memory addresses and how pointers are a magic wand to the C programmers. Let us now get familiar with some pointer concepts.
Reading memory address of any variable
We know that each and every declared variable has a name, memory location and value. Name is the identifier name which we give during declaration of the variable. Values are the constants that the variable contains.
For example – int num = 10; num is the variable name and 10 is the value stored in that variable. But what about the memory address?
In C programming the unary & (Address of) operator is used to get memory address of any variable. Address of operator when prefixed with any variable returns the actual memory address of that variable. Let us see a program to get actual memory address of variables.
Program to get memory address using address of operator
/**
* C program to get memory address using address of operator
*/
#include <stdio.h>
int main()
{
/* Simple declarations */
char character = 'C';
int integer = 1;
float real = 10.4f;
long long biginteger = 989898989ll;
/* Print variable value with their memory address */
printf("Value of character = %c, Address of character = %u\n", character, &character);
printf("Value of integer = %d, Address of integer = %u\n", integer, &integer);
printf("Value of real = %f, Address of real = %u\n", real, &real);
printf("Value of biginteger = %lld, Address of biginteger = %u", biginteger, &biginteger);
return 0;
}
Note: The above program will produce different result on different systems. Also you can use any other format specifier other than %u to print memory addresses. You can use any format specifier that prints an integer. Try using %x %d %i %ld etc.
Output
Value of character = C, Address of character = 6356751 Value of integer = 1, Address of integer = 6356744 Value of real = 10.400000, Address of real = 6356740 Value of biginteger = 989898989, Address of biginteger = 6356728
Creating, initializing and using pointer variables
Pointers can handle many low level memory operations (including dynamic memory allocation). However, before we get in deep of pointers let us first learn to declare a pointer variable. Like other variable declarations pointer also follow the same syntax –
Syntax to declare pointer variable
<data-type> * <variable-name>
Example of pointer declaration
int * integer_pointer;
float * float_ptr
char * charPtr;
long * lptr;
Program to create, initialize and use pointer variable
/**
* C program to create, initialize and use pointers
*/
#include <stdio.h>
int main()
{
int num = 10;
int * ptr;
/* Stores the address of num to pointer type */
ptr = #
printf("Address of num = %d\n", &num);
printf("Value of num = %d\n", num);
printf("Address of ptr = %d\n", &ptr);
printf("Value of ptr = %d\n", ptr);
printf("Value pointed by ptr = %d\n", *ptr);
return 0;
}
Output
Address of num = 6356748. Value of num = 10 Address of ptr = 6356744 Value of ptr = 6356748 Value pointed by ptr = 10
Happy coding 😉