20
Programming in C Pointer Basics Pointer Basics

Programming in C

Embed Size (px)

DESCRIPTION

Pointer Basics. Programming in C. What is a pointer. In a generic sense, a “pointer” is anything that tells us where something can be found. Addresses in the phone book URLs for webpages Road signs. Java Reference. - PowerPoint PPT Presentation

Citation preview

Page 1: Programming in C

Programming in C

Pointer BasicsPointer Basics

Page 2: Programming in C

What is a pointer

• In a generic sense, a “pointer” is anything that In a generic sense, a “pointer” is anything that tells us where something can be found.tells us where something can be found.– Addresses in the phone book– URLs for webpages– Road signs

Page 3: Programming in C

Java Reference

• In Java, the name of an object is a reference to In Java, the name of an object is a reference to that object. Here that object. Here ford is a reference to a Truck is a reference to a Truck object. It contains the memory address at which object. It contains the memory address at which the Truck object is stored. the Truck object is stored. Truck ford = new Truck( );

• The syntax for using the reference is pretty The syntax for using the reference is pretty simple. Just use the “dot” notation.simple. Just use the “dot” notation.ford.start( );

ford.drive( 23 );

ford.turn (LEFT);

Page 4: Programming in C

1/14/10

What is a pointer ?• In C, a pointer variable (or just “pointer”) is In C, a pointer variable (or just “pointer”) is

similar to a reference in Java except thatsimilar to a reference in Java except that– A pointer can contain the memory address of any

variable type (Java references only refer to objects)– A primitive (int, char, float)

– An array

– A struct or union

– Dynamically allocated memory

– Another pointer

– A function

– There’s a lot of syntax required to create and use pointers

Page 5: Programming in C

1/14/10

Why Pointers?

• They allow you to refer to large data structures in a They allow you to refer to large data structures in a compact waycompact way

• They facilitate sharing between different parts of programsThey facilitate sharing between different parts of programs

• They make it possible to get new memory dynamically as They make it possible to get new memory dynamically as your program is runningyour program is running

• They make it easy to represent relationships among data They make it easy to represent relationships among data items. items.

Page 6: Programming in C

1/14/10

Pointer Caution

• They are a powerful low-level device.They are a powerful low-level device.• Undisciplined use can be confusing and thus the Undisciplined use can be confusing and thus the

source of subtle, hard-to-find bugs. source of subtle, hard-to-find bugs. – Program crashes– Memory leaks– Unpredictable results

Page 7: Programming in C

1/14/10

C Pointer Variables

To declare a pointer variable, we must do two thingsTo declare a pointer variable, we must do two things– Use the “*” (star) character to indicate that the variable

being defined is a pointer type.

– Indicate the type of variable to which the pointer will point (the pointee). This is necessary because C provides operations on pointers (e.g., *, ++, etc) whose meaning depends on the type of the pointee.

• General declaration of a pointerGeneral declaration of a pointertype *nameOfPointer;

Page 8: Programming in C

1/14/10

Pointer DeclarationThe declarationThe declaration

int *intPtr; defines the variable defines the variable intPtr to be a pointer to a variable of to be a pointer to a variable of type type int. . intPtr will contain the memory address of some will contain the memory address of some int variable or variable or int array. Read this declaration as array. Read this declaration as– “intPtr is a pointer to an int”, or equivalently

– “*intPtr is an int”

Caution -- Be careful when defining multiple variables on the Caution -- Be careful when defining multiple variables on the same line. In this definitionsame line. In this definition

int *intPtr, intPtr2;

intPtr is a pointer to an int, but intPtr2 is not!

Page 9: Programming in C

1/14/10

Pointer Operators

The two primary operators used with pointers are The two primary operators used with pointers are

* (star) and (star) and && (ampersand) (ampersand)

– The * operator is used to define pointer variables and to deference a pointer. “Dereferencing” a pointer means to use the value of the pointee.

– The & operator gives the address of a variable.Recall the use of & in scanf( )

Page 10: Programming in C

1/14/10

Pointer Examplesint x = 1, y = 2, z[10];

int *ip; /* ip is a pointer to an int */

ip = &x; /* ip points to (contains the memory address of) x */

y = *ip; /* y is now 1, indirectly copied from x using ip */

*ip = 0; /* x is now 0 */

ip = &z[5]; /* ip now points to z[5] */

If ip points to x, then *ip can be used anywhere x can be used so in this example *ip = *ip + 10; and x = x + 10; are equivalent

The * and & operators bind more tightly than arithmetic operators so

y = *ip + 1; takes the value of the variable to which ip points, adds 1 and assigns it to y

Similarly, the statements *ip += 1; and ++*ip; and (*ip)++; all increment the variable to which ip points. (Note that the parenthesis are necessary in the last statement; without them, the expression would increment ip rather than what it points to since operators like * and ++ associate from right to left.)

Page 11: Programming in C

1/14/10

Pointer and Variable types• The type of a pointer and its pointee must matchThe type of a pointer and its pointee must match

int a = 42;

int *ip;

double d = 6.34;

double *dp;

ip = &a; /* ok -- types match */

dp = &d; /* ok */

ip = &d; /* compiler error -- type mismatch */

dp = &a; /* compiler error */

Page 12: Programming in C

1/14/10

More Pointer Code• Use ampersand (Use ampersand ( & ) to obtain the address of the pointee ) to obtain the address of the pointee• Use star ( Use star ( * ) to get / change the value of the pointee ) to get / change the value of the pointee• Use Use %p to print the value of a pointer with to print the value of a pointer with printf( )• What is the output from this code?What is the output from this code?

int a = 1, *ptr1;

/* show value and address of a ** and value of the pointer */ptr1 = &a ;printf("a = %d, &a = %p, ptr1 = %p, *ptr1 = %d\n",

a, &a, ptr1, *ptr1) ;

/* change the value of a by dereferencing ptr1** then print again */*ptr1 = 35 ;printf(“a = %d, &a = %p, ptr1 = %p, *ptr1 = %d\n", a, &a, ptr1, *ptr1) ;

Page 13: Programming in C

1/14/10

NULL• NULL is a special value which may be assigned to a pointerNULL is a special value which may be assigned to a pointer• NULL indicates that this pointer does not point to any NULL indicates that this pointer does not point to any

variable (there is no pointee)variable (there is no pointee)• Often used when pointers are declaredOften used when pointers are declared

int *pInt = NULL;• Often used as the return type of functions that return a Often used as the return type of functions that return a

pointer to indicate function failurepointer to indicate function failureint *myPtr;myPtr = myFunction( );if (myPtr == NULL){

/* something bad happened */}

• Dereferencing a pointer whose value is NULL will result in Dereferencing a pointer whose value is NULL will result in program terminationprogram termination..

Page 14: Programming in C

1/14/10

Pointers and Function Arguments• Since C passes all primitive function arguments “by value” Since C passes all primitive function arguments “by value”

there is no direct way for a function to alter a variable in the there is no direct way for a function to alter a variable in the calling code.calling code.

• This version of the This version of the swap function doesn’t work. function doesn’t work. WHY NOT?WHY NOT?/* calling swap from somewhere in main() */int x = 42, y = 17;Swap( x, y );

/* wrong version of swap */void Swap (int a, int b){

int temp;temp = a;a = b;b = temp;

}

Page 15: Programming in C

1/14/10

A better swap( )• The desired effect can be obtained by passing pointers to The desired effect can be obtained by passing pointers to

the values to be exchanged.the values to be exchanged.• This is a very common use of pointers.This is a very common use of pointers.

/* calling swap from somewhere in main( ) */int x = 42, y = 17;Swap( &x, &y );

/* correct version of swap */void Swap (int *px, int *py){

int temp;temp = *px;*px = *py;*py = temp;

}

Page 16: Programming in C

1/14/10

More Pointer Function Parameters

• Passing the address of variable(s) to a function Passing the address of variable(s) to a function can be used to have a function “return” multiple can be used to have a function “return” multiple values.values.

• The pointer arguments point to variables in the The pointer arguments point to variables in the calling code which are changed (“returned”) by calling code which are changed (“returned”) by the function.the function.

Page 17: Programming in C

1/14/10

ConvertTime.cvoid ConvertTime (int time, int *pHours, int *pMins) {

*pHours = time / 60;*pMins = time % 60;

}

int main( ){

int time, hours, minutes;printf("Enter a time duration in minutes: ");scanf ("%d", &time);ConvertTime (time, &hours, &minutes);printf("HH:MM format: %d:%02d\n", hours, minutes);return 0;

}

Page 18: Programming in C

1/14/10

An Exercise• What is the output from this code?What is the output from this code?void F (int a, int *b) {

a = 7 ; *b = a ; b = &a ;

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

}

int main(){

int m = 3, n = 5;F(m, &n) ;printf("%d, %d\n", m, n) ;return 0;

}

4, 4

3, 7

Page 19: Programming in C

1/14/10

Pointers to struct/* define a struct for related student data */typedef struct student {

char name[50]; char major [20];double gpa;

} STUDENT;

STUDENT bob = {"Bob Smith", "Math", 3.77};STUDENT sally = {"Sally", "CSEE", 4.0};STUDENT *pStudent; /* pStudent is a "pointer to struct student" */

/* make pStudent point to bob */pStudent = &bob;

/* use -> to access the members */ printf ("Bob's name: %s\n", pStudent->name);printf ("Bob's gpa : %f\n", pStudent->gpa);

/* make pStudent point to sally */pStudent = &sally;printf ("Sally's name: %s\n", pStudent->name);printf ("Sally's gpa: %f\n", pStudent->gpa);

Note too that the following are equivalent. Why?? pStudent->gpa and (*pStudent).gpa /* the parentheses are necessary */

Page 20: Programming in C

9/24/10

Pointer to struct for functions

void PrintStudent(STUDENT *studentp)

{

printf(“Name : %s\n”, studentp->name);

printf(“Major: %s\n”, studentp->major);

printf(“GPA : %4.2f”, studentp->gpa);

}

Passing a pointer to a struct to a function is more efficient than passing the struct itself. Why is this true?