944,047 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 664
  • C++ RSS
Nov 6th, 2009
0

Problem with passing char arrays to functions

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spankboy11 is offline Offline
7 posts
since Jun 2009
Nov 6th, 2009
-7
Re: Problem with passing char arrays to functions
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.

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. char* tags[4] = {0};
  2. char radia1[80];
  3. tags[1] = radia1;
  4. ...
  5. ...
Last edited by Ancient Dragon; Nov 6th, 2009 at 10:34 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Nov 7th, 2009
0
Re: Problem with passing char arrays to functions
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spankboy11 is offline Offline
7 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to store data into a single file
Next Thread in C++ Forum Timeline: Compiling error (novice problem)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC