0% found this document useful (0 votes)
39 views6 pages

Concept of Pointer

Pointers are variables that store memory addresses of other variables. They can be used to indirectly access or modify the value of the variable located at the stored address. There are several pointer concepts in C including pointer declaration syntax, dereferencing with * and & operators, pointer arithmetic, passing pointers to functions, and constant pointers vs pointers to constants. Pointers allow for efficient manipulation of arrays and structures by accessing elements indirectly through memory addresses.

Uploaded by

sathiyavijayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views6 pages

Concept of Pointer

Pointers are variables that store memory addresses of other variables. They can be used to indirectly access or modify the value of the variable located at the stored address. There are several pointer concepts in C including pointer declaration syntax, dereferencing with * and & operators, pointer arithmetic, passing pointers to functions, and constant pointers vs pointers to constants. Pointers allow for efficient manipulation of arrays and structures by accessing elements indirectly through memory addresses.

Uploaded by

sathiyavijayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Pointers are variables that hold address of another variable of same data type.

Concept of Pointer

#include <stdio.h>
int main()
{
int var = 5;
printf("Value: %d\n", var);
printf("Address: %u", &var); //Notice, the ampersand(&) before var.
return 0;
}
Output
Value: 5
Address: 2686778
Pointer variables
stores just the address of another variable. It is called Pointer variable or, simply, a pointer.
Declaration of Pointer
data_type* pointer_variable_name;
int* p;

Reference operator (&) and Dereference operator (*)


& is called reference operator. It gives you the address of a variable.
gets you the value from the address, it is called a dereference operator (*).
Note: The * sign when declaring a pointer is not a dereference operator. It is just a similar
notation that creates a pointer.
/* Source code to demonstrate, handling of pointers in C program */
#include <stdio.h>
int main(){
int* pc;
int c;
c=22;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
return 0;
}
Output
Address of c: 2686784
Value of c: 22

Address of pointer pc: 2686784


Content of pointer pc: 22

Address of pointer pc: 2686784


Content of pointer pc: 11

Address of c: 2686784
Value of c: 2

NULL Pointers
A pointer that is assigned NULL is called a null pointer. NULL pointer is a constant with a value of
zero
#include <stdio.h>
int main () {
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr ); // output is 0
}
important pointer concepts
C - Pointer arithmetic
Incrementing a Pointer : ptr++ is equivalent to ptr + (sizeof(pointer_data_type)).
Decrementing a Pointer : ptr--; is equivalent to ptr - (sizeof(pointer_data_type)).
#include<stdio.h>
#include<conio.h>

void main() {
int int_var = 10, *int_ptr;
char char_var = 'A', *char_ptr;
float float_val = 4.65, *float_ptr;

/* Initialize pointers */
int_ptr = &int_var;
char_ptr = &char_var;
float_ptr = &float_val; Program Output
Address of int_var = 2293300
printf("Address of int_var = %u\n", int_ptr); Address of char_var = 2293299
printf("Address of char_var = %u\n", char_ptr); Address of float_var = 2293292
printf("Address of float_var = %u\n\n", float_ptr);
After increment address in int_ptr =
2293304
/* Incrementing pointers */ After increment address in char_ptr =
int_ptr++; 2293300
char_ptr++; After increment address in float_ptr =
float_ptr++; 2293296
printf("After increment address in int_ptr = %u\n", int_ptr);
printf("After increment address in char_ptr = %u\n", char_ptr); After addition address in int_ptr =
printf("After increment address in float_ptr = %u\n\n", float_ptr);2293312
After addition address in char_ptr =
2293302
/* Adding 2 to pointers */ After addition address in float_ptr =
int_ptr = int_ptr + 2; 2293304
char_ptr = char_ptr + 2;
float_ptr = float_ptr + 2;

printf("After addition address in int_ptr = %u\n", int_ptr);


printf("After addition address in char_ptr = %u\n", char_ptr);
printf("After addition address in float_ptr = %u\n\n", float_ptr);

}
Pointer and Arrays
int arr[5]={ 1, 2, 3, 4, 5 };
arr is equal to &arr[0] // by default
We can declare a pointer of type int to point to the array arr.
int *p;
p = arr;
or p = &arr[0]; //both the statements are equivalent.

Pointer to Array
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int *p = &a[0]
for (i=0; i<5; i++)
{
printf("%d", *p);
p++;
}
Array of Pointers
char *name[3]={
"Adam",
"chris",
"Deniel"
};
//Now see same array without using pointer
char name[3][20]= {
"Adam",
"chris",
"Deniel"
};
Pointers are very helpful in handling character array with rows of varying length.

In the second approach memory wastage is more, hence it is prefered to use pointer in such
cases.

Pointer to Structure
struct Book
{
char name[10];
int price;
}

int main()
{
struct Book a; //Single structure variable
struct Book* ptr; //Pointer of Structure type
ptr = &a;

struct Book b[10]; //Array of structure variables


struct Book* p; //Pointer of Structure type
p = &b;
}
Accessing Structure Members with Pointer
struct Book
{
char name[10];
int price;
}

int main()
{
struct Book b;
struct Book* ptr = &b;
ptr->name = "Dan Brown"; //Accessing Structure Members
ptr->price = 500;
}
To access members of structure with structure variable, we used the dot . operator. But when we
have a pointer of structure type, we use arrow -> to access structure members.
Pointer as Function parameter
Pointer to functions
possible to declare a pointer pointing to a function which can then be used as an argument in
another function. A pointer to a function is declared as follows,
type (*pointer-name)(parameter);

Example :
int (*sum)(); //legal declaraction of pointer to function
int *sum(); //This is not a declaraction of pointer to function

#include <stdio.h>
#include <conio.h>

int sum(int x, int y)


{
return x+y;
}

int main( )
{
int (*fp)(int, int);
int s;
fp = sum;
s = fp(10, 15); // (*fp)(10,15);
printf("Sum is %d",s);
getch();
return 0;
}
Output : 25
// A simple C program to show function pointers as parameter
#include <stdio.h>

// Two simple functions


void fun1() { printf("Fun1\n"); }
void fun2() { printf("Fun2\n"); }

// A function that receives a simple function


// as parameter and calls the function
void wrapper(void (*fun)())
{
fun();
}

int main()
{
wrapper(fun1);
wrapper(fun2);
return 0;
}
Output fun1, fun2
C Constant Pointer and Pointer to Constant
C Constant pointer
A pointer is said to be constant pointer when the address its pointing to cannot be changed.
A constant pointer is declared as :
<type-of-pointer> *const <name-of-pointer>
Lets take an example :
char ch, c;
char *ptr = &ch
ptr = &c
In the above example we defined two characters (ch and c) and a character pointer ptr. First,
the pointer ptr contained the address of ch and in the next line it contained the address of c. In
other words, we can say that Initially ptr pointed to ch and then it pointed to c.
But in case of a constant pointer, once a pointer holds an address, it cannot change it. This means
a constant pointer, if already pointing to an address, cannot point to a new address.
If we see the example above, then if ptr would have been a constant pointer, then the third line
would have not been valid.
include<stdio.h>

int main(void)
{
char ch = 'c';
char c = 'a';

char *const ptr = &ch; // A constant pointer


ptr = &c; // Trying to assign new address to a constant pointer. WRONG!!!!

return 0;
}
When the code above is compiled, compiler gives the following error :
$ gcc -Wall constptr.c -o constptr
constptr.c: In function main:
constptr.c:9: error: assignment of read-only variable ptr
C Pointer to Constant
const <type-of-pointer> *<name-of-pointer>;
char ch = 'c';
char *ptr = &ch
*ptr = 'a';
In the above example, we used a character pointer ptr that points to character ch. In the last
line, we change the value at address pointer by ptr. But if this would have been a pointer to a
constant, then the last line would have been invalid because a pointer to a constant cannot change
the value at the address its pointing to.
#include<stdio.h>
int main(void)
{
char ch = 'c';
const char *ptr = &ch; // A constant pointer 'ptr' pointing to 'ch'
*ptr = 'a';// WRONG!!! Cannot change the value at address pointed by 'ptr'.

return 0;
}
When the above code was compiled, compiler gave the following error :
$ gcc -Wall ptr2const.c -o ptr2const
ptr2const.c: In function main:
ptr2const.c:7: error: assignment of read-only location *ptr
C Pointer to Pointer

include <stdio.h>
#include <conio.h>

int main () {
int count = 10;
/* pointer to an integer variable */
int *ptr = &count;
/* pointer to a pointer*/
int **ptrToPtr = &ptr;

printf("Value of count variable = %d\n", count);


printf("Value of count variable retreived uisng ptr %d\n", *ptr);
printf("Value of count variable retreived uisng ptrToPtr = %d\n",
**ptrToPtr);

getch();
return 0;
}
Value of count variable = 10
Value of count variable retreived uisng ptr 10
Value of count variable retreived uisng ptrToPtr = 10

You might also like