Pointers are used in C program to access the memory and manipulate the address.
Pointer variable or simply pointer are the special types of variables that holds memory address rather than data, that is, a variable that holds address value is called a pointer variable or simply a pointer.
Dynamic memory allocation, cannot be performed without using pointers.
If x is a variable then, &x is the address in memory.Reference operator(&)
/* Example to demonstrate use of reference operator in C programming. */#include <stdio.h>
int main()
{
int var=5;
printf("Value: %d\n",var);
printf("Address: %d",&var);
return 0;
}
Output
int var=5;
printf("Value: %d\n",var);
printf("Address: %d",&var);
return 0;
}
Output
Value: 5
Address: 2898058
Reference operator(*)
/* Source code to demonstrate, handling of pointers in C program */#include <stdio.h>
int main()
{
int * pc; // declaration of pointer
int c;
c=22;
printf("Address of c: %d \n",&c);
printf("Value of c: %d \n\n", c);
pc=&c;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%d\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
int c, *pc;
pc=c; /* pc is address whereas, c is not an address. */
*pc=&c; /* &c is address whereas, *pc is not an address. */
In both cases, pointer pc is not pointing to the address of c.
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}
-----------------------------------------------------------------------------------------------
int c, *pc ;
// c is variable & pc is pointer. c can hold value & pc can hold memory address.
c=25;
pc=&c ;
int * pc; // declaration of pointer
int c;
c=22;
printf("Address of c: %d \n",&c);
printf("Value of c: %d \n\n", c);
pc=&c;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%d\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
Commonly done mistakes in pointers
Suppose, the programmer wants pointer pc to point to the address of c. Then,int c, *pc;
pc=c; /* pc is address whereas, c is not an address. */
*pc=&c; /* &c is address whereas, *pc is not an address. */
In both cases, pointer pc is not pointing to the address of c.
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}
-----------------------------------------------------------------------------------------------
int c, *pc ;
// c is variable & pc is pointer. c can hold value & pc can hold memory address.
c=25;
pc=&c ;
No comments:
Post a Comment