Thread: How to fix Printing Errors

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    How to fix Printing Errors

    Hi,
    Simply I'm tried to compile this code which is printing as what's shown in the printf .. I don't know it's displaying me on screen wrong results and don't know what's the logic Error in the code to fix it.
    Code:
    int main()
    {
        char source[10], *des = strcpy(source, "012345678");
        printf("%d\n", sizeof(des));// didn't understand why it's giving me 8 instead of 9?! I'm having a pointer to string "012345678", pointer to string is also string!
        char *dest = (char *)malloc(strlen(source) + 1); // +1 for nill value, right?! I reckon on that.
        for (int i = 1; i <= strlen(source);i++)
            dest[i] = source[i];
        dest[strlen(source) + 1] = '\0';
        printf("dest = %s", dest); // I dont know what's the wrong here its printing me "12345678" and "not 012345678" !!!
    }
    thanks for your help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't need strcpy for this. Use initialisation:
    Code:
    char source[10] = "012345678", *des = source;
    As for your confusion: you invoked sizeof on the pointer named des, so of course you got the result of the number of bytes in a pointer. I don't know why you expect to get 9 rather than 10 though: the underlying array source has 10 chars, so sizeof(source) will result in 10, not 9.

    source is null terminated, so the typical approach for copying into your new dynamic array is just to copy from the char at index 0 (not 1, which is why you noticed '0' missing) until you reach the null character, and just copy that too and end. If you really want to call strlen, call it once and save the result to reuse.
    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
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    You don't need strcpy for this. Use initialisation:
    Code:
    char source[10] = "012345678", *des = source;
    As for your confusion: you invoked sizeof on the pointer named des, so of course you got the result of the number of bytes in a pointer. I don't know why you expect to get 9 rather than 10 though: the underlying array source has 10 chars, so sizeof(source) will result in 10, not 9.
    source is null terminated, so the typical approach for copying into your new dynamic array is just to copy from the char at index 0 (not 1, which is why you noticed '0' missing) until you reach the null character, and just copy that too and end. If you really want to call strlen, call it once and save the result to reuse.
    I didn't understand what you said on "the number of bytes in a pointer", in other words, aren't the byte of pointer is the same byte of the value that's the pointer pointing at?! and is the bytes's size of a pointer regardless to its type constant?!
    and for reaching the null character, is there more sympathetic way than source[i] != '\0'?! thanks !!

  4. #4
    Banned
    Join Date
    Apr 2015
    Posts
    596
    In addition, what I understand from you that the "pointer" is a variable and once I sizeof that pointer, we are getting the bytes that's the pointer itself holding "regardless to what's he pointing at!" .

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Think about sizeof(int). The result of that is going to be the same whether the int has a value of 10 or 10000. Likewise, the result of sizeof(char*) is going to be the same whether the char* points to the first character of a 10 char string or the first character of a 10000 char string.

    What do you mean by "sympathetic" regarding code?
    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

  6. #6
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Think about sizeof(int). The result of that is going to be the same whether the int has a value of 10 or 10000. Likewise, the result of sizeof(char*) is going to be the same whether the char* points to the first character of a 10 char string or the first character of a 10000 char string.

    What do you mean by "sympathetic" regarding code?
    meaning like source !=NULL

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That doesn't make sense to me: what's wrong with source[i] != '\0'?
    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

  8. #8
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Think about sizeof(int). The result of that is going to be the same whether the int has a value of 10 or 10000. Likewise, the result of sizeof(char*) is going to be the same whether the char* points to the first character of a 10 char string or the first character of a 10000 char string.

    What do you mean by "sympathetic" regarding code?
    and is the byte of pointer constant regardless to what's pointing on? meaning sizeof(char*) is the same sizeof(int*) and the same as sizeof(float*) ... etc ...?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Probably, but that's not guaranteed.
    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
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    That doesn't make sense to me: what's wrong with source[i] != '\0'?
    Nothing wrong but they just said here over our forum there's a way to write something more sympathetic to code by using *source != NULL or something like that .. just forget

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    We would use NULL for a null pointer constant because it is a named constant that expresses our intention more clearly than directly using 0. '\0' is already a character constant (even though it is technically of type int).
    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

  12. #12
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Probably, but that's not guaranteed.
    Alright, thanks !
    Moreover, the size of (STRUCT) is depending on what we are declaring inside it, right? meaning if I build a struct like that
    Code:
    struct DENT
    {
       int a;
    }
    ;
    so the sizeof(DENT) will be sizeof (int) only, right?!

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There's no reason for it not to be, but in other cases you may find padding complicates the calculation.
    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

  14. #14
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    There's no reason for it not to be, but in other cases you may find padding complicates the calculation.
    THANKS!
    Another lily question!
    I have seen over here in the forum that we always want to keep the '\0' in ARRAY[strlen(<string>)-1], why specifically -1? we can keep it ARRAY[strlen(<string>)] and its allowed not going to overlapping or something !

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By definition of strlen, the null character is in ARRAY[strlen(<string>)]. It is impossible for it to be in ARRAY[strlen(<string>) - 1].
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-11-2014, 05:41 AM
  2. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  3. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM
  4. Printing
    By John Kull in forum Windows Programming
    Replies: 3
    Last Post: 05-22-2002, 02:38 AM
  5. Printing, Is it possible to..
    By JamMan in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 03:46 PM

Tags for this Thread