pointers. 1.a) pointer expressions pointer variables can be used in expression if p1 and p2 are...

27
POINTERS

Upload: sandra-patrick

Post on 30-Dec-2015

236 views

Category:

Documents


0 download

TRANSCRIPT

POINTERS

1.a) POINTER EXPRESSIONS• Pointer variables can be used in expression

• If p1 and p2 are properly declared and initialized pointers then following are valid

y = *p1 * *p1; same as (*p1)*(*p2) sum=sum + *p1; z = 5* - *p2/ *p1; same as (5 * (-(*p2)))/(*p1) p1++ -p2 sum += *p2

• Pointers can also be compared using relational operators .

p1 > p2 , p1 == p2 , p1 != p2

• Pointers cannot be divided or multiplied or added p1/p2 or p1*p2 or p1/3 or p1+p2

• Add integer to or subtract integers from pointers are allowed

• Subtraction of one pointer from another of same type is allowed

Important note

• *ptr++ is equivalent to *(ptr++) as ++ has greater operator precedence

expression will increase the value of ptr so that it points to the next element.

• (*ptr)++ will increment the value of the variable whose address is stored in ptr .

1.b) POINTER INCREMENT

• P1++ or p1=p1+1 will cause the pointer to point to the next value of

its type. e.g if p1 value is 2800 then after p1++ it will be 2802. *p1 will give value stored at next

locationThe value by which it is increased is called as scale

factor• Sizeof(variable) – operator returns size of bytes

occupied by that variable

#include<stdio.h>#include<conio.h>int main(){int num, *pnum;pnum=&num;*pnum=10;printf("*pnum= %d", *pnum);printf(" num = %d", num);*pnum= *pnum + 1; //increments the value of numprintf("\n after increment *pnum=%d", *pnum);printf("\n after increment num=%d", num);getch();return 0;}

Find out output.

1.d) POINTERS AND CHARACTER STRINGS

• String without pointer char str[5]=“good”• String with pointer char *str=“good”; pointer str now points to the first character of the string

or char *string1; string1= “good”;string1 is not a string it is pointer which also will work as a

name for the string

• To print string string1 we can use printf or puts (). * not required

• /*---------- STRING HANDLING USING POINTERS --------*/• void main()• { char *name;• int length;• char *cptr;• name = "DELHI";• cptr=name;• printf ("%s\n", name);• while(*cptr != '\0')• { • printf("%c is stored at address %u\n", *cptr, cptr);• cptr++;• }• length = cptr - name;• printf("\nLength of the string = %d\n", length); • getch();• }

Output:-

DELHID is stored at address 170 E is stored at address 171L is stored at address 172H is stored at address 173I is stored at address 174

Length of the string = 5

#include<stdio.h>

int main(){ // Declaring/Initializing three characters pointers char *ptr1 = "Himanshu"; char *ptr2 = "Arora"; char *ptr3 = "TheGeekStuff";

//Declaring an array of 3 char pointers char* arr[3];

// Initializing the array with values arr[0] = ptr1; arr[1] = ptr2; arr[2] = ptr3;

//Printing the values stored in array printf("\n [%s]\n", arr[0]); printf("\n [%s]\n", arr[1]); printf("\n [%s]\n", arr[2]); getchar(); return 0;}

1.c) POINTERS AND 1D- ARRAYS

• Accessing array elements by pointers is always faster than accessing them by subscripts.

e.g int x[5] = { 1,2,3,4,5};

Int *p; We can make pointer p point to an array x by: p = x or p = &x[0]1D , x[i] can be represented by *(x+i) or *(p+i)

• Array notation is a form of pointer notation. The name of the array is the starting address of the array in memory. It is also known as a base address. In other words, base address is the address of the first element in the array or the address of arr[0].

Int *ptr;Ptr = &arr[0];Here, ptr is made to point to the first element of the

array.Ptr++; // will point to the second element of the

array

Impt : Num[i], *(num+i), *(i+num), i[num] -- all refer to same element int main() { int num[ ] = { 24, 34, 12, 44, 56, 17 } ; int i ; for ( i = 0 ; i <= 5 ; i++ ) { printf ( "\naddress = %u ", &num[i] ) ; printf ( "element = %d %d ", num[i], *( num + i ) ) ; printf ( "%d %d", *( i + num ), i[num] ) ; } getch(); return 0;}

This output is on 64-bit pc due to which int is taking 4 byte

int main() { int num[ ] = { 24, 34, 12, 44, 56, 17 } ; int i, *j ; j = &num[0] ; /* assign address of zeroth element */ for ( i = 0 ; i <= 5 ; i++ ) { printf ( "\naddress = %u ", j ) ; printf ( "element = %d", *j ) ; j++ ; /* increment pointer to point to next location */ } getch(); return 0; }

This output is on 64-bit pc due to which int is taking 4 byte

E.g • main() { int b[]={ 10,20,30,40,50}; int i, *k; k=b; for(i=0;i<=4;i++) { Printf(“\n%d”, *k); k++; } }

what will be output ??????

1.d) POINTERS AND 2D ARRAY

• Int b[3][3] = { {1,3,5} , {7,9,11} , {13,15,17}}

b is a pointer to b[0][0] b[0] is also a pointer to b[0][0] b[1] is a pointer to b[1][0] b[2] is a pointer to b[2][0] *b is a pointer to b[0] (special case) *b[1] is the value of b[1][0] which is 7 *b[2]+1 is the value of b[2][0]+1 (which is 14)

• Int b[10]; we can write b[0] as *(b+0) or *b• Int a[0][0] can also be written as *(a[0]+0) or

*(*a+0) or **a• S[2][1] *(s[2]+1) *(*(s+2)+1) all refer to same element

Fig . memory map of a 2D array

OUTPUT

Exercise : find out the output for the program and explain working of program

1.e) POINTERS AND 3D ARRAY

Arr[2][3][1] is written as

*(*(*arr+2)+3)+1)

1.f) ARRAY OF POINTERS

• Like array of ints or an array of floats , we have array of pointers

• Array of pointers is a collection of addresses.• The addresses present in the array of pointers

can be : a) addresses of isolated variables b) addresses of array element c) or any other addresses

Example 1 : Array of pointer

Example 2 : Array of pointers // run this code on software

Output ????????? Find by yourself

Questions) using pointers

• WAP to find largest number in an array using pointer

• WAP to find smallest number in an array using pointer

• WAP to exchange the values of largest with the smallest number of an array

• WAP to find the sum of all elements of an array • WAP to read and display the values of 3*3 matrix• WAP to read and display the values of 3*3*3 matrix

1.f) POINTER AND STRUCTURE

• Pointer can also point to a structure . Such pointers are called as ‘structure pointers’

• Structure variables can be accessed by ‘->’ operator or dot (.) operator

• Pointer_var->structure_var (*pointer_var).structure_var

Example : structure pointer