Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.

Similar presentations


Presentation on theme: "Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6."— Presentation transcript:

1 Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6

2 Pointer  C uses pointers in three different ways:  C uses pointers to create dynamic data structures -- data structures built up from blocks of memory allocated from the heap at run-time.  C uses pointers to handle variable parameters passed to functions.  Pointers in C provide an alternative way to access information stored in arrays. Pointer techniques are especially valuable when you work with strings. There is an intimate link between arrays and pointers in C.

3 Pointer Reference Operator (&)  The memory of your computer can be imagined as a succession of memory cells.  As soon as we declare a variable, the amount of memory needed is assigned for it at a specific location in memory (its memory address).  The address that locates a variable within memory is what we call a reference to that variable.  reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator.  The variable that stores the reference to another variable is what we call a pointer. andy = 25; fred = andy; ted = &andy; 1.we have assigned the value 25 to andy (a variable whose address in memory is 1776). 2.copied to fred the content of variable andy. 3.copies to ted the reference of andy.

4 Pointer Dereference Operator (*)  Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by".  Notice the difference between the reference and dereference operators:  & is the reference operator and can be read as "address of“  * is the dereference operator and can be read as "value pointed by" andy = 25; ted = &andy; beth = *ted; *(&andy) == andy

5 Pointer Declaring Variables of Pointer Types  Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going point to.  Declarations: int *number; char *character; float *greatnumber;

6 Pointer Example 1 // my first pointer #include using namespace std; int main () { int firstvalue, secondvalue; int *mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; printf("firstvalue is &d\n", firstvalue); printf("secondvalue is &d\n", secondvalue); return 0; }

7 Pointer Example 1 (OUTPUT) // my first pointer #include using namespace std; int main () { int firstvalue, secondvalue; int *mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; printf("firstvalue is &d\n", firstvalue); printf("secondvalue is &d\n", secondvalue); return 0; } firstvalue is 10 secondvalue is 20 firstvalue is 10 secondvalue is 20

8 Pointer (Example 2) Function with Output Arguments void /*-this function returns 2 results-*/ calculation(int num, /* input */ int *cal1,/* output */ int *cal2)/* output */ { *cal1 = num + num; *cal2 = num * num; } int main(void) { int value; /* input: number entered by user */ int sum;/* output: num + num */ int multi;/* output: num * num */ printf(“Enter a value to analyze> “); scanf(“%lf”, &value); calculation(value, &sum, &multi); printf(“Sum = %d, Different = %d”, sum, multi); return(0); }

9 Pointer (Example 3)

10


Download ppt "Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6."

Similar presentations


Ads by Google