Thread: Problem with string array

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    15

    Problem with string array

    Hi,

    Im using the code bloks and I have this code and the result is a big character and very rubbish

    someone can help me

    Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
    
    char buffer[8][8];
    int a;
    
    
    strncpy(buffer[0], "   ##   ", 8);
    strncpy(buffer[1], "  #  #  ", 8);
    strncpy(buffer[2], "#      #", 8);
    strncpy(buffer[3], "#      #", 8);
    strncpy(buffer[4], "########", 8);
    strncpy(buffer[5], "#      #", 8);
    strncpy(buffer[6], "#      #", 8);
    strncpy(buffer[7], "#      #", 8);
    
    printf("\n valor: %s",&buffer[0][0]);
    printf("\n valor: %s",&buffer[1][0]);
    printf("\n valor: %s",&buffer[2][0]);
    printf("\n valor: %s",&buffer[3][0]);
    printf("\n valor: %s",&buffer[4][0]);
    printf("\n valor: %s",&buffer[5][0]);
    printf("\n valor: %s",&buffer[6][0]);
    printf("\n valor: %s",&buffer[7][0]);
    
        return 0;
    }
    Result:
    Code:
    valor:    ##     #  #  #      ##      ##########      ##      ##      #( (
     valor:   #  #  #      ##      ##########      ##      ##      #( (
     valor: #      ##      ##########      ##      ##      #( (
     valor: #      ##########      ##      ##      #( (
     valor: #########      ##      ##      #( (
     valor: #      ##      ##      #( (
     valor: #      ##      #( (
     valor: #      #( (
    Process returned 0 (0x0)   execution time : 0.013 s
    Press any key to continue.
    Last edited by totoco; 12-01-2017 at 10:37 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    This happens because 8 bytes aren't enough for those strings, you needs an extra character for the terminator.
    char * strncpy ( char * destination, const char * source, size_t num );
    Copy characters from string
    Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

    No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    15
    Quote Originally Posted by GReaper View Post
    This happens because 8 bytes aren't enough for those strings, you needs an extra character for the terminator.
    I'm testing and work fine. I was change it:

    char buffer[8][9];

    and all lines with: 9

    strncpy(buffer[0], " ## ", 9);

    Thankuou

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string array assignment problem
    By tlinetrader in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2012, 05:50 PM
  2. problem with string and array
    By Methje in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2006, 01:15 PM
  3. string array input problem
    By DoItAllMom115 in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2003, 10:38 AM
  4. string/array problem?
    By Mr_Stuck in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2003, 10:10 AM

Tags for this Thread