char array[10]={5,10,15,20,25,30,35,40,45,50 };
// an array with ten elements.
An array is a way to store many values under the same name in adjacent memory
locations.
Example: Int A[10];
Memory Allocation:
Base Address is :address of A[0]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Must explicitly
int *array pass the size
o Array pointer to the first (0th) array
element int
foo(int array[],
o a[i] *(a+i) unsigned int size)
{
array[size - 1]
o An array is passed to a function as a
}
pointer
o The array size is lost !
int
main(void)
o Usually bad style to interchange {
arrays and pointers
int a[10], b[5];
o Avoid pointer arithmetic!
foo(a, 10) foo(b, 5)
}
Arrays and Pointers
int
foo(int array[],
unsigned int size)
{
What does this print?
printf(%d\n, sizeof(array)); Prints the size of an pointer: 8
}
Memory Allocation:
Base Address is :address of A[0] i.e A
[0] is[1]
If Base address 1000 [2] [3] [4] [5] [6] [7] [8] [9]
Sizeof(int) =4
Base +0 +4 +8 +12 +16 etc.
Address
An array is a way to store many values under the same name in adjacent memory
locations.
Example: Int A[2][5];
Memory Allocation:
Base Address is :address of A[0][0]
[0][0] [0][1] [0][2] [0][3] [0][4] [1][0] [1][1] [1][2] [1][3] [1][4]
int main()
{
int i;
char *students[3]={Ram, John, Abdul};
for (i=0; i<3; i++}
{
printf(%s\n, students[i]);
}
return 0;
}
Array of Pointers
#include<stdio.h>
int main(void)
{
char *p1 = "Himanshu";
char *p2 = "Arora";
char *p3 = "India";
char *arr[3];
arr[0] = p1;
arr[1] = p2;
arr[2] = p3;
printf("\n p1 = [%s] \n",p1);
printf("\n p2 = [%s] \n",p2);
printf("\n p3 = [%s] \n",p3);
printf("\n arr[0] = [%s] \n",arr[0]);
printf("\n arr[1] = [%s] \n",arr[1]);
printf("\n arr[2] = [%s] \n",arr[2]);
return 0;
}
What is the output of the above program?
Array of Pointers
Example 2: Write a function which returns a pointer to a character string
containing the name of the nth month.
char *month_name(int n)
{
static char *name[] = {
"Illegal month",
"January", "February", "March",
"April", "May", "June",
"July", "August, "September",
"October", "November", "December"
};
return (n < 1 || n > 12) ? name[0] : name[n];
}
Array of Pointers
Memory Representation
name Value
0 Illegal Month
1 January
2 February
11 November
12 December
What is the output of following program:
:
#include <stdio.h>
int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr; /* let us have array address in pointer */
ptr = var; for ( i = 0; i < MAX; i++)
{
printf("Address of var[%d] = %x\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr ); /* move to
the next location */
ptr++;
}
return 0;
}
What is the output of following program:
#include <stdio.h>
int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr; /* let us have array address in pointer */
ptr = var; for ( i = 0; i < MAX; i++)
{
printf("Address of var[%d] = %x\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr ); /* move to
the next location */
ptr++;
}
return 0;
}
------------------------Output --------------------------------
Address of var[0] = 3a9cb8a0
Value of var[0] = 10
Address of var[1] = 3a9cb8a4
Value of var[1] = 100
Address of var[2] = 3a9cb8a8
What is the output of following program:
:
#include <stdio.h>
int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
/* let us have array address in pointer */
ptr = &var[MAX-1];
for ( i = MAX; i > 0; i--)
{
printf("Address of var[%d] = %x\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr );
ptr++;
i++;
}
return 0;
}
What is the output of following program:
:
int MAX = 3; Address of var[0] = bfdbcb20
int main () Value of var[0] = 10
{ Address of var[1] = bfdbcb24
int var[] = {10, 100, 200}; Value of var[1] = 100
int i, *ptr; Address of var[2] = bfdbcb28
/* let us have address of the first element in Value of var[2] = 200
pointer */
ptr = var;
i = 0;
while ( ptr <= &var[MAX - 1] )
{
printf("Address of var[%d] = %x\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr );
ptr++;
i++;
}
return 0;
}
What is the output of following program:
:
#include <stdio.h>
int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr[MAX];