Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

johnc82

cin >> *ptr;

This topic is 5809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi i wonder how to make the code below work: char *c = NULL; cin >> c; cout << c << "\n"; Thanks :-)

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by johnc82
i wonder how to make the code below work
<snip>

Make c point to a valid block of memory (cin needs something to write to).

Share this post


Link to post
Share on other sites
this means it only work like:

char c[20];
cin >> c;
cout << c << "\n";

however does anyone have try to solve the problem that make the cin "write" to a pointer:

char *c = NULL;
cin >> c;
cout << c << "\n";

Thanks

:-)

Share this post


Link to post
Share on other sites
quote:
Original post by johnc82
this means it only work like:

char c[20];
cin >> c;
cout << c << "\n";

however does anyone have try to solve the problem that make the cin "write" to a pointer:

char *c = NULL;
cin >> c;
cout << c << "\n";

Thanks

:-)



The problem is simple. A pointer points to an address in memory, you can''t write to a pointer unless it is pointing to a valid peice of memory. Which means it must be allocated somehow. You have 2 choices Static memory allocation, or dynamic memory allocation. Static memory allocation looks like this "char buffer[20]" while dynamic looks like this "char *string; string = new char[20]:". The dynamic solution to your problem is as follows:


char buffer[80];
cin >> buffer;
char *c = new char[strlen(buffer)+1];
strcpy(c,buffer);
c[strlen(buffer)] = ''\0'';
cout << c << endl;
delete [] c;

the static solution is

char c[80];
cin >> c;
cout << c << endl;


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry but that dynamic solution is terrible.

Firstly when you use strcpy() it deals with the null zero''s for you.


  
strcpy(c, buffer);
c[strlen(buffer)] = ''\0'';


should just be:


  
strcpy(c, buffer);


Also, why do you cin into static memory then copy into dynamic memory?

The dynamic solution should just be:


  
char *c = new char[80];
cin >> c;
cout << c << endl;


or even better:


  
char *c = new char[80];
cin.getline(c, 80);
cout << c << endl;


and always remember to de-allocate your string when you''ve used new:


  
delete []c;

Share this post


Link to post
Share on other sites
[offtopic]
I want scaling source boxes back! That, or an automatic switch to code tags if the enclosing code spans fewer than a set number of lines. Reading that previous reply was terrible.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster

Also, why do you cin into static memory then copy into dynamic memory?

The dynamic solution should just be:


char *c = new char[80];
cin >> c;
cout << c << endl;


or even better:


char *c = new char[80];
cin.getline(c, 80);
cout << c << endl;


and always remember to de-allocate your string when you''ve used new:


delete []c;



Obviously the reason he used a static buffer instead of allocating memory was that he didn''t want to dynamically allocate more memory than he needed for the current string input. Seeing as how you have no way of determining the length of the string before it''s placed into a buffer it was the correct way to allocate the appropriate ammount of memory for the string.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!