Sei sulla pagina 1di 25

Exercise 1

Declare variables x and y as integer variable and p and q as pointer to integer. Set x to 2, y to 8, p to the address of x, and q to the address of y. Then print the following information:
1. 2. 3. 4. 5. 6. The address of x and the value of x. The value of p and the value of *p. The address of y and the value of y. The value of q and the value of *q. The address of p(not its contents!). The address of q(not its contents!).

Exercise 2
Declare three integer variables x, y and z with distinct values. Also declare three integer pointer p, q and r. Set p, q, r to the addresses of x, y, z respectively.
1. Print with labels the values of x, y, z, p, q, r, *p,*q,*r. 2. Print the message: Swapping pointers. 3. Execute the swap code: r = p; p = q; q = r; 4. Print with labels the values of x, y, z, p, q, r, *p,*q,*r.

Common Programming Errors


1. ptr1 = ptr1 + &50;
2. ptr1 = ptr1 + &( x + 10 );

3. int num[10];
int *ptr; ptr = # 4. int *p = 50;

Find out..
If m, n are integers and p1, p2 are pointers to integers. Find out errors if any. 1. p1 = &m 2. p2 = n 3. *p1 = &n

Pointers and arrays


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

Starting address of the array

Offset

Address of Grade[3]

Pointers and arrays


g_ptr
Address of Grades[0] Grades[0] Grades[1] Grades[2] Grades[3] Grades[4]

*(g_ptr + 3)

Elements Value

x[0]
1

x[1]
2

x[2]
3

x[3]
4

x[4]
5

Address

1000 1002 1004 1006 1008


p = &x[0] => 1000

Base address p

p+1= &x[1] => 1002


p+2= &x[2] => 1004

p = &x[0];

p+3= &x[3] => 1006


p+4= &x[4] => 1008

Pointer Arithmetic
*ptr++ : use the pointer and then increment it *++ptr : increment the pointer before using it *ptr-- : use the pointer and then decrement it *--ptr : decrement the pointer before using it

Important note
Following notations are same
num[x] *(num+x) *(x+num) x[num]

Associativity Right to Left

Note the difference *(num+x) and *num+x ++*num and *++num

Example
int a[] = {10, 20, 30, 40}; int *p = a; 1. printf (%d, *p); 2. printf(%d, *p+1); 3. printf(%d, *(p+1)); 4. printf(%d, (*p)++); 5. printf(%d, ++(*p)); 6. printf(%d, ++*p); 7. printf(%d, p[1]); 8. printf(%d, 1[p]);

9. printf(%d, *++p); 10. printf(%d, *p++);

Pointers and Two-Dimensional Arrays


Two dimensional array is a collection of single dimensional array

Example

2000 X

2002 2004

2006

2008

200A

ROW 0

ROW 1

ROW 2

Example
2000 X 2002 2004 2006 2008 200A

ROW 0 Answer the following:

ROW 1

ROW 2

1. What is the address of individual elements? 2. What is the starting address of each row? 3. Is it possible to access individual elements of row using row address?

Example
1. What is the address of individual elements? 2000 2002 2004 2006 2008 200A

ROW 0 Element 0:

ROW 1 &x[0][0]

ROW 2

Element 1:
Element 2: Element 3: Element 4: Element 5:

&x[0][1]
&x[1][0] &x[1][1] &x[2][0] &x[2][1]

Example
What is the starting address of each row? 2000 2002 2004 2006 2008 X 200A

ROW 0

ROW 1

ROW 2

Row 0: &x[0][0] = x[0]

Row 1: &x[1][0] = x[1]


Row 2: &x[2][0] = x[2]

Example
Is it possible to access individual elements of row using row address? 2000 2002 2004 2006 2008 200A X

ROW 0

ROW 1

ROW 2

*(Row address + column number) For Example: Element 6: *(x[2]+1)

Example
Use of pointers to access individual elements X 2000 2002 2004 2006 2008 200A

ROW 0

ROW 1

ROW 2

Conclusion
A pointer variable can be assigned the address of another variable A pointer variable can be assigned the values of another pointer variable A pointer variable can be initialized with NULL or Zero value A pointer variable can be prefixed or post fixed with increment and decrement operators.

Conclusion
An integer value may be added or subtracted from a pointer variable When two pointers point to the same array, one pointer variable can be subtracted from another. When two pointers point to the objects of the same data types, they can be compared using relational operator. A pointer variable cannot be multiplied by a constant

Conclusion
Two pointer variables cannot be added A value (arbitrary address )cannot be assigned to a pointer variable

Pointers and One dimensional arrays


Problem Statement: Write a program to find maximum number in an array

Potrebbero piacerti anche