C-and-C++/C4/Understanding-Pointers/English
Title of script: Pointer in C and C++
Author: Ashwini Patil
Keywords: Pointers, video tutorial()
|
|
Opening slide1 | Welcome to the spoken tutorial on Pointers in C and C++ |
Learning objectives Slide 2 | In this tutorial we will learn
What are Pointers. How to create Pointers. And operations on Pointer. |
System Requirement Slide 3 | For this tutorial I am using
Ubuntu 11.10 OS gcc compiler and g++ |
Prerequisites | To follow this tutorial you must know how to write and run a C and C++ program.
|
What is a Pointer? | Pointers point to the locations in memory.
|
Switch to gedit Text Editor
|
Let us now see the code
|
Highlight *ptr | Then we have declared a pointer ptr.
Asterisk sign is used to declare a pointer. This pointer can point to type int. |
Highlight &num | Ampersand is used for retrieving memory address of the variable.
So ampersand num will give the memory laddress of num. This statement will print the address of the variable num. |
Highlight ptr=&num | Over here ptr stores the address of num. |
Highlight &ptr | This statement will print the address of ptr. |
Highlight sizeof(ptr) | Sizeof function will give the size of ptr. |
Highlight ptr | This is will give the value of ptr.
That is the memory address of num. |
Highlight *ptr | And asterisk ptr will give the value at the address.
So using asterik will not give the memory address. Instead it will give the value. |
Save, Compile and Run | Save , Compile and Run the file. |
Output | We see the output that num address and ptr value is same.
Where as memory address of num and ptr are different. Then the size of pointer is 4 bytes. Since int type size is 4 bytes. Also the value pointed by ptr is 10 which was assigned to num. |
Code in C++ | Now let us see the same code in C++.
It is similar to C. |
Save, Compile and Run | Save, compile and Run the file. |
Output | We see the same output.
So this is how pointers declared and initialized in C and C++. |
Code in C
|
We will now see a opearation perfomed on a pointer.
|
Highlight *point | Then we have declared a pointer point of type int.
As we see the asterisk sign a the start . |
Highlight point = Arr; | Then we have assigned the memory address of array to the pointer.
So point will hold the address of array Arr. |
Highlight print statement | Then we print the address of the pointer holding the first value of array. |
Highlight point++ | Here ++ operator moves the pointer ahead by one memory address.
So now it will point to the second value of array that it holds. |
Highlight print statement | This will print the address and value that pointer point is currently holding. |
Highlight point++ | Again point++.
So again it will point to the next memory address. That is to the 3rd value of array. Similarly – operator will make pointer point to the previous address. |
Highlight %p %d | Here make a note.
Percent p is used as format specifiers for pointer. While %d for value pointed by the pointer. This is because value is type int. |
Save, Compile and Run | Save , Compile and Run the file. |
Output | We see the output with addresses and their corresponding values. |
Code in C++ | We will see the same code in C++
It looks similar to C. |
Save, Run and Compile | Save, Run and Compile the program. |
Output | We see the same output.
So this is how pointers can be used in C and C++. |
Summary Slide | So in this tutorial we have learnt.
About the pointer. To create a pointer. Operation on pointer. |
Assignment Slide | For self assessment write a C and C++ program,
declare a variable and pointer. Store the address of variable in the pointer. Print the value of pointer. |
Slide 8
About Slide |
To know more about the Spoken Tutorial Project
|
Slide 9
About Slide
|
The Spoken Tutorial Project Team
|
Slide 10
Acknowledgment |
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
We have come to the end of this tutorial.
Thanks for joining. This is Ritwik signing off. Jai Hind. |