Initializing Pointers:
In this tutorial we will learn the syntax and examples of initializing pointers.
Pointers are initialized by assigning it a valid memory address of the variable.
The initialization is normally done with the help of & the address-of operator. The & operator returns the address of the variable.
For e.g. int n;
Int *ptr=&n;
Pointer initialization syntax in C programming.
The n is an int variable, &n returns the address of the variable n .
Following are the syntax for pointer variable initialization:
1. Static initialization.
2. Dynamic initialization.
1. Static Initialization:
The process of pointer variable initialization at compile time is known as static initialization.
Once the static variable is initialized its scope remains until the program termination. The static variable holds its value even the variable goes out of scope. static variable cannot be re-declared and initialized once it is declared and initialized. The scope and lifetime of static exist till the termination of that program.
A declaration of static pointer is shown below.
Syntax:
data type variable_name=value;
static data type *ptr_variable=&variable_name;
C pragram for static variable Declaration
#include<stdio.h>
void staticptr()
{
int x = 100, y = 200;
static int *ptr; // ptr is declared as static pointer variable
if(ptr == 0)
{
ptr = &x;
}
else
{
ptr = &y;
}
printf("value = %d\n", *ptr);
printf("address of pointer ptr = %d\n", &ptr);
printf("address of memory pointed by pointer ptr = %d\n", ptr);
printf("address of x = %d\n", &x);
printf("address of y = %d\n", &y);
}
int main(){
staticptr();
staticptr();
}
Output:
value = 100
address of pointer ptr= 4218932
address of memory pointed by pointer ptr = 6422204
address of x = 6422204
address of y = 6422200
value = 200
address of pointer ptr= 4218932
address of memory pointed by pointer ptr = 6422200
address of x = 6422204
address of y = 6422200
In the given above program a function staticptr() has been called from the main() function. In first call of the function, static variable is initialized to zero 0 by compiler , so value of 'x' and ‘y’ will be assigned and as *ptr is static variable it holds its value even after completion of function execution. So that in second iteration of staticptr() static pointer 'ptr' is assigned to y's value. We can observe the given output. Address of static pointer is constant throughout the execution of program. But where it points-to can be modified.
The process of initializing the pointer variable at run time in c programming is known as dynamic initialization.
Syntax:
data type *ptr-variable;
ptr-variable=(data type *)malloc(sizeof(data type));
For example,
int *ptr;
ptr = (int *)malloc(sizeof(int)); //dynamic initialization
Here, the malloc() function is used to allocate memory at run time and the ptr is a pointer variable that is initialized at run time.
The C program to create dynamic array
#include<stdio.h>
#include <stdlib.h>
int main()
{
int* ptr; // This pointer will hold the
// base address of the block
int n, q;
printf("\nEnter The number of elements:");
scanf("%d",&n);
printf("\nThe number You entered is: %d", n);
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) { // check if the memory is allocated
printf("Memory has been not allocated.\n");
exit(0);
}
else {
// Memory has been successfully allocated
printf("Memory has been allocated by using malloc()for %d numbers .\n",n);
// Assignes the elements to the array
for (q = 0; q < n; q++) {
ptr[q] = q * 2;
}
// show the elements of the array
printf("The elements in the array are: ");
for (q = 0; q < n; q++) {
printf("%d, ", ptr[q]);
}
}
return 0;
}
Output:
Enter number of elements: 10
Memory has been allocated by using malloc()for 10 numbers .
The elements of the array are: 0, 2, 4, 6, 8,10,12,14,16,18
Previous Topic:-->> Pointers initialization in C. || Next topic:-->>Use of Pointer in C.
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