Pointers in C C++ Spring 2000  Arrays
Pointers A pointer is a variable that holds the  address  of something else. C++ Spring 2000  Arrays int foo; int *x; foo = 123; x = &foo; foo x 123 3 ... ... MEMORY 0 1 2 3 4 5 81345 81346 81347 Address
Pointers Example: int *x=i; x is a pointer to an integer. C++ Spring 2000  Arrays “ the int x points to”
Pointers In C you can get the  address  of a variable with the “&” operator. &foo   means “the address of foo” foo int foo; foo = 123; x = &foo; 123 ... ... MEMORY 0 1 2 3 4 5 Address
Assigning a value to a  dereferenced  pointer A pointer must have a value before you can  dereference  it (follow the pointer). C++ Spring 2000  Arrays int *x; *x=3; int foo; int *x; x = &foo; *x=3; ERROR!!! x doesn’t point to anything!!! this is fine x points to foo
Pointers to anything int *x; int **y; double *z; C++ Spring 2000  Arrays x some  int some  *int some  int y z some  double
Pointer arithmetic Integer math operations can be used with pointers. If you increment a pointer, it will be increased by the size of whatever it points to. C++ Spring 2000  Arrays int a[5]; a[0]  a[1]  a[2]  a[3]  a[4] int *ptr = a; *ptr *(ptr+2) *(ptr+4)
Operations on Pointers Can be done,  Valid Operations Addition/Subtraction of a number to/from pointer Subtraction of two pointers Comparison of two pointers Assignment of a pointer to another pointer Can NOT be done,  Invalid Operations Addition of two pointers Multiplication/Division of a pointer with a number C++ Spring 2000  Arrays
Pointer arithmetic You can use the integer x points to in a C expression like this: y = *x + 17; *x = *x +1; C++ Spring 2000  Arrays
Q&A Q. Are the expression *ptr++ and ++*ptr same? No. *ptr++ increment the pointer and not the value pointed by it, whereas ++*ptr increments the value pointed to by ptr. Q. Can you write any another expression which does the same job as ++*ptr? A. (*ptr)++ C++ Spring 2000  Arrays
Pointers and Arrays An array name is basically a  const  pointer. You can use the [] operator with a pointer: int *x; int a[10]; x = &a[2]; C++ Spring 2000  Arrays x  is “the address of  a[2]  ”
Printing an array  using Pointer void print_array(int a[], int len) { for (int i=0;i<len;i++)  cout << &quot;[&quot; << i << &quot;] = &quot;  << a[i] << endl; } C++ Spring 2000  Arrays void print_array(int  *a , int len) { for (int i=0;i<len;i++)  cout << &quot;[&quot; << i << &quot;] = &quot;  <<  *a++  << endl; } pointer version array version
Q&A What would be the output of the following? main() { int arr[]={12,13,14,15,16}; printf(“%d %d” %d”, sizeof(arr),sizeof(*arr),sizeof(arr[0])); } 10 2 2 2  2  2 10 10 10 2  10  2 Answer: A C++ Spring 2000  Arrays
Q&A Q. What would be equivalent expression for referring the same element as a[i][j][k][l]? A. *(*(*(*(a+i)+j)+k)+l) C++ Spring 2000  Arrays
Pointers and Functions C++ Spring 2000  Arrays
Call by Value main( ) {  int a = 4; myFunc (a); } myFunc (int b) { b = b * 2; } Address (2 bytes) 65514 65515 65516 65517 65518 65519 65520 65521 65522 65523 65524 65525 a 4 b 4 8 main myFunc
Call by Reference main( ) {  int a = 4; myFunc (&a); } myFunc (int *b) { *b = *b * 2; } Data Word Size  1 byte (assume) Address Word Size (2 bytes) 65514 65515 65516 65517 65518 65519 65520 65521 65522 65523 65524 65525 a 4 main myFunc 8 b 65520
Passing pointers as parameters void swap( int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } C++ Spring 2000  Arrays
Pointer Parameters Pointers are passed by value (the value of a pointer is the address it holds). If we change what the pointer points to the caller will see the change. If we change the pointer itself, the caller won't see the change (we get a copy of the pointer) C++ Spring 2000  Arrays
Q&A main() { int a[3][4]={ 1,2,3,4, 5,6,7,8, 7,8,9,0 }; int *ptr; ptr=&a[0][0]; func(&ptr); } void func(int **p) { printf(&quot;%d&quot;,**p);} Answer: 1 C++ Spring 2000  Arrays
Pointers and String Declared and initialized as:   char str[6]=“c prog”;    // compiler insert null ‘\0’ char    at the  end Or    char *str=“c prog”;  //stores address in the pointer variable Or   char *str;   str=“hello”;   //Not a String copy  Display  the content as:   printf(“%s”,str); Or  puts(str);
Pointers and String Length of string  char name[20]; char *str; str=name; while(ptr!=‘\0 ’)// condition true until end of the string reached, then ptr holds the address of null character. So, int length=ptr-str;
Pointers to Structures struct part { float price ; char name [10] ; } ; struct part *p , thing; p = &thing; /* The following three statements are equivalent */ thing.price = 50; (*p).price = 50;  /* () around *p is needed */ p -> price = 50;
Pointer as Structure Member struct node{ int data; struct node *next; }; struct node a,b,c; a.next = &b; b.next = &c; c.next = NULL; NULL a b c a.data = 1; a.next->data = 2; /* b.data =2 */ a.next->next->data = 3; /* c.data = 3 */ c.next = (struct node *) malloc(sizeof(struct node)); ……
Pointers+(2)

Pointers+(2)

  • 1.
    Pointers in CC++ Spring 2000 Arrays
  • 2.
    Pointers A pointeris a variable that holds the address of something else. C++ Spring 2000 Arrays int foo; int *x; foo = 123; x = &foo; foo x 123 3 ... ... MEMORY 0 1 2 3 4 5 81345 81346 81347 Address
  • 3.
    Pointers Example: int*x=i; x is a pointer to an integer. C++ Spring 2000 Arrays “ the int x points to”
  • 4.
    Pointers In Cyou can get the address of a variable with the “&” operator. &foo means “the address of foo” foo int foo; foo = 123; x = &foo; 123 ... ... MEMORY 0 1 2 3 4 5 Address
  • 5.
    Assigning a valueto a dereferenced pointer A pointer must have a value before you can dereference it (follow the pointer). C++ Spring 2000 Arrays int *x; *x=3; int foo; int *x; x = &foo; *x=3; ERROR!!! x doesn’t point to anything!!! this is fine x points to foo
  • 6.
    Pointers to anythingint *x; int **y; double *z; C++ Spring 2000 Arrays x some int some *int some int y z some double
  • 7.
    Pointer arithmetic Integermath operations can be used with pointers. If you increment a pointer, it will be increased by the size of whatever it points to. C++ Spring 2000 Arrays int a[5]; a[0] a[1] a[2] a[3] a[4] int *ptr = a; *ptr *(ptr+2) *(ptr+4)
  • 8.
    Operations on PointersCan be done, Valid Operations Addition/Subtraction of a number to/from pointer Subtraction of two pointers Comparison of two pointers Assignment of a pointer to another pointer Can NOT be done, Invalid Operations Addition of two pointers Multiplication/Division of a pointer with a number C++ Spring 2000 Arrays
  • 9.
    Pointer arithmetic Youcan use the integer x points to in a C expression like this: y = *x + 17; *x = *x +1; C++ Spring 2000 Arrays
  • 10.
    Q&A Q. Arethe expression *ptr++ and ++*ptr same? No. *ptr++ increment the pointer and not the value pointed by it, whereas ++*ptr increments the value pointed to by ptr. Q. Can you write any another expression which does the same job as ++*ptr? A. (*ptr)++ C++ Spring 2000 Arrays
  • 11.
    Pointers and ArraysAn array name is basically a const pointer. You can use the [] operator with a pointer: int *x; int a[10]; x = &a[2]; C++ Spring 2000 Arrays x is “the address of a[2] ”
  • 12.
    Printing an array using Pointer void print_array(int a[], int len) { for (int i=0;i<len;i++) cout << &quot;[&quot; << i << &quot;] = &quot; << a[i] << endl; } C++ Spring 2000 Arrays void print_array(int *a , int len) { for (int i=0;i<len;i++) cout << &quot;[&quot; << i << &quot;] = &quot; << *a++ << endl; } pointer version array version
  • 13.
    Q&A What wouldbe the output of the following? main() { int arr[]={12,13,14,15,16}; printf(“%d %d” %d”, sizeof(arr),sizeof(*arr),sizeof(arr[0])); } 10 2 2 2 2 2 10 10 10 2 10 2 Answer: A C++ Spring 2000 Arrays
  • 14.
    Q&A Q. Whatwould be equivalent expression for referring the same element as a[i][j][k][l]? A. *(*(*(*(a+i)+j)+k)+l) C++ Spring 2000 Arrays
  • 15.
    Pointers and FunctionsC++ Spring 2000 Arrays
  • 16.
    Call by Valuemain( ) { int a = 4; myFunc (a); } myFunc (int b) { b = b * 2; } Address (2 bytes) 65514 65515 65516 65517 65518 65519 65520 65521 65522 65523 65524 65525 a 4 b 4 8 main myFunc
  • 17.
    Call by Referencemain( ) { int a = 4; myFunc (&a); } myFunc (int *b) { *b = *b * 2; } Data Word Size 1 byte (assume) Address Word Size (2 bytes) 65514 65515 65516 65517 65518 65519 65520 65521 65522 65523 65524 65525 a 4 main myFunc 8 b 65520
  • 18.
    Passing pointers asparameters void swap( int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } C++ Spring 2000 Arrays
  • 19.
    Pointer Parameters Pointersare passed by value (the value of a pointer is the address it holds). If we change what the pointer points to the caller will see the change. If we change the pointer itself, the caller won't see the change (we get a copy of the pointer) C++ Spring 2000 Arrays
  • 20.
    Q&A main() {int a[3][4]={ 1,2,3,4, 5,6,7,8, 7,8,9,0 }; int *ptr; ptr=&a[0][0]; func(&ptr); } void func(int **p) { printf(&quot;%d&quot;,**p);} Answer: 1 C++ Spring 2000 Arrays
  • 21.
    Pointers and StringDeclared and initialized as: char str[6]=“c prog”; // compiler insert null ‘\0’ char at the end Or char *str=“c prog”; //stores address in the pointer variable Or char *str; str=“hello”; //Not a String copy Display the content as: printf(“%s”,str); Or puts(str);
  • 22.
    Pointers and StringLength of string char name[20]; char *str; str=name; while(ptr!=‘\0 ’)// condition true until end of the string reached, then ptr holds the address of null character. So, int length=ptr-str;
  • 23.
    Pointers to Structuresstruct part { float price ; char name [10] ; } ; struct part *p , thing; p = &thing; /* The following three statements are equivalent */ thing.price = 50; (*p).price = 50; /* () around *p is needed */ p -> price = 50;
  • 24.
    Pointer as StructureMember struct node{ int data; struct node *next; }; struct node a,b,c; a.next = &b; b.next = &c; c.next = NULL; NULL a b c a.data = 1; a.next->data = 2; /* b.data =2 */ a.next->next->data = 3; /* c.data = 3 */ c.next = (struct node *) malloc(sizeof(struct node)); ……