You are on page 1of 20

Arrays & Pointers in C

Version 0.1

Objectives

Declare

, initialize ,manipulate arrays

Identify situations where to use pointers. use arrays as function parameters differentiate between array & pointer Access & Manipulate arrays using

pointers

Oasis/Module/Topic/LU/Version No/Author

Table of Contents

Arrays in C C-Pointers C Pointer An Example Altering a variable in the Calling function Altering a variable in the Calling function by pointer Arrays & Pointers Accessing array using pointer Difference between Array & Pointer Passing an Array to a function Code to show passing an array to a function Two dimensional Arrays Example 2D Array Dynamically Allocating a 2D Array
Oasis/Module/Topic/LU/Version No/Author

Arrays in C

C array - formed by laying out all the elements contiguously in memory The array as a whole is referred to by the address of the first element. For example int a[6];

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

The statement int a[6] defines an array a of size 6 , where each cell can hold an integer element.
Similar definitions - char a[6] , float c[100]

In C arrays are 0 based ie the index starts from 0 to n-1 if number of elements to be stored be n
The notation a[i] refers to the i-th element of the array.
4

Oasis/Module/Topic/LU/Version No/Author

C-Pointers

Typical computer --many consecutively numbered memory locations. Each location can store a byte. Memory locations may be manipulated individually or in contiguous group( if multibyte things are stored).

Each memory location has its own address.


A pointer is a group of cells( ie a variable ) that can hold an address

operator & gives the address of an object. The statement p=&c assigns the address of c to the variable p . Now p is said to point to c.
Oasis/Module/Topic/LU/Version No/Author

C Pointer An Example

int x=1 , y=2 ; int * ptr ; char * cptr ptr is variable of type pointer to an integer . That is space allocated for ptr can hold the address of an integer. Similarly cptr is a variable of type pointer to a character variable. * is the indirection or dereferencing operator when applied to a pointer , accesses the object the pointer points to. int x=1 , y=2 ; int *ptr ; ptr=&x; /*ptr now points to x*/ y=*ptr /*y is now 1*/ *ptr=0; /*x is now 0*/
Oasis/Module/Topic/LU/Version No/Author

Altering a variable in the Calling function


C passes arguments to functions by values There is no direct way for the called function to alter a variable in the calling function. Need to swap two variables a & b in the main function , the following swap function is coded.
Void swap( int x , int y) { int temp; temp=x; x=y; y=temp; } main() { int i=8, j=9; swap( i , j); }

Will the above code serve the purpose?


Oasis/Module/Topic/LU/Version No/Author

Altering a variable in the Calling function by pointer

To alter a variable's value in the calling function we need to pass the address of the variable to the called function .
So the swap code can now be written as void swap( int *x , int *y) { int temp; temp=*x; *x=*y; * y=temp; }

In the calling function the code should be

main() { int i=8, j=9; swap( &i , &j);/*&i , & j - the address of variables i & j */ }

Oasis/Module/Topic/LU/Version No/Author

Arrays & Pointers

In an array say int a[5] , the name of the array ie a is the address of the first element of the array.

The memory picture for an array in term of offset( ie considering first element at location 0) follows Assume an integer element is placed at each location which takes 4 bytes each. Location 0 a[0] loaction 4 a[1] location 8 a[2] location 12 a[3] location 16 a[4] The name of the array is pointer to the first element so the second element's address is a+1 , the third element's address is a+2 and so on. To refer a[i] we use *(a+i)

Oasis/Module/Topic/LU/Version No/Author

Accessing array using pointer

Code finds the minimum element of an array using pointer notation and writes a 0 in that location.
main() { int a[10]={ 100 , 788 , 999, 10 , 22, 45 , 876, 32 , 67 , 55}; int min , i; min = *a; index =0; for ( i=1; i< 10; i++) { if (*(a+i) < min ){ min =*(a+i) index=i;} } *(a+index) = 0; printf(The minimum of the array is %d at loaction %d , min, index); }

10

Oasis/Module/Topic/LU/Version No/Author

Difference between Array & Pointer

One distinction between an array and a pointer, is that the pointer which represents the base address of an array cannot be changed in the code.
int intArray[100] int *ptr; int i; intArray = NULL; Error cannot change base addr ptr intArray = &i; Error intArray = intArray + 1; Error intArray++; Error ptr = intArray; ok, ptr is a regular pointer which can be changed

ptr now pointing to same array intArray

ptr++; ok, ptr can still be changed (and intArray cannot) ptr = NULL; ptr = &i; ok

11

Oasis/Module/Topic/LU/Version No/Author

Passing an Array to a function


When

an array is passed into a function, the function receives not a copy of the array, but instead the address of the first element of the array. is function receives a pointer to the start of the array. modifications made via this pointer will be seen in the calling program.

That Any

12

Oasis/Module/Topic/LU/Version No/Author

Code to show passing an array to a function


void doubleme(int a[], int size); /*void printme(int a[], int size);*/ int main() { int myarray[10] = {1,2,3,4,5,6,7,8,9,10}; doubleme(myarray, 10); return 0; }

void doubleme(int a[], int size) { int i; for (i = 0; i < size; i++) { a[i] = 2 * a[i]; } }

13

Oasis/Module/Topic/LU/Version No/Author

Two dimensional Arrays

int a [10][20] A two dimensional array 200 int sized locations have been set aside Notation to access the element in ith row & jth col is a[i][j]

In C a 2D array is really a one dimensional array

Elements are stored by rows Each of the arrays element is a one dimensional array.

Passing a 2D array to a function


Parameters declared in the function must include the number of columns The number of rows is irrelevant In C an entire array is not passed in function.

In 2D array we are passing a pointer to an array of rows( where each row is an array of ints or char etc.)
14

Oasis/Module/Topic/LU/Version No/Author

Example 2D Array

int a[2] [3] ={{2,3,4},{5,6,7}}; (Initialising) int (*p) [3]


P is a pointer to an array [3] of ints

p=a; (assigning the address of the first element of the array to p) What is the output?
printf(%d%d\n , (*p)[0], (*p)[1]); p++; printf(%d%d , (*p)[0], (*p)[1]);

15

Oasis/Module/Topic/LU/Version No/Author

Dynamically Allocating a two dimensional Array


columns p

16

Oasis/Module/Topic/LU/Version No/Author

Key Points
Accessing & Manipulating Pointers Modify variables function calling Accessing & manipulating arrays using pointers Passing arrays to functions 2D Arrays
Arrays

17

Oasis/Module/Topic/LU/Version No/Author

Laboratory Assignments

Write a program that prints the index of the smallest and the largest element in an array of 20 integers.If the numbers are repeated also print all the indices.

Write a program that copies the elements of an array into another but in reverse order. Print both the arrays.
Write a program that will merge the contents of two sorted arrays into a single sorted array.The lengths of the input arrays may vary and they may contain duplicate elements but the merged array should not contain duplicate elements. Assume arrays of fixed sizes. Simulate the 2D array as shown using dynamic allocation.

18

Oasis/Module/Topic/LU/Version No/Author

Additional Learning Self Study


C Programming Language Kernighan & Ritchie(PHI) http://c-faq.com/ Frequently asked questions on C http://cslibrary.stanford.edu/101/ The link contains good tutorials on C http://www.le.ac.uk/cc/tutorials/c/ccccfile.html
The

19

Oasis/Module/Topic/LU/Version No/Author

THANK YOU

You might also like