Fun With Pointers in C++

In this articile, we try to go in depth and grasp the fundamental idea of using pointers. The articile will also show, how pointers manipulation in c++ can be carried out. The IDE tht was used in writing the code was .NET studio 2005. Scroll down for explanation of code.

 

 

 

MENU

1) Artificial Intelligence

2) Computer Security ( Bell LaPadula Model)

3) Critical Essay and Analysis Part 2

4) Critical Essay and Analysis Part 1.

5) Steps for installing Firestarter (fire wall) on Ubuntu    

6 )Here are some interesting Probability and Random Processes course problems.

7) Software Requirements

8) Resume Builder

9) Shell Scripting with Linux/Unix ( Part 2)

10) Shell Scripting with Linux/Unix 

 11) Latest World News:

12) Explicit and Implicit DLL Linking

13) VirtualFunctions C++

// hello_c++.cpp : Defines the entry point for the console application.

//

#include"stdafx.h"

classnode

{

public:

int data;

node *ptr;

public:

node()

{

data=0;

ptr=0;

}

};

node* linked_list()

{

node *head=0;

node *second=0;

node *third=0;

head=new node();

second=new node();

third=new node();

head->data=1;

head->ptr=second;

second->data=2;

second->ptr=third;

third->data=3;

third->ptr=0;

return head;

}

intcompute_length( node**linked_list2)

{

int count=0;

while(*linked_list2!=0)

{

*linked_list2=(*linked_list2)->ptr;

count=count+1;

}

return count;

}

voidpopulate_linked_list(node**ref_head)

{

int i=0;

//*ref_head=new node();

node* iterator=new node();

*ref_head=iterator;

while(i<6)

{

(iterator)->data=i;

if(i==5)

(iterator)->ptr=0;

else

{

(iterator)->ptr=new node();

iterator=iterator->ptr;

}

i=i+1;

}

}

voidpush(node**&head1,intdata)

{

node* new_node=new node();

new_node->data=data;

new_node->ptr=(*head1);

*head1=new_node; //in the first iteration,

}

voidappend_tail(node *& head_ref)

{

//make a copy of the head node

node * copy_head=head_ref;

node * tail;

while(copy_head!=0)

{

copy_head->ptr;

tail=copy_head;

copy_head=copy_head->ptr;

}

//at this point we have managed to get the last element

node * new_elem=new node();

new_elem->data=100;

new_elem->ptr=0;

tail->ptr=new_elem;

}

node * copy_list(node *original_pointer)

{

node *head_c=new node();

node **head_ref=&head_c;

while(original_pointer!=0)

{

(*head_ref)->data=original_pointer->data;

(*head_ref)->ptr=new node();

head_ref=&((*head_ref)->ptr);

original_pointer=original_pointer->ptr;

}

return head_c;

}

int _tmain(intargc, _TCHAR* argv[])

{

node *head=new node(); //pointer pointing to type node

node *head_c;

head->ptr=0;

node **ptr1=&head; //pointer pointing to type node*

int data=6;

while(data>=0)

{

push(ptr1,data);

ptr1=&(*ptr1)->ptr;

data=data-1;

}

append_tail(head);

head_c=copy_list(head);

// populate_linked_list(&head);

// linked_list2=new node();

// linked_list2->data=0;

// head=linked_list();

// linked_list2->ptr=head;

// head=linked_list2;

// int a=(int)&head;

// compute_length(&head);

return 0;

}

The focus of the article is on the function

node * copy_list(node *original_pointer)

The function main has a pointer called head of type node. This pointer points to the linked list in memory(heap). The idea now is to take this linked list and create a same duplicate memory and return the pointer pointing to the new linked list. How can this be performed?

The function copy_list as part of its argument takes in the parameter node* original_pointer which is a pointer to the original linked list.

Inside copy_list:

A pointer of type node called head_c is created which is pointing at the first element of the new linked list. A pointer of type node * is now created which will be used as an iterator. The purpose of this iterator is to walk through the new linked list, generate objects of type node appropiately and copying the data element from the original linked list into the new one. To a expert C++ programmer, the while loop is straight forward. To a novice user, futher explanation is provided. By dereferencing the head_ref pointer, ( remember this is the first time we enter the while loop) we are actually pointing to the heap memory pointed by the head_c pointer. We get the data from the original linked list and copy it to the new linked list. This is performed by the following statement.

(*head_ref)->data=original_pointer->data;

As this is a linked list, a new object is created

(*head_ref)->ptr=new node();

this object later will be populated with the 2nd element from the original linked list, when the control enters the while loop, the second time.

THE MOST IMPORTANT PIECE OF CODE IN THE FUNCTION IS THE FOLLOWING:

head_ref=&((*head_ref)->ptr);

The iterator, which is head_ref is dereferenced, to get the memory/heap being pointed by (*head_ref)->ptr. The address is captured of this heap/memory by using the & sign. This address is provided to the iterator so it can now point to the first new element just created.

Browse Happy logo