Pointeri
Pointeri
Pointeri
A pointer is a variable that stores the address of another variable. Unlike other variables that hold
values of a certain type, pointer holds the address of a variable. For example, an integer variable
holds (or you can say stores) an integer value, however an integer pointer holds the address of a
integer variable. In this guide, we will discuss pointers in C programming with the help of
examples.
Before we discuss about pointers in C, lets take a simple example to understand what do we
mean by the address of a variable.
So let’s say the address assigned to variable num is 0x7fff5694dc58, which means whatever value we
would be assigning to num should be stored at the location: 0x7fff5694dc58. See the diagram below.
#include <stdio.h>
int main()
{
int num = 10;
printf("Value of variable num is: %d", num);
/* To print the address of a variable we use %p
* format specifier and ampersand (&) sign just
* before the variable name like &num.
*/
printf("\nAddress of variable num is: %p", &num);
return 0;
}
Output:
Important point to note is: The data type of pointer and the variable must match, an int pointer
can hold the address of int variable, similarly a pointer declared with float data type can hold the
address of a float variable. In the example below, the pointer and the variable both are of int type.
#include <stdio.h>
int main()
{
//Variable declaration
int num = 10;
//Pointer declaration
int *p;
Pointer is just like another variable, the main difference is that it stores address of another
variable rather than a value.
double a = 10;
double *p;
p = &a;
*p would give us the value of the variable a. The following statement would display 10 as output.
printf("%d", *p);
Similarly if we assign a value to *pointer like this:
*p = 200;
It would change the value of variable a. The statement above will change the value of a from 10 to
200.
char ch='a';
char *ptr;
Read the value of ch
ch = 'b';
or
*ptr = 'b';
The above code would replace the value ‘a’ with ‘b’.
#include <stdio.h>
int main()
{
int var =10;
int *p;
p= &var;
3) Function pointers – A function pointer is just like another pointer, it is used for storing the
address of a function. Function pointer can also be used for calling a function in C program.
/*
storing address of the first element
of the array in pointer variable
*/
ptr = var;
Output:
/*
storing address of the last element
of the array in pointer variable
*/
ptr = &var[MAX-1];
Output:
C - Pointers and Strings
Creating a string
In the following example we are creating a string str using char character array of
size 6.
#include <stdio.h>
int main(void) {
// string variable
// pointer variable
while(*ptr != '\0') {
printf("%c", *ptr);
ptr++;
return 0;
}
#include <stdio.h>
int main(void) {
char *t = strPtr;
while(*t != '\0') {
printf("%c", *t);
t++;
return 0;
}
Note! In the above code we are using another character pointer t to print the
characters of the string as because we don't want to lose the starting address of the
string "Hello" which is saved in pointer variable strPtr.
In the above image the string "Hello" is saved in the memory location 5000 to 5005.
The pointer variable strPtr is at memory location 8000 and is pointing at the string
address 5000.
The temporary variable is also assigned the address of the string so, it too holds the
value 5000 and points at the starting memory location of the string "Hello".
Array of strings
We can create a two dimensional array and save multiple strings in it.
For example, in the given code we are storing 4 cities name in a string array city.
char city[4][12] = {
"Chennai",
"Kolkata",
"Mumbai",
"New Delhi"
};
The problem with this approach is that we are allocating 4x12 = 48 bytes memory to
the city array and we are only using 33 bytes.
We can save those unused memory spaces by using pointers as shown below.
char *cityPtr[4] = {
"Chennai",
"Kolkata",
"Mumbai",
"New Delhi"
};
In the above code we are creating an array of character pointer cityPtr of size 4 to
store the name of the four cities.
We can represent the array of pointers as follows.
The cityPtr pointer variable is allocated the memory address 8000 to 8007.
Assuming integer address value takes 2 bytes space. So, each pointer gets 2 bytes.
Name of the cities are saved in locations 1000, 2000, 3000 and 4000.
#include <stdio.h>
int main(void) {
// array of pointers
char *cityPtr[4] = {
"Chennai",
"Kolkata",
"Mumbai",
"New Delhi"
};
// temporary variable
int r, c;
// print cities
c = 0;
while(*(cityPtr[r] + c) != '\0') {
c++;
printf("\n");
return 0;
Output:
Chennai
Kolkata
Mumbai
New Delhi
In the above code we are using the r variable to access each row of the pointer. And
we are using the c variable to access each character in a selected row.