Pointers in C/C++
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Pointers in C/C++

on

  • 1,642 views

 

Statistics

Views

Total Views
1,642
Views on SlideShare
1,620
Embed Views
22

Actions

Likes
1
Downloads
87
Comments
0

1 Embed 22

http://www.techgig.com 22

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Pointers in C/C++ Presentation Transcript

  • 1. Chapter 7: PointersIn this chapter you will learn about, Basic concept of pointers Pointer variables Pointer declaration Pointer definition and initialization Pointer operator (& and *) Parameter passing by referencePrinciples of Programming - NI2005 1
  • 2. What is a pointer So far, we have seen that a variable is used to store a value. Variables allow the programmer to directly manipulate the data in memory. A pointer variable, however, does not store a value but store the address of the memory space which contain the value i.e. it directly points to a specific memory address. 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.Principles of Programming - NI2005 2
  • 3. Variable declaration A variable declaration such as, int number = 20; causes the compiler to allocate a memory location for the variable number and store in it the integer value 20. This absolute address of the memory location is readily available to our program during the run time. The computer uses this address to access its content. number number directly references a 20 variable whose value is 7 11001100Principles of Programming - NI2005 3
  • 4. Pointer declaration General Format: data_type *pointer_name; A pointer declaration such as, int *numberPtr; declares numberptr as a variable that points to an integer variable. Its content is a memory address. The * indicates that the variable being declared is a pointer variable instead of a normal variable.Principles of Programming - NI2005 4
  • 5. Pointer declaration cont… Consider the following declaration int *numberPtr, number = 20; In this case, two memory address have been reserved in the memory, namely the numberPtr and number. The value in variable number is of type integer, and the value in variable numberPtr is an address for another memory. *numberPtr number 20 11111111 11001100Principles of Programming - NI2005 5
  • 6. Pointer Initialization To prevent the pointer from pointing to a random memory address, it is advisable that the pointer is initialized to 0, NULL or an address before being used. A pointer with the value NULL, points to nothing. Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL is preferred.Principles of Programming - NI2005 6
  • 7. 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. Look at this example: int number = 20; int *numberPtr; Output: numberPtr = &number; number = 20 printf(“number = %d”, *numberPtr); The statement numberPtr = &number assigns the address of the variable number to a pointer variable numberPtr. Variable numberPtr is then said as to “point to” variable number.Principles of Programming - NI2005 7
  • 8. Graphical representation int *numberPtr, number = 20; *numberPtr number 20 11111111 11001100 numberPtr = &number; *numberPtr number 11001100 20 11111111 11001100Principles of Programming - NI2005 8
  • 9. Pointer Operator (& and *) cont… After a pointer is assigned to a particular address, the value in the pointed address can be accessed/modified using the * operator. This operator is commonly called as the indirection operator or dereferencing operator. The * operator returns the value of the object to which its operand points. For example, the statement printf(“number = %d”, *numberPtr); prints the value of variable number, namely as 20. Using * in this manner is called dereferencing operator.Principles of Programming - NI2005 9
  • 10. Example: & and *#include <stdio.h>void main(void){ int var = 10; int *ptrvar = &var; printf(“The address of the variable var is: %dn”, &var); printf(“The value of the pointer ptrvar is: %dn”, ptrvar); printf(“Both values are the samen”); printf(“The value of the variable var is: %dn”, var); printf(“The value of *ptrvar is: %dn”, *ptrvar); printf(“Both values are the samen”); printf(“The address of the value pointed by ptrvar is: %dn”, &*ptrvar); printf(“The value inside the address of ptrvar is: %dn”, *&ptrvar); printf(“Both values are the samen”);} Principles of Programming - NI2005 10
  • 11. Example: & and */*Sample Output */The address of the variable var is: 1245052The value of the pointer ptrvar is: 1245052Both values are the sameThe value of the variable var is: 10The value of *ptrvar is: 10Both values are the sameThe address of the value pointed by ptrvar is: 1245052The value inside the address of ptrvar is: 1245052Both values are the samePress any key to continuePrinciples of Programming - NI2005 11
  • 12. &* and *& & and * are inverse operations. &* acts equivalent to *& and this leads back to the original value. Example: (Assume that the address of num is 1245052)#include <stdio.h>void main(void) Output:{ 1245052 int num = 5; 1245052 int *numPtr = &num; 1245052 printf("%d n", numPtr); printf("%d n", &*numPtr); printf("%d n", *&numPtr);}Principles of Programming - NI2005 12
  • 13. Parameter Passing by Reference/Pointer A function may return multiple values by declaring their formal parameters (passing value) as pointers variables. This way of passing the argument is known as call by reference 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.Principles of Programming - NI2005 13
  • 14. Parameter Passing by Reference/Pointer 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.Principles of Programming - NI2005 14
  • 15. Parameter Passing by Reference/Pointer To pass the value of variable by pointers between two functions, we must do the following: Declare the variable that is meant to return a value to the calling function as a pointer variable in the formal parameter list of the function. void function_name(int *varPtr); When to call the function, use a variable together with address operator (&) function_name(&var);Principles of Programming - NI2005 15
  • 16. Parameter Passing by Reference/Pointer Note : the effect of the previous two actions is the same as the effect of the declarations int var; int *varPtr=&var; Declare the function with pointer parameter appropriately in a function prototype by using symbol * as a prefix for pointer parameters. void function_name(int *);Principles of Programming - NI2005 16
  • 17. Example#include <stdio.h>void Func1(int, int);void Func2(int *, int *);void main( ){ int a = 8, b = 9; printf(“Before Func1 is called, a = %d, b = %dn”, a, b); Func1(a, b); printf(“After Func1 is called, a = %d, b = %dn”, a, b); printf(“nBefore Func2 is called, a = %d, b = %dn”, a, b); Func2(&a, &b); printf(“After Func2 is called, a = %d, b = %dn”, a, b);}Principles of Programming - NI2005 17
  • 18. Examplevoid Func1(int a, int b){ a = 0; b = 0; printf(“The value inside Func1, a = %d, b = %dn”, a, b);}void Func2(int *pa, int *pb){ *pa = 0; *pb = 0; printf("The value inside Func2, *pa = %d, *pb = %dnn", *pa, *pb);} Principles of Programming - NI2005 18
  • 19. Result/* output */Before Func1 is called, a = 8, b = 9The value inside Func1, a = 0, b = 0After Func1 is called, a = 8, b = 9Before Func2 is called, a = 8, b = 9The value inside Func2, *pa = 0, *pb = 0After Func2 is called, a = 0, b = 0Press any key to continuePrinciples of Programming - NI2005 19
  • 20. Summary In this chapter, you have learnt The pointer operator: & and * Pointer declaration and assignment Parameter passing by reference The 3 steps needed to pass the value of variable by pointers between two functions. Declare the variable as a pointer variable in formal parameter list of the function. Use a variable together with address operator (&) when to call a function. Using symbol * in function prototype to declare any pointer parameter.Principles of Programming - NI2005 20