Thread: Getting strange output!

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    27

    Question Getting strange output!

    Could someone explain to me what is going on here? Why does this code:

    Code:
    char[6] startText;
    
    char data[] = "1.1\nWord\nWord\n0-5\n\0";
    char* dataPtr = data;
    char** objectData2 = &dataPtr;
    
    
    std::cout << "Data:" << *objectData2 << std::endl;
    
    
    char null[1];
    null[0] = '\0';
                    
    strncpy(startText, *objectData2, 5);
    strcat(startText, null);
                    
    std::cout << "startText: " << startText << std::endl;
    Give me this output:

    Data:1.1
    Word
    Word
    0-5


    startText: 1.1
    W00 00-00 00-00 00-A


    What is this rubbish at the end of the startText???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to initialise startText.

    EDIT:
    Oh, wait a minute, you used strncpy not strncat on startText, so that removes the problem with failing to initialise it. However, you then attempt to strcat an empty string... before appending the null character. Correct would be:
    Code:
    strncpy(startText, *objectData2, 5);
    startText[5] = '\0';
    Sorry about that, I was thrown off by the strange use of a named empty string.
    Last edited by laserlight; 06-17-2013 at 01:09 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    oh... didn't realise I had to do it as I was using all 6 chars in it, I thought all the rubbish in the startText would be overwritten...

    but yes, this sorted the problem:

    Code:
    for (int i = 0; i < 6; i++) {
        startText[i] = '\0';
    }


    However, it looks a bit awkward - is this a good way to initialise a char array?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You only needed to write:
    Code:
    char startText[6] = "";
    Note that your syntax was wrong. Also, is this supposed to be C or C++?

    Besides this, refer to my edit of post #2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    Quote Originally Posted by laserlight View Post
    You forgot to initialise startText.

    EDIT:
    Oh, wait a minute, you used strncpy not strncat on startText, so that removes the problem with failing to initialise it. However, you then attempt to strcat an empty string... before appending the null character. Correct would be:
    Code:
    strncpy(startText, *objectData2, 5);
    startText[5] = '\0';
    Sorry about that, I was thrown off by the strange use of a named empty string.

    oh right.... isn't appending a null character the same as strcat-int an empty string?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mondaytofriday
    oh right.... isn't appending a null character the same as strcat-int an empty string?
    No, strcat concatenates by finding the null character and then appending to the string from that point. As such, you cannot use strcat to append a null character when there is none.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    oh! gotcha thanks

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    this is C with some C++ functionality...

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Huh, what does that mean? If it contains "C++ functionality" in the same code, then it is C++, not C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    What would I do then in this case:

    Code:
    char* endText;
    endText = strstr(*objectData2, " 5000");
    I don't know how long endText would be, but I need to append '\0' to the end of it. I was going to strcat a char array with one null character in it to endText, but I guess this is a bad idea again....

  11. #11
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    Well I can use things like std::cout in my code for now, but will need to remove them later for the final build, as the final product will need to run on a C-only system.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mondaytofriday
    I don't know how long endText would be, but I need to append '\0' to the end of it.
    No, you don't. strstr returns a pointer to somewhere within the search that you are searching, hence it is also null terminated.

    Quote Originally Posted by mondaytofriday
    Well I can use things like std::cout in my code for now, but will need to remove them later for the final build, as the final product will need to run on a C-only system.
    Why not just use C facilities then, e.g., printf?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    Quote Originally Posted by laserlight View Post
    No, you don't. strstr returns a pointer to somewhere within the search that you are searching, hence it is also null terminated.


    Why not just use C facilities then, e.g., printf?

    I do use printf in other places, just prefer std::cout for temporary logging as I used to use it in my happy C++ days

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by mondaytofriday View Post
    I don't know how long endText would be, but I need to append '\0' to the end of it.
    Well, you have a contradiction here.
    Either you know where "endText" should end or it is impossible to append a '\0'.

    If you tell us what you want to achieve, perhaps someone will find a good solution.
    Describe the goal, not the step.

    Bye, Andreas

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    This post needs to be moved to C++ forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange Output
    By jokes_finder in forum C Programming
    Replies: 1
    Last Post: 05-13-2011, 07:44 AM
  2. Strange Output
    By ThLstN in forum C Programming
    Replies: 2
    Last Post: 04-05-2008, 12:27 PM
  3. Strange Output
    By newby01 in forum C Programming
    Replies: 1
    Last Post: 01-21-2008, 04:07 AM
  4. output bit strange
    By onthewaytoC in forum C Programming
    Replies: 5
    Last Post: 06-10-2005, 05:36 AM
  5. Strange output ---need help
    By jack_2205 in forum C Programming
    Replies: 7
    Last Post: 02-21-2002, 07:07 PM

Tags for this Thread