Array and Pointers in C
Pointers and Arrays in C
Pointers and arrays are closely related, but they are not synonymous.
Arrays
-
An array is a non-empty set of sequentially indexed elements having the same data type.
-
Each element has a unique index number, and changes to one element do not affect others.
-
An array occupies a contiguous block of memory.
-
The array name represents the base address (address of the zeroth element).
Array notation and pointers:
-
*agives the zeroth element. -
*(a+1)gives the first element. -
a[i]is equivalent to*(a + i).
Example Program
Explanation:
-
a[0]and*a→ prints 10 (zeroth element) -
aand&a[0]→ prints base address of the array -
&a[1]and(a+1)→ prints address of first element -
*(a+1)→ prints 20 (first element)
Pointers to Arrays
-
You can assign an array to a pointer:
-
A pointer does not have to point to the first element; it can point to any element:
-
Array elements can be accessed using the pointer:
-
Incrementing a pointer moves it by the size of its data type (scale factor).
Memory example (int = 4 bytes):
| Pointer | Address |
|---|---|
| p | &a[0]=1000 |
| p+1 | &a[1]=1004 |
| p+2 | &a[2]=1008 |
| p+3 | &a[3]=1012 |
| p+4 | &a[4]=1016 |
Address calculation:
-
Accessing element:
Important Note
-
The array name is a constant address and cannot be changed.
-
Pointers are modifiable, so they can point to different memory locations.
-
Array cannot be used as an lvalue, but a pointer can.
Example Question
Output:
Explanation:
-
*a→ 30 -
*a + 1→ 30 + 1 = 31 -
*(a+1)→ 20
Difference between Arrays and Pointers
Arrays Pointers Space is allocated automatically Must be explicitly assigned to allocated space Cannot be resized Can be resized using realloc() Cannot be reassigned Can be reassigned to different addresses sizeof(arrayname) → total bytes of array sizeof(p) → bytes used by pointer variable
| Arrays | Pointers |
|---|---|
| Space is allocated automatically | Must be explicitly assigned to allocated space |
| Cannot be resized | Can be resized using realloc() |
| Cannot be reassigned | Can be reassigned to different addresses |
sizeof(arrayname) → total bytes of array | sizeof(p) → bytes used by pointer variable |
#include <stdio.h>
void main()
{
int a[5],i;
int *p;
p=a;
printf("\nenter 5 numbers...\n");
for(i=0;i<5;i++)
scanf("%d",(p+i));
printf("\neven numbers..\n");
for (i=0;i<5;i++)
if (*(p+i)%2==0) printf("%d\n",*(p+i));
}
{
int i,j,temp;
for(i=0,j=n-1;i<j;i++,j--)
{
temp=*(ptr+i);
*(ptr+i)=*(ptr+j);
*(ptr+j)=temp;
}
}
int main()
{
int a[10]={3,2,4,1,5,7,8,9,6,0};
int i;
reverse(a,10);
printf("The reversed array is\n");
for(i=0;i<10;i++)
printf("%2d",a[i]);
}
#include <stdlib.h>
int main()
{
int *p,i,n;
printf("Enter.....n\n");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
printf("Enter array elements...\n");
for(i=0;i<n;i++)
scanf("%d",p+i);
printf("Even numbers in the array...\n");
for(i=0;i<n;i++)
if(*(p+i)%2==0) printf("%d\n",*(p+i));
}
{
int i,j;
char t;
for(i=0;*(pstr+i)!='\0';i++); // finding the end of the string
i--;
for(j=0;j<i;i--,j++) // swapping characters first and last
{t=*(pstr+i);
*(pstr+i)=*(pstr+j);
*(pstr+j)=t;
}
}
Comments
Post a Comment