Diffuculty with 2D arrays and print,fprintf

This is a discussion on Diffuculty with 2D arrays and print,fprintf within the C Programming forums, part of the General Programming Boards category; Hi, I am having diffuculty with 2D arrays and printf, I am using a 3*17 array to store 3 strings ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    6

    Question Diffuculty with 2D arrays and print,fprintf

    Hi,
    I am having diffuculty with 2D arrays and printf, I am using a 3*17 array to store 3 strings 17 characters long. When I use printf it prints all 3 strings e.g.
    Code:
    for(j=0;j<3;j++)
    printf("error: %s\n",error[j]);
    The desired output would be someting like
    error: 100000000000000000
    error: 010000000000000000
    error: 001000000000000000

    However I get,
    The desired output would be someting like
    error: 10000000000000000001000000000000000000100000000000 0000
    error: 010000000000000000001000000000000000
    error: 001000000000000000
    Any ideas what causes this and how to get what I want,
    Cheers
    I have a simaler problem with fprintf.

  2. #2
    and the hat of mystery Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    31,433
    > I am using a 3*17 array to store 3 strings 17 characters long.
    A string with 17 chars needs 18 bytes to store it, if you want to add a \0 on the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,299
    A C-string is a char array whose final element is '\0'.

    A char array could be just any sequence of characters. So a C-string is a specific form of array (it's "null terminated").
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    I tried adding the extra null character by:
    Code:
    for(j=0;j<3;j++)
    error[j][18]= '\0';
    and the output was
    error: 10000000000000000
    error: 01000000000000000|}01000000000000000
    error: 001000000000000000|}

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,299
    A seventeen byte array begins with [0] and ends with [16].
    An eighteen byte array begins with [0] and ends with [17].
    A nineteen byte array begins with [0] and ends with [18].

    If you printf an array with a '\0' in it as a %s, the output will end at the '\0'. There is no other possibility, besides a computer possessed by aliens, etc.

    Maybe you should write as short an example of this as you can (you should be able to do it in < 10 lines) and then post the code so we can see what you are doing wrong.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    Your right I should have written
    Code:
    for(j=0;j<3;j++)
    error[j][17]= '\0';
    That solves my problem and makes sense; Cheers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21