Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-18-2005, 04:21 PM   #1 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Talking TIP #4: Pointer Rambo gets busted.

A 10 minute read this time.

Intro

Soon enough the student will know the basics of pointers. Below I will create a character array, and a pointer that will point to this array:
Code:
char FirstArray[20];
char* pToArray;
pToArray = FirstArray;
This is what we are talking about in TIP #4.

A Tiny Program

Let's write a program that does the following:
Create an infinite loop that keeps asking for (console) input. When some input is given, output to screen, showing the entered word letter by letter.
One special requirement: Use pointers only in the loop!

Okidoky. Here is our neat attempt:
Code:
#include <iostream>
#include <cstring>

int main() 
{ 
  char FirstArray[20];
  char* pToArray;
  pToArray = FirstArray;
  
  for(;;) //Forever.
  {
    std::cout<<"Enter a string: "<<std::endl;
    std::cin>>FirstArray;
    
    while(*pToArray)
    {
      std::cout<<*pToArray<<std::endl;
      *pToArray++;
    }
  }  

  return (0); 
}
The Problem

Ay, this may or this may not work. The standard doesn't guaruantee it. Why should it anyway?
The first time the loop runs, all works well. But the next time, it gets buggy. The reason is that the pointer pToArray is increased by 1 each iteration. So after the last iteration (depends on entered word length), pToArray may point to anywhere.

The Solution

The solution is to move the pointer assignment inside the main loop. This way you'll be sure that on each iteration, the pointer points to a valid location, namely the start of data pointed by FirstArray:
Code:
#include <iostream>
#include <cstring>

int main() 
{ 
  char FirstArray[20];
  char* pToArray;
  
  for(;;) //Forever.
  {
    pToArray = FirstArray; // !
    std::cout<<"Enter a string: "<<std::endl;
    std::cin>>FirstArray;
    
    while(*pToArray)
    {
      std::cout<<*pToArray<<std::endl;
      *pToArray++;
    }
  }  

  return (0); 
} 

//-----------
The Moral

Learn to keep ACTIVELY track of the behaviour of individual pointers as well as their relationships with eachother. One good way is to actually sketch the memory layout on a piece of paper at key code execution points. Do this when the tough gets going. Pointer Rambo's don't exist.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof() and pointer vs array DJMaze Standard C, C++ 21 04-19-2005 06:44 PM
Array to pointer to pointer frier Standard C, C++ 8 03-25-2005 07:57 PM
Explicit vs. Implicit use of the this Pointer fp_unit Standard C, C++ 2 01-30-2005 04:20 PM
pointer to function with class? Kportertx Standard C, C++ 5 04-11-2003 06:12 AM


All times are GMT -8. The time now is 06:22 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting