Null issues raised by scanf

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michael8808
    New Member
    • Apr 2010
    • 3

    Null issues raised by scanf

    Hello everyone,

    I am a beginer of C programming and have a short question here.
    I declared my variables as bellow in the main method.

    char a[10];
    char b[25];
    char c[25];
    char d[5];
    char e[3];

    scanf("%s%s%s%s %s",a,b,c,d,e );

    printf("%s\t%s\ t%s\t%s\t%s\t", a,b,c,d,e);


    when I do the scanf, my inputs are
    a
    b
    c
    A80
    P01

    (I have them in the same line when I input the values, here just for make them more clearly to read.)


    My problem is that after the printf() executed, the outputs are
    a
    b

    A80
    P01.

    The variable c is supposed not to be null. It should have its own value, which is 'c' in this case. I am wondering what causes the null pointer and what is the solution?

    Your help will be mostly gratitude. Thanks all in advance.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The e array is not large enough to hold the entered string. The resulting overflow leads to undefined behavior.

    Follow this convention to prevent buffer overflow:
    Code:
    char e[3];
    ...
    scanf("%2s",e);
    Where the field width is one less than the size of the buffer (to allow for the terminating null character).

    Comment

    • Michael8808
      New Member
      • Apr 2010
      • 3

      #3
      Thanks for your answer.
      However, it doest not solve my problem. Actually, my array e works properly, i think, as it contains the string which i input. (in this case, it's P01) The issue is in array C, for nothing is in it. Any new ideas?

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Suppose the compiler organizes memory such that the c array immediately follows the e array. Four characters ('P', '0', '1', and '\0') are written into the e array, but since it is only large enough to hold three characters, the last one overflows into the next variable. Thus, a null character is written into the first slot of the c array.

        I don't know if that is what's happening, but it is plausible and it would explain your symptoms. Fix the overflows. If you still have problems then we'll keep investigating.

        Comment

        • Michael8808
          New Member
          • Apr 2010
          • 3

          #5
          yeah.. i think u r definitely right. I fixed the problem by set all the sizes of array to 80, though i think this may be kind of a waste of memory.
          By the way, array in c language, especially in char[] type is so different from other languages'. Sometimes, i am sure that there should be only 10 elements in the array but i have to initialize the size of array to be a very large number, for instance 30. Otherwise, fragmentation faults will be raised. Do u know exactly how to exactly set the array size according to a specific need rather than just giving a big number for considerations of safety.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            When reading in your input with scanf, set width modifiers for the maximum amount of input allowed in.

            Reference: http://www.cplusplus.com/reference/c.../cstdio/scanf/
            Example: http://www.tenouk.com/clabworksheet/labworksheet6.html
            Garbage in, garbage out.

            Comment

            Working...