Lesson 17: Pointers in C


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



After three days of waiting, C++ Maniac brings you another interesting lesson. This one is labeled no. 17, and I think moment has come when I can proudly say we have crossed a half-way of my complete C Tutorial; at least first part of it, “C Programming In General”. This Lesson is about Pointers and their useful implementation in Your future C programs. Let’s start…



Important:

Examples are translated using Microsoft Visual C++ compiler.



Example:

What will be printed after execution of a following program block?

int a = 5; //a address:1245052, value:5


int *b; //a address:1245052, value:5
//b address:1245048, value:?


b = &a; //a address:1245052, value:5
//b address:1245048, value:1245052

*b = 6; //a address:1245052, value:6
//b address:1245048, value:1245052


printf("a = %d *b = %d\n", a, *b);
printf("a = %d b = %ld\n", a, b);
printf("&a = %ld &b = %ld\n", &a, &b);



Result:

a = 6 *b = 6
a = 6 b = 1245052
&a = 1245052 &b = 1245048



Important:

Value that is pointed by variable b is on the same address (b = &a) as the value assigned to variable a.

What would happen if we left out this line: b = &a; ?



Result:

Before assigning value to it, pointer b points to undefined address. This makes it possible for program to store value 6 to an address previously reserved for some other variable or code. This would result in an unexpected behavior or cause an error in program’s execution (because of unauthorized access to memory’s section).



Typical Mistakes:

scanf(“%d", n);
printf(“%d", &n);



Connection between Pointers and Arrays:


Any declared array’s name can also be used as a pointer. Any pointer can also be used as an array.


What will be printed as a result of following program block?

int x[] = {0, 1, 2}; //x[0] address:1245044, value:0
//x[1] address:1245048, value:1
//x[2] address:1245052, value:2


printf("&x[0] = %ld x[0] = %d\n", &x[0], x[0]); // 1
printf("&x[1] = %ld x[1] = %d\n", &x[1], x[1]); // 2
printf("&x[2] = %ld x[2] = %d\n", &x[2], x[2]); // 3




Result:

&x[0] = 1245044 x[0] = 0
&x[1] = 1245048 x[1] = 1
&x[2] = 1245052 x[2] = 2



Important:

Printings in an upper block could’ve been substituted with following lines:

printf("x = %ld *x = %d\n", x, *x);                   // 1
printf("(x + 1) = %ld *(x+1) = %d\n", x + 1, *(x+1)); // 2
printf("(x + 2) = %ld *(x+2) = %d\n", x + 2, *(x+2)); // 3


because these following lines are equivalent:

*x      <=>  x[0]     x    <=>  &x[0]
*(x+1) <=> x[1] x+1 <=> &x[1]
*(x+2) <=> x[2] x+2 <=> &x[2]




Array’s name (in upper example: x) represents pointer to null member of an array. Notation x + 1 represents pointer to first member of an array, whose distance from null member is: 1 * sizeof(int). In a same way, x + 2 represents pointer to second member of an array, whose distance from null member is:
2 * sizeof(int)
.




Array’s Name as Pointer’s Constant:


When compiling this program block:

int a[10], b[10];
a = b;

error is reported. Array’s name (a) is representing pointer’s constant in this example, and because of that we aren’t able to modify this pointer’s value.





Connection between Pointers and Array’s:


Assigning array’s address to pointer (which is equivalent to assigning address of first array’s element to pointer) can be executed in two ways:

int *p, a[10];
p = a; // first solution
p = &a[0]; // second solution




Pointer’s Arithmetic:


What will be printed after execution of a following C program block?

int x[] = {1, 2, 3, 4};   //x[0] address:1245040, value:1
//x[1] address:1245044, value:2
//x[2] address:1245048, value:3
//x[3] address:1245052, value:4



int *p = &x[2]; //p address:1245036, value:1245048


int *q = &x[1]; //q address:1245032, value:1245044


int *r = ++q;
//r address:1245028, value:1245048
//q address:1245032, value:1245048



printf("(p + 1) = %d *(p + 1) = %d\n", (p + 1), *(p + 1));
printf("(p - 1) = %d *(p - 1) = %d\n", (p - 1), *(p - 1));
printf("q = %d *q = %d\n", q, *q);
printf("r = %d *r = %d\n", r, *r);



Result:

(p + 1) = 1245052 *(p + 1) = 4
(p - 1) = 1245044 *(p - 1) = 2
q = 1245048 *q = 3
r = 1245048 *r = 3






Technorati Tags:
, , , , , ,



1 Responses to “Lesson 17: Pointers in C”

  1. Anonymous Anonymous 

    Hi,

    > x + 1 represents pointer to first member of an array, whose distance from null member is: 1 * sizeof(int)

    I think it would look more logical if you put 1 * sizeof(x) instead of int explicitly, as this remains valid for other integral type arrays (char, short etc.) and non-integral arrays as well.

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


German Flag Spanish Flag French Flag Italian Flag Portuguese Flag Japanese Flag Korean Flag Chinese Flag British Flag


This Website is optimized for Firefox. Users browsing with Internet Explorer may encounter problems while viewing pages.


C++ Maniac



Learn C



Additional



#include



Learn Converting



Appendix


Links


Previous posts




Daily Lessons for programming in Visual Studio, using C code.