Sei sulla pagina 1di 21

Pointer Array:

Here, we will learn how to declare, initialize and assign pointer to an


array?

Pointer to an array is the pointer of array variable.

Declaration of pointer to array


data_type *pointer_variable;

Initialization of pointer to array


pointer_variable=&array_variable[0]; OR
pointer_variable=array_variable;
Let’s consider the declaration and initialization of
pointer to integer array
1) Declare integer array with the number of
elements
int arr[5];
2) Declare integer pointer to point the array
int *ptr;
3) Initialize pointer with the base address of array
ptr= &arr[0]; OR pt= arr;
4) Initialization with the pointer declaration
int *ptr= &arr[0]; OR int *ptr= arr;
#include <stdio.h>
int main()
{
int arr[]={10,20,30,40,50};
int *ptr,loop;
//initialize ptr
ptr = arr;
//printing the elements
for(loop=0; loop<5; loop++)
printf("arr[%d]: %d\n",loop,*(ptr+loop));
return 0;
}
Output
arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50
#include <stdio.h>
int main()
{
int arr[5];
int *ptr=&arr[0];
int i;
printf("Enter array elements:\n");
for(i=0; i<5; i++)
{
printf("Enter element [%d]: ",i+1);
scanf("%d",(ptr+i));
}
printf("Array elements are:\n");
for(i=0; i<5; i++)
{
Output
Enter array elements:
Enter element [1]: 100
Enter element [2]: 200
Enter element [3]: 300
Enter element [4]: 400
Enter element [5]: 500
Array elements are:
Element [1]: 100
Element [2]: 200
Element [3]: 300
Element [4]: 400
Element [5]: 500
Dynamic array initialization using malloc()
Syntax
data_type *pointer_variable = (data_type*) malloc
(N * sizeof(data_type));
Here, N is the number of elements
Example
int *ptr = (int*) malloc(5*sizeof(int));
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n;
int *ptr;
printf("Enter total number of elements: ");
scanf("%d",&n);
//pointer initialization
ptr=(int*)malloc(n*sizeof(int));
printf("Enter array elements:\n");
for(i=0; i<n; i++)
{
printf("Enter element [%d]: ",i+1);
scanf("%d",(ptr+i));
Output
Enter total number of elements: 5
Enter array elements:
Enter element [1]: 100
Enter element [2]: 200
Enter element [3]: 300
Enter element [4]: 400
Enter element [5]: 500
Array elements are:
Element [1]: 100
Element [2]: 200
Element [3]: 300
Element [4]: 400
A simple example to print the address of array elements
#include <stdio.h>
int main( )
{
int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;
/* for loop to print value and address of each element of
array*/
for ( int i = 0 ; i < 7 ; i++ )
{
printf("val[%d]: value is %d and address is %d\n", i, val[i],
&val[i]);
}
return 0;
}
Output:
val[0]: value is 11 and address is 1423453232
val[1]: value is 22 and address is 1423453236
val[2]: value is 33 and address is 1423453240
val[3]: value is 44 and address is 1423453244
val[4]: value is 55 and address is 1423453248
val[5]: value is 66 and address is 1423453252
val[6]: value is 77 and address is 1423453256
LINKED LISTS
// A linked list node
struct Node {
int data;
struct Node* next;
};
// A simple C program to introduce
// a linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Program to create a simple linked
// list with 3 nodes
int main()
{
struct Node* head = NULL;
struct Node* second = NULL;

Potrebbero piacerti anche