Assign, initialize pointer-variable in C programming language.


“The process of assigning the address of a variable to a pointer is known as pointer initialization.”

& the address operator is used to find the address of a variable.
The & operator immediately preceding a variable name returns the address of the variable associated with it.
Syntax:
Data type *pointer-variable;
The given above syntax is to declare a pointer variable.
eg.

int var=20;
int *ptr1;
ptr1=&var;
The integer variable var is declared and initialized value 20.
int *ptr1;
*ptr1=&var;
integer pointer *ptr1 has declared .And pointer ptr is assigned the address of var.
Now the pointer *ptr1 pointing to the value of var.The pointed value is 20.



C program to demonstrate declare and initialize pointer

#include<stdio.h>
int main()
{
  int var = 20; 
  int *ptr1;  // declare a integer pointer
  ptr1 = &y; // assign value to pointer ptr1
  printf("The Value at ptr1 variable is: %d \n", *ptr1);
  printf("Address pointed by ptr1 is: %p \n", ptr1);
  return 0;
}

Value at ptr is: 20
Address pointed by ptr is: 0x7fff99c0e6c4



Previous Topic:-->> Pointer Declaration in C. || Next topic:-->>syntax Pointer Initialization.