C-and-C++/C4/Understanding-Pointers/English

From Script | Spoken-Tutorial
Revision as of 13:57, 3 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

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.


If not, for relevant tutorials please visit our website as shown.

What is a Pointer? 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


Highlight num

Let us now see the code


We have a variable 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 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


Point to Arr[5]

We will now see a opearation perfomed on a pointer.


In the code, we have declared an array Arr having 5 elements.

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
  • 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


We have come to the end of this tutorial.

Thanks for joining.

This is Ritwik signing off.

Jai Hind.

Contributors and Content Editors

Ashwini