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:
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.
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.
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". andy = 25; ted = &andy; beth = *ted; *(&andy) == andy
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"
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;
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; firstvalue is 10 } secondvalue is 20
Pointer (Example 2) Function with Output Arguments void calculation(int num, int *cal1, int *cal2) { *cal1 = num + num; *cal2 = num * num; } int main(void) { int value; int sum; int multi;
/*-this function returns 2 results-*/ /* input */ /* output */ /* output */
/* input: number entered by user */ /* output: num + num */ /* output: num * num */
printf(“Enter a value to analyze> “); scanf(“%lf”, &value); calculation(value, &sum, &multi); printf(“Sum = %d, Multiply = %d”, sum, multi); return(0);
Pointer (Example 3)