Pointers in C

Pointers in C programming is the most powerful concept because pointer variables in C contain or hold the address of another variable. Pointers in C 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.

In the computer world, the System will allocate the memory (in Bytes) as per the requirement to each variable we store. It means, Every variable has value and memory. The Pointers in c example,

int x = 10;

int *p;

x is the variable name.

10 is the value associated or assigned to the x.

10 stored in some memory location. Say 1042

So, C Pointer *p will assign to 1042 and access the value inside the x, i.e., 10.

Syntax of a Pointers in C

Data_Type *Pointer_Name;

Pointer_Name: Name of it.

(*): It tells that the variable is a pointer.

Data Type: 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 by the pointer. For instance,

int *x;

It means C 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 add of the *x.

x = &a; means we are actually assigning the address of the a to x.

To access the value inside the C pointer variable, we can simply use *x.

int *x;

*x = 20;

printf("%d",*x)

To access the address of the pointers in C programming

printf("%d", x)

C Pointer Example

This C pointer program allows the user to enter any positive integer. Using the address of the variable, access the user-entered value. Next, using the pointer 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;
}
Pointers in C Programming

In this example, we declared one integer Number and one pointer P of type integer.

The printf statement asks the user to Enter any Positive Integer. And the scanf statement assigns the user entered value to the Number we already declared.

The first statement will print the value inside the Number (User Entered Value), and the next one prints the Address of the declared Number.

In the Next line, We assigned the address of the Number to the address of the C pointer. 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 the address of the P assigned to the address of the Number So, 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 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 means,

  • Value inside the *P = 20
  • From the above (P = &Number), The address of the C pointer variable and Number var is 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 addres of the Number and Pointer variables.