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 pointed 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 variable x.

10 stored in some memory location. Say 1042

So, C Pointer variable *p will point 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 the pointer variable.

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

Data Type: C Pointer variable contains the address of other variables. So, there is no point in declaring its type. The Data Type in the syntax does not belong to the pointer variable. It belongs to the variable that is pointer by the pointer variable. For instance,

int *x;

It means C pointer variable x is pointed to the integer variable only. If you assign or point the pointer x to float or character variable, 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 variable a and x is the address of the pointer variable *x.

x = &a; means we are actually assigning the address of the variable 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 C programming pointer variable

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.

/* Example for Pointers in C Programming */
#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 C pointer example, we declared one integer variable Number and one pointer variable P of type integer.

int *P, Number;

Below printf statement asks the user to Enter any Positive Integer

printf("\n Please Enter any Positive Integer");

The scanf statement assigns the user entered value to the Number variable we already declared

scanf("%d", &Number);

It will print the value inside the Number (User Entered Value)

printf("\n Value Inside the Number = %d \n", Number);

It prints the Address of the declared variable (Number)

printf(" Address of the Number = %d\n", &Number);

In the Next line, We assigned the address of the Number variable to the address of the C pointer variable.

P = &Number;

Here, P is the address of the C pointer variable that we already declared (*P), and &Number is the address of the Number.

It prints the value inside the Pointer P. Here the address of the pointer 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 in C, 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 pointer variable *P

*P = 20;

It means,

  • Value inside the pointer variable *P = 20
  • From the above (P = &Number), The address of the C pointer variable and Number variable is the same. It means we are not only changing the value of pointer variable *p, But We are also actually changing the data inside the Number variable (Number)
  • It means, the User entered value inside the Number variable will be changed to 20.

It will print the value and address of the Number and Pointer variables.

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);