Difference between revisions of "C-and-C++/C4/Understanding-Pointers/English"
(Created page with ''''Title of script''': Pointer in C and C++ '''Author: Ashwini Patil''' '''Keywords: Pointers, video tutorial()''' {| style="border-spacing:0;" | style="border-top:0.002cm s…') |
|||
Line 13: | Line 13: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Opening slide1 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Opening slide1 | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on Pointers in C and C++ | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Pointers in C and C++''' |
|- | |- | ||
Line 19: | Line 19: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn | ||
− | + | Pointers. | |
− | + | To create Pointers. | |
And operations on Pointer. | And operations on Pointer. | ||
+ | |||
+ | We will do this with the help of an example. | ||
|- | |- | ||
Line 29: | Line 31: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using | ||
− | Ubuntu | + | Ubuntu OS version11.10 |
− | gcc | + | gcc and g++ compiler version 4.6.1 on Ubuntu |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| What is a Pointer? | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| What is a Pointer? | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Pointers point to the locations in memory. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let is start with an introduction to pointers. |
− | + | Pointers point to the locations in memory. | |
Pointers store the memory address. | Pointers store the memory address. | ||
− | |||
It also gives value stored at that address. | It also gives value stored at that address. | ||
Line 53: | Line 47: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to gedit Text Editor | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to gedit Text Editor | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Now let us see an example on Pointers | ||
+ | Note that our filename is '''pointer_demo.c''' | ||
+ | Let us go through the code now. | ||
+ | This is our header file as '''stdio.h''' | ||
+ | This is our main function. | ||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
Highlight num | Highlight num | ||
− | |||
− | |||
− | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | |
+ | Here we have a long integer num assigned value 10. | ||
|- | |- | ||
Line 66: | Line 66: | ||
Asterisk sign is used to declare a pointer. | Asterisk sign is used to declare a pointer. | ||
− | This pointer can point to type int. | + | This pointer can point to type '''long int.''' |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight &num | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight &num | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In the printf statement ampersand is used for retrieving memory address of the variable. |
− | So ampersand num will give the memory | + | So ampersand num will give the memory address of num. |
This statement will print the address of the variable num. | This statement will print the address of the variable num. | ||
Line 96: | Line 96: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight *ptr | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight *ptr | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And asterisk ptr will give the value at the address. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And here asterisk ptr will give the value at the address. |
So using asterik will not give the memory address. | So using asterik will not give the memory address. | ||
Instead it will give the value. | Instead it will give the value. | ||
+ | %ld is the format specifier for long int. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Save, Compile and Run | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Save, Compile and Run | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Now let us execute the program. |
+ | Open the '''terminal''' by pressing '''Ctrl, Alt and T''' keys simultaneously on your keyboard. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type: gcc pointer_demo.c -o point. |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | ./point |
− | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type: | |
− | + | gcc space pointer_demo.c space -o space point. | |
− | + | '''Press Enter''' | |
− | + | ./point | |
− | + | '''Press Enter''' | |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the same | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed. |
+ | We see that the num address and ptr value is same. | ||
− | + | Where as memory address of num and ptr are different. | |
− | + | Then the size of pointer is 8 bytes. | |
− | + | ||
− | + | Also the value pointed by ptr is 10, which was assigned to num. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Code in C++ |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see the same program in C++. |
− | + | Note that the filename is '''pointers_demo.cpp''' | |
− | + | Here we have a few changes. | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|Highlight Code in C++ |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Like the header file as '''iostream''' |
+ | Then we are using the '''std namespace''' | ||
− | |||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|Highlight Code in C++ |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have cout function in place of printf function. |
+ | Rest all the things are similar. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Let us execute the program. |
+ | Come back to our terminal. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|To compile type: |
− | + | g++ space pointer_demo.cpp space -o space point1 | |
− | + | '''Press Enter''' | |
− | + | ./point1 | |
− | + | '''Press Enter''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the output | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We can see that the output is similar to our C program. |
− | + | This brings us to the end of this tutorial. | |
− | + | Come back to our slides. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Summary Slide | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Summary Slide | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize. |
+ | In this tutorial we have learnt. | ||
About the pointer. | About the pointer. | ||
Line 226: | Line 180: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Assignment Slide | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Assignment Slide | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment |
+ | Write a C and C++ program, | ||
declare a variable and pointer. | declare a variable and pointer. | ||
Line 239: | Line 194: | ||
About Slide | About Slide | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To know more about the Spoken Tutorial Project | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To know more about the Spoken Tutorial Project | ||
− | * Watch the video available at [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial http://spoken-][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial tutorial.org/What_is_a_Spoken_Tutorial] | + | * Watch the video available at the link shown below[http://spoken-tutorial.org/What_is_a_Spoken_Tutorial http://spoken-][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial tutorial.org/What_is_a_Spoken_Tutorial] |
* It summarizes the Spoken Tutorial project | * It summarizes the Spoken Tutorial project | ||
Line 271: | Line 226: | ||
* It is supported by the National Mission on Education through ICT, MHRD, Government of India | * It is supported by the National Mission on Education through ICT, MHRD, Government of India | ||
− | * More information on this Mission is available at | + | * More information on this Mission is available at the link shown below. |
* [http://spoken-tutorial.org/NMEICT-Intro http://spoken-tutorial.org/NMEICT-Intro] | * [http://spoken-tutorial.org/NMEICT-Intro http://spoken-tutorial.org/NMEICT-Intro] |
Revision as of 13:36, 31 January 2014
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
Pointers. To create Pointers. And operations on Pointer. We will do this with the help of an example. |
System Requirement Slide 3 | For this tutorial I am using
Ubuntu OS version11.10 gcc and g++ compiler version 4.6.1 on Ubuntu |
What is a Pointer? | Let is start with an introduction to pointers.
Pointers point to the locations in memory. Pointers store the memory address. It also gives value stored at that address. |
Switch to gedit Text Editor | Now let us see an example on Pointers
Note that our filename is pointer_demo.c Let us go through the code now. This is our header file as stdio.h This is our main function. |
Highlight num |
Here we have a long integer num assigned value 10. |
Highlight *ptr | Then we have declared a pointer ptr.
Asterisk sign is used to declare a pointer. This pointer can point to type long int. |
Highlight &num | In the printf statement ampersand is used for retrieving memory address of the variable.
So ampersand num will give the memory address 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 here asterisk ptr will give the value at the address.
So using asterik will not give the memory address. Instead it will give the value. %ld is the format specifier for long int. |
Save, Compile and Run | Now let us execute the program.
Open the terminal by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
Type: gcc pointer_demo.c -o point.
./point |
To compile type:
gcc space pointer_demo.c space -o space point. Press Enter ./point Press Enter
|
Output | The output is displayed.
We see that the num address and ptr value is same. Where as memory address of num and ptr are different. Then the size of pointer is 8 bytes. Also the value pointed by ptr is 10, which was assigned to num. |
Code in C++ | Now let us see the same program in C++.
Note that the filename is pointers_demo.cpp Here we have a few changes. |
Highlight Code in C++ | Like the header file as iostream
Then we are using the std namespace
|
Highlight Code in C++ | Here we have cout function in place of printf function.
Rest all the things are similar.
|
On the terminal | Let us execute the program.
Come back to our terminal. |
To compile type:
g++ space pointer_demo.cpp space -o space point1 Press Enter ./point1 Press Enter | |
Output | We can see that the output is similar to our C program.
This brings us to the end of this tutorial. Come back to our slides. |
Summary Slide | Let us summarize.
In this tutorial we have learnt. About the pointer. To create a pointer. Operation on pointer. |
Assignment Slide | As an assignment
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. |