Top Banner
C++ Spring 2000 Arrays 1 Pointers in C
25
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Pointers+(2)

C++ Spring 2000 Arrays

1

Pointers in C

Page 2: Pointers+(2)

Pointers

C++ Spring 2000 Arrays

2

A pointer is a variable that holds the address of something else.

...

...

MEMORY

012345

813458134681347

Address

int foo;int *x;

foo = 123;x = &foo;

foo

x

123

3

Page 3: Pointers+(2)

Pointers

C++ Spring 2000 Arrays

3

Example: int *x=i;

x is a pointer to an integer.

“the int x points to”

Page 4: Pointers+(2)

Pointers4

In C you can get the address of a variable with the “&” operator.

...

...

MEMORY

012345

Address

&foo means “the address of foo”

foo

int foo;

foo = 123;x = &foo; 123

Page 5: Pointers+(2)

Assigning a value to a dereferenced pointer

C++ Spring 2000 Arrays

5

A pointer must have a value before you can dereference it (follow the pointer).

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

Page 6: Pointers+(2)

Pointers to anything

C++ Spring 2000 Arrays

6

int *x;

int **y;

double *z;

x some int

some *int some inty

z some double

Page 7: Pointers+(2)

Pointer arithmetic

C++ Spring 2000 Arrays

7

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.

int a[5];

a[0] a[1] a[2] a[3] a[4]

int *ptr = a;

*ptr

*(ptr+2)*(ptr+4)

Page 8: Pointers+(2)

Operations on Pointers

C++ Spring 2000 Arrays

8

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

Page 9: Pointers+(2)

Pointer arithmetic

C++ Spring 2000 Arrays

9

You can use the integer x points to in a C expression like this:

y = *x + 17;

*x = *x +1;

Page 10: Pointers+(2)

Q&A

Q. Are the expression *ptr++ and ++*ptr same?

A.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

10

Page 11: Pointers+(2)

Pointers and Arrays

C++ Spring 2000 Arrays

11

An array name is basically a const pointer.You can use the [] operator with a pointer:

int *x;

int a[10];

x = &a[2];x is “the address of a[2] ”

Page 12: Pointers+(2)

Printing an array using Pointer

C++ Spring 2000 Arrays

12

void print_array(int a[], int len) {for (int i=0;i<len;i++)

cout << "[" << i << "] = " << a[i] << endl;

}

void print_array(int *a, int len) {

for (int i=0;i<len;i++)

cout << "[" << i << "] = "

<< *a++ << endl;

}

pointer version

array version

Page 13: Pointers+(2)

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])); }

A. 10 2 2B. 2 2 2C. 10 10 10D. 2 10 2

Answer: A

C++ Spring 2000 Arrays

13

Page 14: Pointers+(2)

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

14

Page 15: Pointers+(2)

C++ Spring 2000 Arrays

15

Pointers and Functions

Page 16: Pointers+(2)

Call by Value

main( ){

int a = 4;myFunc (a);

}myFunc (int b){

b = b * 2;}

16

Address (2 bytes)65514

6551565516655176551865519655206552165522655236552465525

a 4

b 48

main

myFunc

Page 17: Pointers+(2)

Call by Reference

main( ){

int a = 4;myFunc (&a);

}myFunc (int *b){

*b = *b * 2;}

17Data Word Size 1 byte (assume)Address Word Size

(2 bytes) 655146551565516655176551865519655206552165522655236552465525

a 4 main

myFunc

8

b65520

Page 18: Pointers+(2)

Passing pointers as parameters

C++ Spring 2000 Arrays

18

void swap( int *x, int *y) {

int temp;

temp = *x;

*x = *y;

*y = temp;

}

Page 19: Pointers+(2)

Pointer Parameters

C++ Spring 2000 Arrays

19

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)

Page 20: Pointers+(2)

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("%d",**p);}

Answer: 1

C++ Spring 2000 Arrays

20

Page 21: Pointers+(2)

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);

Page 22: Pointers+(2)

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;

Page 23: Pointers+(2)

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;

Page 24: Pointers+(2)

Pointer as Structure Memberstruct 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));……

Page 25: Pointers+(2)

25