Sie sind auf Seite 1von 23

Arrays and Pointers

Pointers and arrays

char array[10]={5,10,15,20,25,30,35,40,45,50 };
// an array with ten elements.

char *ptr_toarray = &array[0];


//declare that the pointer *ptr_toarray is the same as
array[0] (the same as the first element of the array).

*(ptr_toarray + 2); // is same as array[2]

*(ptr_toarray + 2); is something different then *(ptr_toarray)


+ 2; )
Arrays and Pointers

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]

If Base address is 1000


Sizeof(int) =4

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Base +0 +4 +8 +12 +16 etc.


Address
Arrays and Pointers
Passing arrays

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
}

(Because array is really


int
a pointer)
main(void)
{
int a[10], b[5];
foo(a, 10) foo(b, 5)
What does this print?
printf(%d\n, sizeof(a));
} 40
Pointer Arithmetic
Example: Int A[10];

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

A+1 means: A+4 means:

Adds 1*sizeof(int) to Adds 4*sizeof(int) to


the memory address the memory address

Warning: Pointer arithmetic need to be performed carefully


Arrays and Pointers

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]

If Base address is 1000


Sizeof(int) =4

[0][0] [0][1] [0][2] [0][3] [0][4] [1][0] [1][1] [1][2] [1][3] [1][4]

Base +0 +4 +8 +12 +16 etc.


Address
Array of Pointers
Just like array of integers or characters, there can be array of pointers
too. An array of pointers can be declared as :
<type> *<name>[<number-of-elements];
For example :
char *ptr[3];

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.

/* Return name of n-th 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 );

/* move to the previous location */


ptr--;
}
return 0;
}
What is the output of following program:
:
#include <stdio.h> Output:
int MAX = 3; Address of var[3] = bfedbcd8
int main () Value of var[3] = 200
{ Address of var[2] = bfedbcd4
int var[] = {10, 100, 200}; Value of var[2] = 100
int i, *ptr; Address of var[1] = bfedbcd0
/* let us have array address in pointer */ Value of var[1] = 10
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 );

/* move to the previous location */


ptr--;
}
return 0;
}
What is the output of following program:
:
int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
/* let us have address of the first element in
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:
:
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];

for ( i = 0; i < MAX; i++)


{
ptr[i] = &var[i]; /* assign the address of integer.
*/
}
for ( i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}
What is the output of following program:
:
#include <stdio.h> Address of var[0] = bfdbcb20
int MAX = 3; Value of var[0] = 10
int main () Address of var[1] = bfdbcb24
{ Value of var[1] = 100
int var[] = {10, 100, 200}; Address of var[2] = bfdbcb28
int i, *ptr[MAX]; Value of var[2] = 200

for ( i = 0; i < MAX; i++)


{
ptr[i] = &var[i]; /* assign the address of integer.
*/
}
for ( i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}
What is the output of following program:
:
#include <stdio.h>
const int MAX = 4;
int main ()
{
char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",
};
int i = 0;
for ( i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i]
);
}
return 0;
}
What is the output of following program:
:
#include <stdio.h> Value of names[0] = Zara Ali
const int MAX = 4; Value of names[1] = Hina Ali
int main () Value of names[2] = Nuha Ali
{ Value of names[3] = Sara Ali
char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",
};
int i = 0;
for ( i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i]
);
}
return 0;
}
What is the output of following program:
:
#include <stdio.h>
int main ()
{
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of
operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n",
**pptr);
return 0;
}
What is the output of following program:
:
#include <stdio.h> Value of var = 3000
int main () Value available at *ptr = 3000
{ Value available at **pptr = 3000
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of
operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n",
**pptr);
return 0;
}