Difference between revisions of "C-and-C++/C4/Understanding-Pointers/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 29: Line 29:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| System Requirement Slide 3
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| System Requirement Slide 3
| 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;"| To record this tutorial I am using  
  
 
Ubuntu OS version11.10  
 
Ubuntu OS version11.10  
Line 48: Line 48:
  
 
| 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
 
| 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'''
+
Note that our filename is '''pointers_demo.c'''
 
Let us go through the code now.
 
Let us go through the code now.
 +
 
This is our header file as '''stdio.h'''
 
This is our header file as '''stdio.h'''
 +
 
This is our main function.
 
This is our main function.
  
Line 58: Line 60:
  
 
| 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 a long integer num assigned value 10.
+
Here we have a '''long integer num''' assigned value '''10.'''
  
 
|-
 
|-
Line 101: Line 103:
  
 
Instead it will give the value.
 
Instead it will give the value.
 +
 
%ld is the format specifier for long int.
 
%ld is the format specifier for long int.
  
Line 109: Line 112:
  
 
|-
 
|-
| 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:none;padding:0.097cm;"| Type: gcc pointers_demo.c -o point.
 
./point  
 
./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:
 
| 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.
+
gcc space pointers_demo.c space -o space point.
 
'''Press Enter'''
 
'''Press Enter'''
 
./point
 
./point
Line 125: Line 128:
 
Where as memory address of num and ptr are different.
 
Where as memory address of num and ptr are different.
  
Then the size of pointer is 8 bytes.
+
Then the size of pointer is '''8 bytes.'''
  
Also the value pointed by ptr is 10, which was assigned to num.
+
Also the value pointed by '''ptr''' is 10, which was assigned to '''num.'''
  
 
|-
 
|-
Line 133: Line 136:
 
| 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++.
 
| 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'''
 
Note that the filename is '''pointers_demo.cpp'''
 +
 
Here we have a few changes.
 
Here we have a few changes.
  
Line 156: Line 160:
 
| 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:
 
| 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
+
'''g++ space pointers_demo.cpp space -o space point1'''
 +
 
 
'''Press Enter'''
 
'''Press Enter'''
 +
 +
Type
 +
 
./point1
 
./point1
 +
 
'''Press Enter'''
 
'''Press Enter'''
  

Latest revision as of 16:25, 31 January 2014

Title of script: Pointer in C and C++

Author: Ashwini Patil

Keywords: Pointers, video tutorial()


Visual Cue
Narration
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 To record 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 pointers_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 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 pointers_demo.c -o point.

./point

To compile type:

gcc space pointers_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 pointers_demo.cpp space -o space point1

Press Enter

Type

./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
  • If you do not have good bandwidth, you can download and watch it


Slide 9

About Slide


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


Slide 10

Acknowledgment

Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at the link shown below.


We have come to the end of this tutorial.

Thanks for joining.

This is Ritwik signing off.

Jai Hind.

Contributors and Content Editors

Ashwini