Sie sind auf Seite 1von 23

# Arrays and Pointers

## Pointers and arrays

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

## char *ptr_toarray = &array;

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

## *(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;

Memory Allocation:

## If Base address is 1000

Sizeof(int) =4

         

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

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, b;
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, b;
foo(a, 10) foo(b, 5)
What does this print?
printf(%d\n, sizeof(a));
} 40
Pointer Arithmetic
Example: Int A;

Memory Allocation:

 is
If Base address 1000        
Sizeof(int) =4
Base +0 +4 +8 +12 +16 etc.

## 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;

Memory Allocation:

## If Base address is 1000

Sizeof(int) =4

         

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

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;

int main()
{
int i;
char *students={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;
arr = p1;
arr = p2;
arr = p3;
printf("\n p1 = [%s] \n",p1);
printf("\n p2 = [%s] \n",p2);
printf("\n p3 = [%s] \n",p3);
printf("\n arr = [%s] \n",arr);
printf("\n arr = [%s] \n",arr);
printf("\n arr = [%s] \n",arr);
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 : 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 --------------------------------
Value of var = 10
Value of var = 100
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 = bfedbcd8
int main () Value of var = 200
{ Address of var = bfedbcd4
int var[] = {10, 100, 200}; Value of var = 100
int i, *ptr; Address of var = bfedbcd0
/* let us have array address in pointer */ Value of var = 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 = bfdbcb20
int main () Value of var = 10
{ Address of var = bfdbcb24
int var[] = {10, 100, 200}; Value of var = 100
int i, *ptr; Address of var = bfdbcb28
/* let us have address of the first element in Value of var = 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 = bfdbcb20
int MAX = 3; Value of var = 10
int main () Address of var = bfdbcb24
{ Value of var = 100
int var[] = {10, 100, 200}; Address of var = bfdbcb28
int i, *ptr[MAX]; Value of var = 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 = Zara Ali
const int MAX = 4; Value of names = Hina Ali
int main () Value of names = Nuha Ali
{ Value of names = 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;
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;