1 / 10

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

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman. CP 202 Chapter 6. Pointer. C uses pointers in three different ways:

clares
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. PointerReference 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; • we have assigned the value 25 to andy (a variable whose address in memory is 1776). • copied to fred the content of variable andy. • copies to ted the reference of andy.

  4. PointerDereference 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. PointerDeclaring 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. PointerExample 1 // my first pointer #include <iostream> usingnamespace std; int main () { intfirstvalue, 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. PointerExample 1 (OUTPUT) // my first pointer #include <iostream> usingnamespace std; int main () { intfirstvalue, 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

  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. Pointer (Example 3)

More Related