Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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
Offset
Address of Grade[3]
*(g_ptr + 3)
Elements Value
x[0]
1
x[1]
2
x[2]
3
x[3]
4
x[4]
5
Address
Base address p
p = &x[0];
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]
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]);
Example
2000 X
2002 2004
2006
2008
200A
ROW 0
ROW 1
ROW 2
Example
2000 X 2002 2004 2006 2008 200A
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
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
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