Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# 09 1 Pointers

1,871
views

Published on

Published in: Technology, News & Politics

1 Comment
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Views
Total Views
1,871
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
165
1
Likes
2
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Pointers by Jumail Bin Taliba Faculty of Computer Science & Information System
• 2. Today’s Topics
• Concept of Variables
• Concept of Pointers
• Declaring Pointer Variables
• Manipulating Pointers
• 3. Variables
• Variable is a space in memory that is used to hold a value.
• Each variable has a name , content and address
• Variable name is a logical reference, used by the programmer
• Address is a physical reference or actual location , and is used by the computer
• 4. Variables
• A char variable uses one byte
Print the addresses Output: 142300 142301
• 5. Variables
• An integer variable uses four bytes.
• The address of an integer variable is the first byte.
• 6. Pointer Variables
• What we have used so far are ordinary variables or also called data variables.
• A data variable contains a value (e.g. integer number, a real number or a character) .
• A pointer variable is another type of variable that contains the address of other variable . It also known as address variable .
• 7. Pointers
• Example:
• a is a data variable
• p is a pointer variable that stores the address of a – that is &a .
• 8. Pointer Variable Declaration
• A pointer variable is declared like the data variable. The difference is, put an asterisk (*) in front of it.
• Example:
• int a=-123; // a is a data variable
• int *p; // p is pointer variable
• p = &a; // p then holds the address of variable a
• 9.
• Example: p and q point to the same variable.
• int a=-123;
• int *p;
• int *q;
• p=&a;
• q=p;
• 10.
• The type of a pointer must be matched with the type of the variable that the pointer points to.
• int n;
• int *p;
• double *q;
• p=&n; // this is OK
• q=&n; // this is wrong, type mismatch
• q=p; // this is also wrong
• 11. Multiple Declarations
• To declare multiple pointers in a statement, use the asterisk for each pointer variable
• Example: int *p1, p2;
• Only p1 is a pointer variable; p2 is an ordinary variable
• You may also use typedef to make the declaration clear
• Example: typedef int *IntPtr;
• IntPtr p1, p2;
• Now, both p1 and p2 are pointer variables
• 12. Initialization of Pointer Variables Pointer variables can also be declared and initialized in a single statement
• 13. Accessing Variables Through Pointer Variables
• There are two special operators for pointers: address-of operator (&) and indirection operator (*) .
• is used for getting the address of a variable
• Example: &n
• meaning: “give me the address of variable n”
• Indirection operator (also called dereferencing operator), *
• is used for getting the content of a variable whose address is stored in the pointer.
• Example: *ptr
• meaning: “give me the content of a variable whose address is in pointer ptr”
• Notice that, a pointer declaration also uses an asterisk (*) . Don’t get confused . Pointer declaration and indirection operator are different.
• Indirection operator must only be used with a pointer variable . If n is a data variable, the following would be an error. *n
• 14. The & and * Operators
• void main(void)
• { int a = 5;
• int *ptr = &a;
• printf(&quot;%d &quot;,ptr);
• printf(&quot;%d &quot;,&a);
• printf(&quot;%d &quot;,&ptr);
• printf(&quot;%d &quot;,*ptr);
• *ptr = *ptr + 5;
• printf(&quot;%d &quot;,*ptr);
• printf(&quot;%d &quot;,a);
• }
Memory Address Content 5 2000 2000 2004 a ptr Prints 2000 The addresses are specified by the computer. The addresses above are used only for examples. Prints 2000 Prints 2004 Prints 5. This is how it works. ptr contains 2000 . Go to the address 2000 and get its content => 5. Means that, the value of *ptr is 5. This means, a = a + 5 , because ptr holds the address of a. The new value of a is 10 Prints 10 Prints 10 2008 2009 2010 2011 printf(&quot;%d &quot;, *a ); // This would be an error, because // a is not a pointer variable
• 15.
• The & and * operators are canceling each other
• Example:
• int x;
• int *p=&x;
• *&x is equivalent to x and p.
• However, the following would be an error because x is not a pointer. The * operator can only be used with a pointer variable.
• &*x // Error! This means &(*x)
• 16. int x; int *p=&x; int *q=&x;
• 17. Caution! Pointer assignments
• 18. The malloc Function
• Using pointers, variables can be manipulated even if there is no identifier for them. This variable is called a dynamic variable (or sometimes nameless variable)
• To create a pointer to a new dynamic variable of type int, use the malloc function
• Example:
• int *p;
• p = (int*) malloc ( sizeof(int) );
• You have to include stdlib.h to use the malloc function
p ? dynamic variable pointer variable
• 19. The malloc Function
• The new variable can only be accessed through p . The new variable is then referred to as *p1
• It can be used anyplace an integer variable can
• scanf(“%d”,&*p1 ); // or simply scanf(“%d”, p1 );
• *p1 = *p1 + 7;
p ? dynamic variable pointer variable
• 20.
• Dynamic variables are variables that are created and destroyed while the program is running.
• Static variables (sometimes called automatic variables ) are variables that are automatically created and destroyed by the computer.
Dynamic and Static Variables
• Example:
• int n=99;
• int *p;
• p = new int;
• *p = 10;
p 10 dynamic variable static variable 99 static variable p n
• 21. Basic Memory Management
• An area of memory called the freestore is reserved for dynamic variables
• New dynamic variables use memory in the freestore
• If all of the freestore is used, calls to new will fail
• Unneeded memory can be recycled
• When variables are no longer needed, they can be deleted and the memory they used is returned to the freestore
• 22. The free Function
• When dynamic variables are no longer needed, free them to return memory to the freestore
• Example:
• The value of p is now undefined and the memory used by the variable that p pointed to is back in the freestore
free(p);
• After:
• Before:
? p p ?
• 23. Pointers to Pointers Output: 58 Output: 58 Output: 58