C - Pointers
C - Pointers
IN
C Programming
What are Pointers?
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the
name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk
used for multiplication. However, in this statement the asterisk is being used to designate
a variable as a pointer. Take a look at some of the valid pointer declarations −
The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory address.
The only difference between pointers of different data types is the data type of the
variable or constant that the pointer points to.
How to Use Pointers?
There are a few important operations, which we will do with the help of pointers very
frequently.
(a) We define a pointer variable,
(b) assign the address of a variable to a pointer
(c) finally access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the variable located at
the address specified by its operand.
Declaring a pointer
The pointer in c language can be declared using *
(asterisk symbol). It is also known as indirection pointer
used to dereference a pointer.
int *a; //pointer to int
char *c; //pointer to char
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var ); /* address stored in
pointer variable */
printf("Address stored in ip variable: %x\n", ip ); /* access the value
using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
Output
#include<stdio.h>
#define size 5 printf("Address of a = %p\n",arr[0]);
printf("Address of b = %p\n",arr[1]);
int main() printf("Address of c = %p\n",arr[2]);
{ printf("Address of d = %p\n",arr[3]);
int *arr[size]; printf("Address of e = %p\n",arr[4]);
int a = 10, b = 20, c = 30, d = 40, e = 50, i;
for(i = 0; i < size; i++)
arr[0] = &a; printf("value stored at arr[%d] = %d\
arr[1] = &b; n",i,*arr[i]);
arr[2] = &c;
arr[3] = &d; return 0;
arr[4] = &e; }
Output
Address of a = 0x7fff959067b8
Address of b = 0x7fff959067bc
Address of c = 0x7fff959067c0
Address of d = 0x7fff959067c4
Address of e = 0x7fff959067c8
value stored at arr[0] = 10
value stored at arr[1] = 20
value stored at arr[2] = 30
value stored at arr[3] = 40
value stored at arr[4] = 50
Pointer Array
A pointer is a place in memory that keeps address of An array is a single, pre allocated chunk of contiguous
another place inside elements (all of the same type), fixed
in size and location.
In the following code we are assigning the address of the string str to the pointer ptr.
char *ptr = str;
We can represent the character pointer variable ptr as follows.
The pointer variable ptr is allocated memory address 8000 and it holds the address of
the string variable str i.e., 1000.
Accessing string via pointer
To access and print the elements of the string we can use a loop and check for the \0 null
character.
In the following example we are using while loop to print the characters of the string
variable str.
#include <stdio.h> // print the string
while(*ptr != '\0') {
int main(void) { printf("%c", *ptr);
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.
An Array of Pointers in C
What is an Array?
If we want to include multiple elements in a program with the very same data type, we can use an
array. Let us assume that we are using a total of five integers in any program. There are two different
ways in which we can allocate memory to all of these integers:
• We can create five integers differently;
• We can create an array of all the different integers.
One of the huge advantages of using arrays is that it becomes very easy for a programmer to access all
the elements in the program easily. All the elements can be accessed in a single run of a loop in the
program.
What is a Pointer?
The pointers in C point towards the variables present in a program. They hold the addresses
of the variables. The program will use these addresses to access the variables and modify
them. Here are the three crucial things that come into play when dealing with pointers in C: