Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer problems with (char*)

Status
Not open for further replies.

Ixcaliber

Programmer
Apr 6, 2003
6
0
0
CA
I have the following code, and am trying to read in a character string from the keyboard, and save it as the name of the vector... but as you can see from the output, the input is saved until the next cin statement, at which time the name is switched to 2!! I imagine it is some pointer problem, but I haven't fully mastered them (understatement).


I'm using Visual C++ 6.0 if it matters.

struct Vect
{
char *name;
int size;
float *entry;
};

Vect *vect;

void createNewVector()
{

// somewhere...
vect = new Vect[n];

.........

// set the name
cout << &quot;Create New Vector \n\n Enter a name: &quot;;

char *name;
cin >> name;

vect[vectors-1].name = name;

// checks the name values
cout << &quot;Name1: v,n: &quot; << vect[vectors-1].name << &quot;,&quot; << name << endl;

char *entries;
// get the entries
cout << &quot;Enter Number of Entries: &quot;;
cin >> entries;

// check the name values again...
cout << &quot;Name2: v,n: &quot; << vect[vectors-1].name << &quot;,&quot; << name << endl;

.........

}


/* output:

Create New Vector

Enter a name: a
Name1: v,n: a,a
Enter Number of Entries: 2
Name2: v,n: 2,2

*/


Why do the name values change when I don't change them???
 
>char *name;
>cin >> name;
and
>char *entries;
>// get the entries
>cout << &quot;Enter Number of Entries: &quot;;
>cin >> entries;
Automatic pointers begin with garbage addresses. This means that you don't own the memory they point to. If you don't point them to a valid address then you'll either crash with a bus error or break something unexpectedly later in your program. A quick fix would be to use C++ strings instead of error prone pointers:
Code:
#include <string>

string name;
cin>> name;

char *entries;
// get the entries
cout<<&quot;Enter Number of Entries: &quot;;
cin>> entries;
Or if the input values may have whitespace in them use getline:
Code:
getline(cin, name);
 
Hi
I think this yours sample is not much clear as even the usage of structure too..But I gave a try with your pointer problem ..Ok i picked up whatever you posted..Akarui is right upto some extent ..pointers are not much helpful if memory location to be used is not much clear..at least i prefer in my apps usage of buffers unless it becomes mandatory to use pointers (..on safe side)
Ok here is code..hope it helps a bit
#include &quot;stdafx.h&quot;
#include <iostream.h>
#include <windows.h>

#define vectors 3

struct Vect
{
char name[16];
int size;
float *entry;
};

struct Vect* vect;//This line I modified


int main(int argc, char* argv[])
{
vect = (Vect*) new Vect[vectors];
cout << &quot;Create New Vector \n\n Enter a name: &quot;;

char name[]=&quot;&quot;;//This line I modified
cin >> name;

strcpy(vect[vectors-1].name, name);//This line I modified

// checks the name values
cout << &quot;Name1: v,n: &quot; << vect[vectors-1].name << &quot;,&quot; << name << endl;

char entries[]=&quot;&quot;;//This line I modified
// get the entries
cout << &quot;Enter Number of Entries: &quot;;
cin >> entries;

// check the name values again...
cout << &quot;Name2: v,n: &quot; << vect[vectors-1].name << &quot;,&quot; << name << endl;

delete[] vect;//This line I added
vect=NULL;//This line I added
exit(0);//This line I added
return 0;
}

 
>struct            Vect*               vect;//This line I modified
There's no need to use the struct keyword in C++, your modification does nothing except make the fact that Vect is a structure more explicit.

>char name[]=&quot;&quot;;//This line I modified
This isn't any better, name is now an array of size 1. If the name entered is longer than one character, you'll be overwriting memory your program doesn't own, the exact same problem.

>strcpy(vect[vectors-1].name, name);//This line I modified
This wasn't technically needed in the original code since Vect::name was a pointer, you changed it to an array. I agree that this way is better if name is likely to change.

>char entries[]=&quot;&quot;;//This line I modified
See above, this is the same problem.
Code:
#include <iostream>
#include <string>

using namespace std;

struct Vect
{
    string name;
    int size;
    float *entry;
};

Vect *vect;

const int n = 3;
const int vectors = 1;

void createNewVector()
{
    vect = new Vect[n];
    
    // set the name
    cout << &quot;Create New Vector \n\n Enter a name: &quot;;
    string name;
    cin>> name;
    
    vect[vectors-1].name = name;
    
    // checks the name values
    cout << &quot;Name1: v,n: &quot; << vect[vectors-1].name << &quot;,&quot; << name << endl;

    string entries;
    // get the entries
    cout << &quot;Enter Number of Entries: &quot;;
    cin>> entries;
    
    // check the name values again...
    cout << &quot;Name2: v,n: &quot; << vect[vectors-1].name << &quot;,&quot; << name << endl;
}

int main()
{
    createNewVector();
}
 
Thanks Akarui, it works great with the string class. I was hoping to understand the pointer arrays, and mem allocation, but it seems finding ways around the problem is usually easier than understanding what the problem really is...
 
>I was hoping to understand the pointer arrays, and mem allocation
Pointer arrays are generally unsafe and error prone, you shouldn't need them much at all with the string and vector classes offered by C++. However, the basic idea is this:
Code:
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char *pstr; // Start with a simple pointer

    pstr = new char[10]; // Allocate memory to the pointer

    // Access the memory through the pointer
    strncpy(pstr, &quot;Testing&quot;, 10);
    cout<< pstr <<endl;

    delete [] pstr; // Release the memory when you're done
}
If you have a pointer, it must point to either the address of another variable, or dynamically allocated memory. Anything else is very suspect if you use the pointer. Always delete memory that you allocate, and be very wary of the array boundaries.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top