Initialize a Pointer Variable In this tutorial section let learn how to declare and initialize value to pointer variable? When the pointer is declared initially it contains garbage value. Garbage value means the pointer could point anywhere in the memory. We need to assign the value to the pointer. Value assigned to pointer is an address of a variable.
Initialization of Pointers in C programming.
“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.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ