Alex
Alex

Reputation: 13

Confusing char arrays and pointers in c++

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.

Upvotes: 1

Views: 620

Answers (3)

karlphillip
karlphillip

Reputation: 93410

This could be happening due to a couple of reasons.

  • When you declare parsedString ps; it would be good to initialize the structure and make sure that you are receiving clean memory blocks to work with.parsedString ps = {0}; Some platforms don't do this and it's up to you to zero the memory before using it.

  • char* strings must end with the \0 character. This character signals the end of a char*. This is mandatory! Not inserting it at the end of the string will most probably make the print operation (for instance) display contents beyond the limits of the array giving you garbage characters. This is automatically achieved by zeroing all the struct as I suggest above.

Upvotes: 1

James Kanze
James Kanze

Reputation: 153919

It's not clear why you're trying to do this with '\0' terminated strings, when C++ has a perfectly usable string class, but supposing some pedagogical reasons: are your strings '\0' terminated? How do you extract and copy the first x chars into ps.chunk1? How do you ensure that it has a '\0'?

If you really want to get exactly n characters, you have to:

assert(strlen(parseStr) >= n);

, copy them into the target (which must contain at least n+1 char's),

memcpy(ps.chunk1, parseStr, n);

, and add the final '\0':

ps.chunk1[n] = '\0';

(And, of course, advance parseStr: parseStr += n;)

Or you can simply use std::string everywhere, and write something like:

ps.chunk1(stringToParse.substr(startPosition, length));

Upvotes: 6

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76818

As pointed out by others, you should use std::string to represent strings, and save yourself all the trouble. This could look like this:

struct parsedString{
    std::string chunk1;
    std::string chunk2;
    std::string chunk3;
};

parsedString parseMe(const std::stirng & stringToParse) {
    parsedString result;

    // just an example - this assigns the first two characters of stringToParse 
    // to chunk1
    result.chunk1 = stringToParse.substr(0, 2); 
    // get the other chunks

    return result; // return the result
}

The above code should illustrate the usage. You can find more information on std::string here.

Upvotes: 1

Related Questions