# Pointers and one dimensional arrays

Views:

Category: Entertainment

## Presentation Description

No description available.

## Presentation Transcript

### Pointers and one dimensional arrays :

Pointers and one dimensional arrays int l[]={10,20,30,40,50,60,70,80}; This array will be stored in the contiguous locations of the main memory Each element of this array occupies 2 bytes. Name of the array contains the starting address of the array. Name l contains the address 2001. printf(“\n Address of zeroth element of array= %d”,l);printf(“\n Value of zeroth element of array= %d”,*l); 10 20 30 40 50 60 70 80 2001 2003 2005 2007 2009 2011 2013 2015 l

### Array Indexing Method of printing an array :

Array Indexing Method of printing an array main() { int l[]={10,20,30,40,50}; int i; printf(“the list is…..\n”); for(i=0;i<5;i++) printf(“\n %d \t %d ….element”,l[i],i); getch(); } Output: 0……..element 2……..element 3…….element 40 4…….element 50 5…….element 60 6…….element

### Sample program can also be written by pointer method :

Sample program can also be written by pointer method main() { int l[]={10,20,30,40,50}; int i; printf(“the list is…..\n”); for(i=0;i<5;i++) printf(“\n %d \t %d ….element”,*(l+i),i); getch(); } *(l+i) : since l designates the address of zeroth element of the array *l *(l+0) = l[0] *(l+1)= l[1] and so onSo we can refer the ith element of array by following ways: *(l+i) or *(i+l) or l[i]

### Pointer variable method for printing array :

Pointer variable method for printing array main() { int l[]={10,20,30,40,50}; int *p,i; p=l; printf(“\n the list”); for(i=0;i<5;i++) printf(“\n %d ”, *p++); getch(); } Output: 10 20 30 40 50

### Two-dimensional array :

Two-dimensional array int a[3][4]; where a is name of two dimensional array of 3 rows and 4 columns. 1001 1003 1005 1007 a[0] a[1] 1009 a[2] 1017 Base address of 1st row will be in a or a[0] as 1001 Base address of 2nd row will be in a or a[1] as 1009To access the elements of 2-dim array a[i][j]=*(*(a+i)+j) a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[2][0] a[1][1] a[1][2] a[1][3] a[2][1] a[2][2] a[2][3]

### Initializing Two-dimensional array :

Initializing Two-dimensional array #define MAX_ROW 5 #define MAX_COL 5 int a[MAX_ROW][MAX_COL]={{0,0,0,0,0}, {1,1,1,1,1}, {2,2,2,2,2}, {3,3,3,3,3}, {4,4,4,4,4}, {5,5,5,5,5}};

### Demonstration of pointers and 2-d arrays :

Demonstration of pointers and 2-d arrays main() { int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; int i,j;printf(“\nAddresses of all rows:\n”);for(i=0;i<3;i++) printf(“\n Address of 1-dim array %u”,a[i]); for(i=0;i<3;i++) {for(j=0;j<4;j++) printf(“%d \t”,*(*(a+i)+j)); } getch(); }

### Pointers and Strings :

Pointers and Strings A group of chars of any length is known as string.In C,the strings are stored and manipulated as array of chars with last char being a null character i.e ‘\0’.eg “COMPUTER” can be stored in array.We can declare this string as a normal array by the following declaration char part[9]; C O M P S R E T U 0 1 2 3 4 5 6 7 8 part

### Slide 9:

char *p;p=part;it will assign starting address of string part to the variable pointer p.

### Usage of pointer to string :

Usage of pointer to string main() { char part[]=“COMPUTER”; char *p; p=part; //assign starting address of string part to p/*Print the String */while(*p!=‘\0’){ printf(“%c”,*p); p++;}getch(); }