What's wrong with this strcpy and this pointer

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: What's wrong with this strcpy and this pointer

 
0
  #11
Feb 18th, 2007
Originally Posted by earlyriser View Post
I didn't know that C is better for char arrays than C++.
That's not true, character arrays can be handled equally by either language.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: What's wrong with this strcpy and this pointer

 
0
  #12
Feb 18th, 2007
Originally Posted by Ancient Dragon View Post
That's not true, character arrays can be handled equally by either language.
Well, yes.

My point was that it's like this. Say you want to input a string using getline():
  1. #include <string>
  2. std::string line;
  3. getline(cin, line);
Now that's all fine and dandy. But let's say you want to copy this to a char string:
  1. #include <cstring>
  2. char name[20];
  3. strcpy(name, line.c_str()); // should error check this
But now you've just had to resort to C to process the string. Sure there are probably workarounds, but generally it's a much, much better idea to use std::strings (although not in all cases; for example some constructors such as std::stream require C-strings).
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: What's wrong with this strcpy and this pointer

 
0
  #13
Feb 19th, 2007
Originally Posted by joeprogrammer View Post
If you're going to use char arrays, you might as well be using C. And no, I know it's not your fault, so I blame your teacher for that.
Seems to me the concept here is to learn to process c-strings, because they are necessary in C++.

earlyriser, stop using any and all functions that have a definition of string and use only char* functions. Help us save Joe's sanity.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: What's wrong with this strcpy and this pointer

 
0
  #14
Feb 19th, 2007
Originally Posted by WaltP View Post
Seems to me the concept here is to learn to process c-strings, because they are necessary in C++.

earlyriser, stop using any and all functions that have a definition of string and use only char* functions. Help us save Joe's sanity.
He's already said his instructor says he must use char arrays. He should be writing a pure C program, not a C++ program basterdised with C. If that is what his instructor is teaching then the instructor needs to take a few more programming courses.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

Re: What's wrong with this strcpy and this pointer

 
0
  #15
Feb 19th, 2007
Hi. In fact the Homework is in C++.
Maybe the error is mine (I dont want to embarras my teacher's reputation).
The structs provided in the HW are below, with the chnages I did because I wnated to reduce the difficulty and have less pointers. Maybe that was the thing that brought the insanity.

struct  Professeur{
   char*   nom; // changed to char nom[20]
   char*   prenom;
   Cours*  listecours;
   Etudiants*  listetudiants;
   Professeur* suivant;
};
 
struct Cours {
   char     * sigle; // changed to char sigle[10]
   Cours  * suivant;
};
 
struct  Etudiant {
   char*     nom; //changed to char nom[20]
   Etudiant* apres;
};

By the way the point of the HW are classes and not the string manipulation
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: What's wrong with this strcpy and this pointer

 
0
  #16
Feb 19th, 2007
>If that is what his instructor is teaching then the instructor needs to take a few more programming courses.

Not really, like Mr disney said it's nice to know about char[] style strings. In fact you'll find a lot of institutions teach kids c++ this way.

Will they come out knowing true c++? Nup, but there ya go. :lol:
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: What's wrong with this strcpy and this pointer

 
0
  #17
Feb 19th, 2007
>>By the way the point of the HW are classes

Well, so much for your teacher's reputation, it just went into the gutter :eek: But we should be discussing your HW, not your teacher.

Had you left the pointers there it would have made your program just a small tad more complex but a whole lot safer because you could have allocated memory of the correct size for the strings, something like this:
  1. PCourant->nom = new char[line.size()+1];
  2. strcpy (PCourant->nom, line.c_str());

Don't forget to delete[] to memory when done with it.
Last edited by Ancient Dragon; Feb 19th, 2007 at 4:56 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

Re: What's wrong with this strcpy and this pointer

 
0
  #18
Feb 20th, 2007
Thanks for all your tips. I will close this thread, because I have started another one with a less specific topic:
http://www.daniweb.com/techtalkforum...tml#post317987
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: What's wrong with this strcpy and this pointer

 
0
  #19
Feb 20th, 2007
Originally Posted by earlyriser View Post
After reading a line in a text file I need to read another line which contains an INT. I thought that the bold line in this code will be ok to retreive the data, but it doesnt.

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. struct Professeur
  9. {
  10. char nom[20];
  11. int ancien;
  12. };
  13.  
  14.  
  15. class DossierProfesseur
  16. {
  17.  
  18.  
  19. public:
  20. Professeur *tete;
  21. DossierProfesseur();
  22. ~DossierProfesseur();
  23. };
  24.  
  25.  
  26. DossierProfesseur::DossierProfesseur()
  27. {
  28. Professeur *PCourant, *PTete=NULL;
  29. string line;
  30. ifstream myfile ("FP.txt");
  31. if (myfile.is_open())
  32. {
  33. while (! myfile.eof() )
  34. {
  35. getline (myfile,line);
  36. PCourant = new Professeur;
  37. strcpy (PCourant->nom, line.c_str());
  38. cout << PCourant->nom;
  39. [B]getline (myfile, PCourant->ancien);[/B]
  40. }
  41. myfile.close();
  42. }
  43.  
  44. else cout << "Unable to open file";
  45. }
  46.  
  47.  
  48. void main () {
  49.  
  50.  
  51. DossierProfesseur *dossierProfesseur;
  52.  
  53. dossierProfesseur= new DossierProfesseur;
  54. }
Being the nerd I am, I'll give my opinion on this particular issue. If you're going to use a structure data type, I'd just use malloc instead of new to allocate memory on the heap:

  1. PCourant = (struct Professeur*)malloc(sizeof(struct Professeur));

Or you can define a macro something similar to new such as:
  1. #define Professor Professeur
  2. #define new(x) (x*)malloc(sizeof(x))
  3.  
  4. PCourant = new(Professor);
And for the string variable line, you might as well use a character array of say 100 characters or etc:
  1. #define MAX_LINE 100
  2. char line[MAX_LINE];
This will save the trouble calling the c_str() string class function and perhaps a bit of proccessor time. So now you previous version would be ok. Last, don't forget to free the allocated memory via the "free" function.

This is just an alternative way, but I guess it boils down to the specifications of your assignment.

Good luck, Lamabot
Last edited by Lazaro Claiborn; Feb 20th, 2007 at 8:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: What's wrong with this strcpy and this pointer

 
0
  #20
Feb 20th, 2007
Originally Posted by Lazaro Claiborn View Post
Being the nerd I am, I'll give my opinion on this particular issue. If you're going to use a structure data type, I'd just use malloc instead of new to allocate memory on the heap:

  1. PCourant = (struct Professeur*)malloc(sizeof(struct Professeur));
Why would you want to do that? :rolleyes:

This is a C++ program. So then use C++ memory allocation methods. The OP's method of using new was fine. (Aside: 2 things the OP did do wrong; didn't use delete and there's a memory leak in the loop as I mentioned previously.)

And for the string variable line, you might as well use a character array of say 100 characters or etc:
  1. #define MAX_LINE 100
  2. char line[MAX_LINE];
Yeah, except that getline() requires a C++ string as an argument... that's why the OP used it in the first place.

And while I'm critcizing people's code (sorry, I can't help it!), trash that void main(). It's not standard, nor is it good practice.
http://users.aber.ac.uk/auj/voidmain.shtml
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 5569 | Replies: 26
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC