Pointers in C programming are the most powerful concept because pointer variables contain or hold the address of another variable. These are beneficial to hold the address of the variable. Using the address, we can access the value inside the associated variable. Or we can manipulate the value stored in that address. So, any changes made in the pointer will reflect the original value.
The Pointers in C example,
int x = 10; int *p;
x is the variable name.
10 is the value associated with or assigned to the x.
10 stored in some memory location. Say 1042
So, Pointer *p will assign to 1042 and access the value inside the x, i.e., 10.
What is a Pointer in C?
A pointer stores the memory address of another variable as its variable value. In the following example, we declared an integer data type variable. Next, we created a pointer variable and used the reference operator (&) to point its address to the pointer variable.
When we try to print the memory address, both the pointer variable and the &number print the same value (the memory address of a number variable).
#include <stdio.h>
int main()
{
int number = 100;
int *ptr = &number;
printf("Number = %d\n", number);
printf("1. Memory Address = %d\n", &number);
printf("2. Memory Address = %d\n", ptr);
return 0;
}
Number = 10
Memory Address = 6291060
Memory Address = 6291060
TIP: If we use the ptr, we get the memory address. However, if we use *ptr inside the printf(), it returns the original value of a variable, that is, 10.
What is a memory address in a C pointers?
In the computer world, the System will allocate memory (in Bytes) as required for each variable we store. It means every variable has a value and memory. In general, if we use the reference operator (&) against any variable, we can get the memory address of that variable.
In the following example, we declared an integer data type. When we call the variable, it prints the actual value inside the variable. However, if we call with the reference operator (&number), it returns the memory address.
#include <stdio.h>
int main()
{
int number = 10;
printf("Number = %d\n", number);
printf("Memory Address = %d\n", &number);
return 0;
}
Number = 10
Memory Address = 6291068
C Pointers Syntax
The Syntax of the Pointers in this programing language is shown below.
Data_Type *Pointer_Name;
From the above pointer syntax
Pointer_Name: Name of it.
(*): It tells that the variable is a pointer.
Data Type: The C pointer variable contains the address of others. So, there is no use in declaring its type. The Data Type in the syntax does not belong to the pointer. It belongs to the variable that is associated with the pointer. For instance,
int *x;
It means the pointer variable x is assigned to the integer only. If you assign the pointer x to float or character data type, then it will give a strange result. Say
int a; float b; x = &a; // Correct because we are assigning integer variable x = &b; //Wrong because we are assigning float variable
As we all know, &a is the address of the a, and x is the addition of the *x.
x = &a; means we are actually assigning the address of the a to x.
To access the value inside the pointers variable, we can simply use *x.
int *x;
*x = 20;
printf("%d",*x)
To access the address of the pointers.
printf("%d", x)
C Pointers Example
This program allows the user to enter any positive integer. Using the variable address, access the user-entered value. Next, using the pointers variable, change the user-entered value.
#include <stdio.h>
int main()
{
int *P,Number;
printf("\n Please Enter any Positive Integer");
scanf("%d", &Number);
printf("\n Value Inside the Number = %d \n", Number);
printf(" Address of the Number = %d\n",&Number);
P = &Number;
printf("\n Value Inside the Pointer P = %d \n", *P);
printf(" Address of the Pointer P = %d\n", P);
*P = 20;
printf("\n Value Inside the Pointer P = %d \n", *P);
printf(" Address of the Pointer P = %d\n", P);
printf(" Value Inside the Number = %d \n", Number);
printf(" Address of the Number = %d\n",&Number);
return 0;
}

In this example, we declared one integer Number and one pointer P of type integer. The printf statement informs the user to enter any Positive Integer. The scanf statement assigns the user-entered value to the Number we already declared.
The first statement prints the value inside the Number (User Entered Value), and the next one prints the declared number’s address.
In the Next line, we assigned the address of the Number to the address of the C pointers. Here, P is the address of the pointer that we already declared (*P), and &Number is the address of the Number.
P = &Number;
It prints the value inside the P. Here is the address of the P assigned to the Number address. We are actually printing the User Entered Value, i.e., Number.
printf("\n Value Inside the Pointer P = %d \n", *P);
Prints the Address of the Pointer, which is obviously the same as the address of the Number.
printf(" Address of the Pointer P = %d\n", P);
In the Next line, We are assigning 20 to the *P
*P = 20;
It It means,
- Value inside the *P = 20
- From the above (P = &Number), the address of the pointers variable and the Number variable in this programming are the same. It means we are not only changing the value of *p, But We are also actually changing the data inside the Number variable (Number)
- It means the user-entered value inside the Number will be changed to 20.
The last four printf statements will print the value and address of the Number and Pointer variables.
TIP: I suggest you refer to the Array, string, functions, passing a pointer to functions, pass array to a function, and the fgets function articles.
What is the size of a Pointer in C?
The size of the pointer depends on the system architecture. On a 32-bit system, it occupies 4 bytes. On the other hand, a 64-bit system requires 8 bytes for a pointer.
In the following example, we declared integer, float, and string data type pointers. Next, the sizeof operator prints the size of each pointer. When you look at the output, it returns 8 bytes for each pointer. Here, the pointer does not consider the data type; it only allocates 8 bytes for all kinds of data types.
#include <stdio.h>
int main()
{
int *i;
float *sales;
char *str;
printf("%zu\n", sizeof(i));
printf("%zu\n", sizeof(sales));
printf("%zu", sizeof(str));
return 0;
}
8
8
8