Docsity

Understanding Pointers in C: Usage, Declaration, and Manipulation, Slides of Computer Programming

Partial preview of the text

Download Understanding Pointers in C: Usage, Declaration, and Manipulation and more Slides Computer Programming in PDF only on Docsity!

Dr. Hanif Durad^

Lecture Outline  Pointers  Pointers as Function Parameters  Arrays as Function Parameters  Troubles with pointers

Pointers  Pointers are variables that contain memoryaddresses as their values 1. int p;2. int i=1,j=2;3. p=&i;4. printf("p=%d\n",p); / p=1 /5. p=&j;6. printf("p=%d\n",p); /* p=2 /7. j++;8. Printf(“p=%d\n”,p); /* *p=3 */

D:\C\CSCI-1190\ lecture-4.ppt, P-

Address of a variable (1/2)  Each variable that is declared is stored in memory. Sincememory is indexed each variable has an address.  C allows programmers to access the address of thevariables.  Unary operator & (read as ‘address of’) gives the addressof the operand variable.  See example.  Since constants, expressions do not have permanentstorage, they donot have address.  &1.0, &(d+6)

are illegal.

^ See printf format.

Dr. Hanif Durad^

D:\C Language\NMP\lecture5.ppt

Address of a variable (2/2)^ #include^ <stdio.h>#define^ line^ "\t-----------------\n"main(){ int^ i^ =^ 0;char^ c^ =^ 'a';short^ s^ =^ 1;long^ l^ =^ 2;float^ f^ =^ 3.0;double^ d^ =^ 4.0;printf("Variable

Table\n");printf(line);printf("\tVar\t\tAddress\t\tValue\n");printf(line);printf("\t%s\t\t%p\t%d\n","i",^ &i,

i); printf("\t%s\t\t%p\t%c\n","c",

&c,^ c); printf("\t%s\t\t%p\t%d\n","s",

&s,^ s); printf("\t%s\t\t%p\t%d\n","l",

&l,^ l); printf("\t%s\t\t%p\t%f\n","f",

&f,^ f); printf("\t%s\t\t%p\t%f\n","d",

&d,^ d); printf(line);}

Dr. Hanif Durad^

adrr_of_var.c

Pointer to a variable (2/5)  The simplest operation is to get an address of a variableand store it in a pointer variable.  Example p = &k;p = &f;  Pointer p is declared to store an address of an intvariable.  Assigning an address of a float variable to p, wouldcause a compiler warning or error. (Continuing in spite ofwarning, may result in disaster).

Dr. Hanif Durad^

Pointer to a variable (3/5)  The value of pointer p is an address of some int variable.  The value of the int variable pointed to by p can be accessedby using a dereferencing operator *.  Printf statement here prints the value of p and that of *p. p = 0xbffffc8c

*p = 2

^ *p is just like k here. All expressions where k can appear, *palso can. j = 7^

*p = 2^ Dr. Hanif Durad^

Pointer to a variable (5/5) #include^ <stdio.h>#define^ line^ "\t----------------\n"main(){ int i^ =^ 0;int j^ =^ 1;int k^ =^ 2;float^ f^ =^ 3.0;int p^ =^ 0; p = &k;printf("p^ =^ %p\t\tp^ =^

%d\n",^ p,^ *p); j^ =^ p^ +^ 5;printf(“j^ =^ %d\t\tp

=^ %d\n",^ j,^ *p); *p^ =^ p^ +^ i;printf("p^ =^ %p\t\tp

=^ %d\n",^ p,^ p); p^ =^ &p;printf("p^ =^ %p\t\tp

=^ %d\n",^ p,^ *p); }

Dr. Hanif Durad^

Pointers as Function Parameters void increment1(int a){a++;} void increment2(int* p){(p)++;} int main(){int i=1;increment1(i);printf("i=%d\n",i); / i=1, not 2 /increment2(&i);printf("i=%d\n",i); / i=2 */}

point_func.c

Troubles with pointers (1/2)#include <stdio.h>void f(int *aa, int bb) {bb = 8;aa[1] = bb[2];aa = bb;} main() {int a[5] = { 1, 2, 3, 4, 5 }, *b;b = a + 2;f(a,b);printf("%d %d %d %d %d\n",a[0], a[1], a[2], a[3], a[4]);}

Dr. Hanif Durad^

What will be printed by following code?

ptr_tr1.c

D:\C\Hanif Search\C.tn.pdf

Troubles with pointers (2/2)#include <stdio.h>void g(int aa, int bb) {bb[2] = aa[-2];aa++ = 17;++aa = 10;} main() {int blap[7] = { 1, 2, 3, 4, 5, 6, 7 };int *c = blap + 3;g(c,blap);printf("%d %d %d %d %d %d %d\n",blap[0], blap[1], blap[2], blap[3],blap[4], blap[5], blap[6]);}

Dr. Hanif Durad^

What will be printed by following code?

ptr_tr2.c

D:\C\Hanif Search\C.tn.pdf