Problem with passing char arrays to functions

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 2
Reputation: spankboy11 is an unknown quantity at this point 
Solved Threads: 0
spankboy11 spankboy11 is offline Offline
Newbie Poster

Problem with passing char arrays to functions

 
0
  #1
20 Days Ago
Hi there, I'm assuming the answer to this problem is really easy, but I just can't work out where the problem is. It's driving me insane and I'd really appreciate some help. I'm new to C++.

My problem is this: I'm trying to pass a char array to a function, where a line is read from a file and then the array is passed back. However, the function returns gibberish - but ONLY when returning a file-derived array element. Here is my code:

  1. int main ()
  2. {
  3. char* tags[128];
  4. char* file="project3.xml";
  5.  
  6. FILE *fp;
  7. fp=fopen(file, "r");
  8.  
  9. int tits = NextTag( fp, tags );
  10. cout << "tags[0] post-Fn = *" << tags[0] << "*\n";
  11. cout << "tags[1] post-Fn = *" << tags[1] << "*\n";
  12.  
  13. system("PAUSE");
  14. return 0;
  15. }
  16.  
  17. int NextTag( FILE* fp, char* tags[] )
  18. {
  19. int count2 = 0;
  20. char* pch;
  21. char rida1[512];
  22. fgets(rida1, sizeof(rida1), fp);
  23. cout << "rida1 in-Fn = *" << rida1 << "*\n";
  24. tags[0] = "bottoms";
  25. tags[1] = rida1;
  26. cout << "tags[0] in-Fn = *" << tags[0] << "*\n";
  27. cout << "tags[1] in-Fn = *" << tags[1] << "*\n";
  28. }

The .xml file it's reading from just contains the word 'bottoms'. Here is the output:

  1. rida1 in-Fn = *bottoms*
  2. tags[0] in-Fn = *bottoms*
  3. tags[1] in-Fn = *bottoms*
  4. tags[0] post-Fn = *bottoms*
  5. tags[1] post-Fn = *L1"*
  6. Press any key to continue. . .
  7.  

Why the gibberish on line 5 of the output when it returns the manually entered entry fine? Any ideas? It's driving me insane!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
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: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
20 Days Ago
It returns gibberish because when that NextTag() returns the array radial is destroyed, which invalidates tags[1]. What you need to do is allocate memory for the text. Then main() will have to delete[] that memory when its done with it.

  1. tags[1] = new char[strlen(radia1)+1];
  2. strcpy(tags[1], radia1);

Another way to do it is for main() to allocate memory for that string
  1. char* tags[4] = {0};
  2. char radia1[80];
  3. tags[1] = radia1;
  4. ...
  5. ...
Last edited by Ancient Dragon; 20 Days Ago at 10:34 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: Jun 2009
Posts: 2
Reputation: spankboy11 is an unknown quantity at this point 
Solved Threads: 0
spankboy11 spankboy11 is offline Offline
Newbie Poster
 
0
  #3
19 Days Ago
A-ha! I hadn't even considered that it might be because rida1 was going out of scope (I had thought that once the value of rida1 had been passed to the array then rida1's value was irrelevent).

Thanks so much - it really makes sense now!
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC