Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.

Similar presentations


Presentation on theme: "Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference."— Presentation transcript:

1 Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference

2 Basic Concept of Pointers A pointer is a variable which directly points to a memory address. It will allow the programmer to directly manipulate the data in memory. So far, we have seen that a variable is used to store a value. A pointer variable, however, does not store a value but instead store the address of the memory space which contain the value. Why would we want to use pointers? –To call a function by reference so that the data passed to the function can be changed inside the function. –To create a dynamic data structure which can grow larger or smaller as necessary.

3 Memory contentMemory address 0x0000 0x0001 0x0002 0x0003 …... 0xFFFF 0xFFFE 0xFFFD 0xFFFC 27 10 76 99 0x0002 787 878 999 A variable of type int is stored here. The address of this location is 0x0002. This location contains the value 0x0002. Therefore this location stores a pointer variable which points to the address 0x0002.

4 Pointer Declaration General format: data_type *ptr_name; Example: –Pointer to an int: int *intptr; –Pointer to a char: char *charptr; The asterik (*) character in front of the variable name tells that the variable is a pointer and not a variable to store value. To prevent the pointer from pointing to a random memory address, it is advisable that the pointer is initialized to 0 or NULL before being used.

5 Pointer Operator (& and *) When a pointer is created, it is not pointing to any valid memory address. Therefore, we need to assign it to a variable’s address by using the & operator. This operator is called a reference operator. After a pointer is assigned to a particular address, the value in the pointed address can be accessed using the * operator. This operator is called a dereference operator. Look at this example: int num = 9; int *num_ptr; num_ptr = # printf(“num = %d”, *num_ptr); Output: num = 9

6 #include void main(void) { int var = 10; int *ptrvar; ptrvar = &var; printf(“The address of the variable var is: %d\n”, &var); printf(“The value in the pointer ptrvar is: %d\n”, ptrvar); printf(“Both values are the same – address of var\n”); printf(“The value in the variable var is: %d\n”, var); printf(“The value in the address pointed by ptrvar is: %d\n”, *ptrvar); printf(“Both values are the same – the value in var \n”); printf(“The value in the address of var is: %d\n”, *&var); printf(“The value in the address pointed by ptrvar is: %d\n”, *ptrvar); printf(“Both values are the same – the value in the address of var\n”); }

7 Parameter Passing by Reference A function call by reference can be done by passing a pointer to the function argument (the same way as passing a normal variable to the argument). When the value referenced by the pointer is changed inside the function, the value in the actual variable will also change. Therefore, we can pass the result of the function through the function argument without having to use the return statement. In fact, by passing pointers to the function argument, we can return more than one value.

8 When a pointer is passed to a function, we are actually passing the address of a variable to the function. Since we have the address, we can directly manipulate the data in the address. In the case where a non-pointer variable is passed, the function will create another space in memory to hold the value locally while the program is inside the function. Therefore, any change to the variable inside the function will not change the actual value of the variable.

9 #include void Func1(int, int); void Func2(int *, int *); void main(void) { int a = 8, b = 9; printf(“Before Func1 is called, a = %d, b = %d\n”, a, b); Func1(a, b); printf(“After Func1 is called, a = %d, b = %d\n”, a, b); printf(“\nBefore Func2 is called, a = %d, b = %d\n”, a, b); Func2(&a, &b); printf(“After Func2 is called, a = %d, b = %\nd”, a, b); } void Func1(int a, int b) { a = 0; b = 0; printf(“Inside Func1, a = %d, b = %d\n”, a, b); }

10 void Func2(int *pa, int *pb) { *pa = 0; *pb = 0; printf(“Inside Func2, *pa = %d, *pb = %d\n”, *pa, *pb); } Output: Before Func1 is called, a = 8, b = 9 Inside Func1, a = 0, b = 0 After Func1 is called, a = 8, b = 9 Before Func2 is called, a = 8, b = 9 Inside Func2, *pa = 0, *pb = 0 After Func2 is called, a = 0, b = 0


Download ppt "Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference."

Similar presentations


Ads by Google