| | |
Problem with passing char arrays to functions
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 2
Reputation:
Solved Threads: 0
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:
The .xml file it's reading from just contains the word 'bottoms'. Here is the output:
Why the gibberish on line 5 of the output when it returns the manually entered entry fine? Any ideas? It's driving me insane!!
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)
int main () { char* tags[128]; char* file="project3.xml"; FILE *fp; fp=fopen(file, "r"); int tits = NextTag( fp, tags ); cout << "tags[0] post-Fn = *" << tags[0] << "*\n"; cout << "tags[1] post-Fn = *" << tags[1] << "*\n"; system("PAUSE"); return 0; } int NextTag( FILE* fp, char* tags[] ) { int count2 = 0; char* pch; char rida1[512]; fgets(rida1, sizeof(rida1), fp); cout << "rida1 in-Fn = *" << rida1 << "*\n"; tags[0] = "bottoms"; tags[1] = rida1; cout << "tags[0] in-Fn = *" << tags[0] << "*\n"; cout << "tags[1] in-Fn = *" << tags[1] << "*\n"; }
The .xml file it's reading from just contains the word 'bottoms'. Here is the output:
C++ Syntax (Toggle Plain Text)
rida1 in-Fn = *bottoms* tags[0] in-Fn = *bottoms* tags[1] in-Fn = *bottoms* tags[0] post-Fn = *bottoms* tags[1] post-Fn = *L1"* Press any key to continue. . .
Why the gibberish on line 5 of the output when it returns the manually entered entry fine? Any ideas? It's driving me insane!!
-7
#2 Nov 6th, 2009
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.
Another way to do it is for main() to allocate memory for that string
C++ Syntax (Toggle Plain Text)
tags[1] = new char[strlen(radia1)+1]; strcpy(tags[1], radia1);
Another way to do it is for main() to allocate memory for that string
C++ Syntax (Toggle Plain Text)
char* tags[4] = {0}; char radia1[80]; tags[1] = radia1; ... ...
Last edited by Ancient Dragon; Nov 6th, 2009 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.
![]() |
Similar Threads
- Help with char arrays (C)
- passing arrays to functions (PHP)
- Passing Multidimensional Arrays To Functions (C++)
- How to pass char arrays to functions? (C++)
- Problem when passing arrays from c# to matlab (C#)
- help passing parrallel arrays to functions (C++)
- passing linked lists through functions?? (C++)
Other Threads in the C++ Forum
- Previous Thread: how to store data into a single file
- Next Thread: Compiling error (novice problem)
Views: 310 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






