Table of Contents
C programming pointers
The strong feature of C and C++ programming is Pointer through which it is more prominent than other popular programming languages like Python and Java.
Pointers are used in C programming to access memory and manipulate memory addresses.
C memory address
Before taking up the concept of pointers let’s get familiar with C programming address.
If your program has a count variable, &count
it will give you the memory address. Here &
k is called the reference operator.
You must see this symbol when you scanf()
use the function. It is used to store the input value to the address of the variable by the user.
/*This program is for better understanding of reference operator in C programming. */
#include
int main(){
int count=5;
printf("Value: %d\n",count);
printf("Address: %u",&count); // Notice that ampersand(&) is used before the count variable.
return 0;
}
output
Value: 5
Address: 2686778
Note: Using this same code you can get different values of address.
In the above source code the value 5 is stored at location 2686778 in memory. count is only the name of that location.
C Pointer variable
In C programming, there is a type of special variable that stores the address of another variable. This is called a pointer variable or just a pointer.
Pointer declaration
data_type* pointer_variable_name;
int * p;
In the above statement, p is int
declared as a pointer variable of type .
C reference(&) and dereference(*) operators
As mentioned earlier, & is called the reference operator. Through this you will get the address of the variable.
Similarly, there is another operator with which you can get a value from an address. This is called the dereference(*) operator.
The following example clearly explains the use of pointers, reference operators and dereference operators.
Note:*
Symbol used in pointer declaration is not dereference operator. It is just a symbol for pointer instruction.
Example: C program to learn pointer usage
#include
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
Explanation of above program and diagram
- int* pc;Creates the pointer variable pc and creates int c;the general variable c . Since both pc and c are uninitialized, the pointer pc does not point to either the address or the random address. Similarly the address of variable c is assigned but it contains random/garbage value.
- c=22;22 is assigned to the variable c, that is, 22 is stored in the memory location of the variable c . Remember, &cwhen printing the address of c(variable) we will use %dit instead %usince address is always positive (unsigned integer).
- pc=&c;Assigns the address of variable c to pointer pc . When printed you will see that pointer pc address and c address will be same. Also the content of pointer pc will be same(22).
- c=11;Assigns 11 to variable c . We assign the new value to the variable c which affects the pointer pc .
- Since pointer pc and c point to the same address, pointer pc refers to the same value 11. The update value 11 is displayed when printing the address and contents of pc .
- *pc=2;Assigns 2 by changing the previous contents of the memory location pointed to by pointer pc . Since pointer pc and variable c point to the same address, the value of c changes to 2.
Common Mistakes When Using Pointers
Suppose you want pointer pc to point to the address of c. then
int c, *pc;
pc = c; /* Error! where pc is the address but c is not the address. */
*pc=&c; /* Error! Where *pc is the value pointed to by address and &c is the address. */
pc=&c; /* Correct! Since both pc and &c are addresses. */
*pc=c; /* Correct! Since *pc is the value indicated by the address and c is the value. */
In both cases the pointer pc does not point to the address of c.
Pingback: C Call by Reference: Using pointers -
Pingback: C Dynamic Memory Allocation -