Hey everyone, thanks for taking the time to address my problem. I've looked at so much material at this point that I've just started getting more and more confused. Basically, I'm writing a simple segment of code that parses a string. Please understand that my question is NOT about parsing. I am using C++ and have never used C before and possess a little bit of c++ experience (introductory experience, I'm still a newbie).
struct parsedString{ char chunk1[50]; char chunk2[10]; char chunk3[50]; }; main(char* bigstring) { parsedString ps; ps = parseMe(bigString) cout << ps.chunk1 << endl; cout << ps.chunk2 << endl; cout << ps.chunk3 << endl; } parsedString parseMe(char* stringToParse) { char* parseStr = stringToParse; parsedString ps; ps.chunk1 = first x chars; ps.chunk2 = next y chars; ps.chunk3 = last z chars; return ps; }
Obviously this is not working code, I didn't want to throw up all the extra stuff since it would be tougher to read through and I'm pretty sure my problem is a newbie c/c++ problem and something about memory allocation or something like that...
Basically when the main function gets to printing the strings from the parsedString it prints out exactly what I want it to, plus garbage characters. I'm entering the values for each array ps.chunk using
ps.chunk1[i] = *parseStr
since parseStr gets me each individual character. I can't figure out where the garbage characters are coming from, does it have something to do with how I am accessing the big string? Originally I used char in the struct instead of arrays and when I printed from within the parseMe() function they would come out great but they would turn into gibberish when I accessed it from the main function. Any help is appreciated, thanks so much.
If something is unclear please let me know I'll do my best to elaborate.
std::string. Char arrays are used for that purpose in C, and using them (in either language) could indeed be a confusing topic for a beginner. Is there a reason why strings cannot be used?