| | |
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:
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 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.
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; 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.
![]() |
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)
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






