SSCANF

Discussion in 'C Programming' started by Superfox il Volpone, Jan 3, 2006.

  1. Hello

    I have some problem with sscanf, I tryed this code but it doesn't works
    :

    char* stringa = "18/2005"
    char mese[3]; char anno[5];
    int i_letture;

    i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
    mese[2] = anno[4] = '\0';

    The values ar random and I don't understand the motive
    I tryed either this variant :

    char* porka_vakka = strchr(stringa, '/');
    *porka_vakka = '\0'; // but either ' ' e '\n'
    i_letture = sscanf(stringa, "%s", &mese);

    But neither this works...

    Could anyone help me ?

    Bye
    - Atari

    p.s. happy new year :)
     
    Superfox il Volpone, Jan 3, 2006
    #1
    1. Advertising

  2. Superfox il Volpone

    nelu Guest

    Superfox il Volpone wrote:
    > Hello
    >
    > I have some problem with sscanf, I tryed this code but it doesn't works
    > :
    >
    > char* stringa = "18/2005"
    > char mese[3]; char anno[5];
    > int i_letture;
    >
    > i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
    > mese[2] = anno[4] = '\0';


    You have mese[3] and anno[5]. When you read with sscanf you tell it to
    store the character arrays in &mese and &anno. mese=&mese[0] and
    anno=&anno[0].
    It should be: i_letture=sscanf(stringa,"%2s/%4s",mese,anno), otherwise
    you are trying to write those values at locations that hold the
    addresses of mese and anno instead of writing them to the addresses
    where mese and anno point.
    If you really want to use & then write:
    i_letture=sscanf(stringa,"%2s/%4s",&mese[0],&anno[0]);

    >
    > The values ar random and I don't understand the motive
    > I tryed either this variant :
    >
    > char* porka_vakka = strchr(stringa, '/');
    > *porka_vakka = '\0'; // but either ' ' e '\n'
    > i_letture = sscanf(stringa, "%s", &mese);

    Same problem as above. Reading into the wrong location.
     
    nelu, Jan 3, 2006
    #2
    1. Advertising

  3. Superfox il Volpone

    Mike Wahler Guest

    "Superfox il Volpone" <> wrote in message
    news:...
    > Hello
    >
    > I have some problem with sscanf, I tryed this code but it doesn't works
    > :
    >
    > char* stringa = "18/2005"


    "18/2005" is a string literal. Any attempts to modify
    any of its characters produces undefined behavior.

    > char mese[3]; char anno[5];
    > int i_letture;
    >
    > i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);


    This gives undefined behavior. You're trying to modify
    a string literal.

    Also, since 'mese' and 'anno' are arrays, their names
    in this context will evalutate to pointers to their
    first element. So e.g. use 'mese', not '&mese'.
    %s must match with type 'char*'. The expression &mese
    does not have that type. Its type is (*)[3] (pointer to
    array of three char). Not the correct type.

    > mese[2] = anno[4] = '\0';


    'sscanf()' already applies the string terminator for you
    (in the proper location)

    Also (disregarding for now the string literal problem)
    note that if the size of data stored by 'sscanf()' is
    less than the size of the array, your arbitrary placement of
    '\0' as the last array element will not terminate the string
    properly (there will be 'garbage' between the data and the
    terminator).

    >
    > The values ar random and I don't understand the motive


    You don't understand how 'sscanf()' works, or how arrays
    and pointers work.

    > I tryed either this variant :
    >
    > char* porka_vakka = strchr(stringa, '/');
    > *porka_vakka = '\0'; // but either ' ' e '\n'


    There are two possible results of these two lines, both of
    which are undefined behavior:

    1) The character '/' is not found in the string (which
    causes 'strchr()' to return NULL), in which case you
    try to dereference a NULL pointer. Undefined behavior.

    2) The character '/' is found (and 'strchr()' returns its
    address. You then try to modify it, but it's part of
    a string literal. Undefined behavior.

    > i_letture = sscanf(stringa, "%s", &mese);


    More undefined behavior. Attemt to modify string literal.
    Wrong data type used with '%s'.

    Finally, even if you do have writable storage for 'sscanf()'
    note that you have no protection against the data overflowing
    your array. Look up the 'width' flag for sscanf() format
    specifiers.

    >
    > But neither this works...
    >
    > Could anyone help me ?


    I think the best advice I can give is to recommend you get
    some good textbooks.
    http://www.accu.org/bookreviews/public/reviews/0sb/beginner_s_c.htm

    -Mike
     
    Mike Wahler, Jan 3, 2006
    #3
  4. Superfox il Volpone

    Michael Mair Guest

    Mike Wahler wrote:
    > "Superfox il Volpone" <> wrote in message
    > news:...
    >
    >>Hello
    >>
    >>I have some problem with sscanf, I tryed this code but it doesn't works
    >>:
    >>
    >>char* stringa = "18/2005"

    >
    > "18/2005" is a string literal. Any attempts to modify
    > any of its characters produces undefined behavior.
    >
    >>char mese[3]; char anno[5];
    >>int i_letture;
    >>
    >>i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);

    >
    > This gives undefined behavior. You're trying to modify
    > a string literal.


    Look again.
    The first parameter of sscanf() is of type const char *
    (qualified by restrict in C99 IIRC).

    I guess you saw sscanf() and thought sprintf()...

    <snip>


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Jan 3, 2006
    #4
  5. Mike Wahler <> wrote:

    > > i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);


    > This gives undefined behavior. You're trying to modify
    > a string literal.


    I don't know that OP is "trying" to modify the string literal. In
    practice, is sscanf() really likely to modify its first argument?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jan 3, 2006
    #5
  6. Christopher Benson-Manica <> writes:

    >Mike Wahler <> wrote:


    >> > i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);


    >> This gives undefined behavior. You're trying to modify
    >> a string literal.


    >I don't know that OP is "trying" to modify the string literal. In
    >practice, is sscanf() really likely to modify its first argument?



    Doesn't the prototype of sscanf() promise to *not* modify its first argument?

    --
    Chris.
     
    Chris McDonald, Jan 3, 2006
    #6
  7. Superfox il Volpone a écrit :
    > I have some problem with sscanf, I tryed this code but it doesn't works
    > :
    >
    > char* stringa = "18/2005"
    > char mese[3]; char anno[5];


    These are arrays of char

    > int i_letture;
    >
    > i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);


    why & ? %s expects exactly what the name of an array of char is : the
    address of its first element.

    The separator must be read too :

    #include <stdio.h>

    int main(void)
    {
    char* stringa = "18/2005";
    char mese[3];
    char anno[5];
    char c;
    int i_letture = sscanf(stringa, "%2s%c%4s", mese, &c, anno);

    if (i_letture == 3)
    {
    printf ("'%s' '%s'\n", mese, anno);
    }
    else
    {
    printf ("sscanf() error\n");
    }
    return 0;
    }

    > mese[2] = anno[4] = '\0';


    No need for that.

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 3, 2006
    #7
  8. Superfox il Volpone

    Mike Wahler Guest

    "Michael Mair" <> wrote in message
    news:...
    > Mike Wahler wrote:
    >> "Superfox il Volpone" <> wrote in message
    >> news:...
    >>
    >>>Hello
    >>>
    >>>I have some problem with sscanf, I tryed this code but it doesn't works
    >>>:
    >>>
    >>>char* stringa = "18/2005"

    >>
    >> "18/2005" is a string literal. Any attempts to modify
    >> any of its characters produces undefined behavior.
    >>
    >>>char mese[3]; char anno[5];
    >>>int i_letture;
    >>>
    >>>i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);

    >>
    >> This gives undefined behavior. You're trying to modify
    >> a string literal.

    >
    > Look again.
    > The first parameter of sscanf() is of type const char *
    > (qualified by restrict in C99 IIRC).
    >
    > I guess you saw sscanf() and thought sprintf()...


    Um, yes. Oops. Blush. Sorry. Er, Happy New Year and all that. :)

    -Mike
     
    Mike Wahler, Jan 3, 2006
    #8
  9. Mike Wahler a écrit :
    >>char* stringa = "18/2005"

    >
    > "18/2005" is a string literal. Any attempts to modify
    > any of its characters produces undefined behavior.


    How would it be modified ? I'm curious. Isn't the first parameter of
    sscanf() a char const * ?

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 3, 2006
    #9
  10. Superfox il Volpone wrote:
    > Hello
    >
    > I have some problem with sscanf, I tryed this code but it doesn't works
    > :
    >
    > char* stringa = "18/2005"

    Missing ';' termination
    > char mese[3]; char anno[5];
    > int i_letture;
    >
    > i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
    > mese[2] = anno[4] = '\0';

    Unnecessary here: sscanf NUL-terminates the results
    >
    > The values ar random and I don't understand the motive


    Well, it works for me! What makes you think the result is wrong?

    Robert
     
    Robert Harris, Jan 3, 2006
    #10
  11. Superfox il Volpone

    Mike Wahler Guest

    "Emmanuel Delahaye" <> wrote in message
    news:43bae22d$0$15331$...
    > Mike Wahler a écrit :
    >>>char* stringa = "18/2005"

    >>
    >> "18/2005" is a string literal. Any attempts to modify
    >> any of its characters produces undefined behavior.

    >
    > How would it be modified ? I'm curious. Isn't the first parameter of
    > sscanf() a char const * ?


    Brain malfunction. Read 'sscanf', saw 'sprintf'.
    My apologies.

    -Mike
     
    Mike Wahler, Jan 3, 2006
    #11
  12. Sorry people, what I posted it's part of a more large program and the
    printf with I verified it was wrong :)

    I solved but I don't understand one thing :
    the sscanf works with '&mese' and 'mese' in the correct manner : maybe
    it's the compiler (GCC) that does some correction ?

    The second thing if I want to jump one argument is it correct the
    behaviour ?
    year[5]
    sscanf(str_date, "%*s/%4s", year);

    and at last, at year will be placed the string terminator ('\0')

    Bye & thx all for the help
    ~ Superfox il Volpone :)
     
    Superfox il Volpone, Jan 4, 2006
    #12
  13. "Superfox il Volpone" <> writes:
    > Sorry people, what I posted it's part of a more large program and the
    > printf with I verified it was wrong :)


    For context, here's the code you posted:

    char* stringa = "18/2005"
    char mese[3]; char anno[5];
    int i_letture;

    i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
    mese[2] = anno[4] = '\0';

    Please read <http://cfaj.freeshell.org/google/>.

    This also demonstrates why you should always post a small, complete,
    compilable program. We can't guess whether the part you didn't post
    is what's causing the problem (in this case, it was). What you posted
    wasn't even a correct code fragment; the declaration of stringa is
    missing a semicolon, an error that the compiler would have caught.
    Don't try to re-type your code; copy-and-paste *exactly* what you fed
    to the compiler.

    > I solved but I don't understand one thing :
    > the sscanf works with '&mese' and 'mese' in the correct manner : maybe
    > it's the compiler (GCC) that does some correction ?


    Given, for example,

    char anno[5];

    the expression "anno", an array name, is implicitly converted (in most
    contexts) to a pointer to the array's first element; in this case, the
    resulting expression is of type char*. The expression "&anno" yields
    the address of the array; the resulting expression is of type
    char (*p)[5], i.e., a pointer to an array of 5 chars.

    These two expressions are of different types, but they're both
    pointers, they both (in some sense) have the same value, and they
    *probably* both have the same representation.

    Since sscanf() takes a variable number and type(s) of arguments, the
    compiler doesn't know what types are expected for the third and fourth
    arguments in your call; it just blindly passes in whatever you
    specify. It's your job to make sure you call it correctly. Since the
    arguments you gave it were (probably) the same size and representation
    as the correct arguments, it happened to work. (Passing something of
    an incorrect type to sscanf() actually invokes undefined behavior;
    working "correctly" is one of the many possible consequences.)

    BTW, I believe there are real systems where char* and char (*p)5 would
    have different representations, and your sscanf() call would fail.

    > The second thing if I want to jump one argument is it correct the
    > behaviour ?
    > year[5]
    > sscanf(str_date, "%*s/%4s", year);
    >
    > and at last, at year will be placed the string terminator ('\0')


    Yes, that should work (assuming the declaration is "char year[5]"
    rather than "year[5]").

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 4, 2006
    #13
  14. Chris McDonald <> wrote:

    > Doesn't the prototype of sscanf() promise to *not* modify its first argument?


    I would have sworn that my K&R2 at work gave the type of sscanf()'s
    first argument as char*, but I see from n869 that either I or K&R2 am
    mistaken. Apologies. It still begs the question of how it can modify
    a string literal, however.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jan 4, 2006
    #14
  15. Christopher Benson-Manica said:

    > Chris McDonald <> wrote:
    >
    >> Doesn't the prototype of sscanf() promise to *not* modify its first
    >> argument?

    >
    > I would have sworn that my K&R2 at work gave the type of sscanf()'s
    > first argument as char*,


    It does. It's listed in the errata.

    > but I see from n869 that either I or K&R2 am
    > mistaken. Apologies. It still begs the question of how it can modify
    > a string literal, however.


    No, it doesn't beg the question. "To beg the question" means "to assume as
    an implicit premise something you are seeking to prove".

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jan 4, 2006
    #15
  16. Superfox il Volpone wrote:
    > Sorry people, what I posted it's part of a more large program and the
    > printf with I verified it was wrong :)
    >
    > I solved but I don't understand one thing :
    > the sscanf works with '&mese' and 'mese' in the correct manner : maybe
    > it's the compiler (GCC) that does some correction ?

    No. If mese is an array, then mese and &mese are the same thing.
    >
    > The second thing if I want to jump one argument is it correct the
    > behaviour ?
    > year[5]
    > sscanf(str_date, "%*s/%4s", year);

    Yes, if you change your format string to "%*2s/%4s"
    >
    > and at last, at year will be placed the string terminator ('\0')

    mese and year will have been terminated with '\0' in any case.
    >
    > Bye & thx all for the help
    > ~ Superfox il Volpone :)
    >
     
    Robert Harris, Jan 4, 2006
    #16
  17. Superfox il Volpone

    Flash Gordon Guest

    Robert Harris wrote:
    > Superfox il Volpone wrote:
    >> Sorry people, what I posted it's part of a more large program and the
    >> printf with I verified it was wrong :)
    >>
    >> I solved but I don't understand one thing :
    >> the sscanf works with '&mese' and 'mese' in the correct manner : maybe
    >> it's the compiler (GCC) that does some correction ?

    > No. If mese is an array, then mese and &mese are the same thing.


    <snip>

    No they are not, they have different types. As a result of this, passing
    the wrong one as one of the varidac parameters to sscanf *could* cause
    it to fail, although I'm not aware of any implementations on which it
    would. The failure could occur if pointer to array of char used a
    different representation to pointer to char (say, an implementation
    encoded the size of the array in pointer to array of char but not in
    pointer to char). It will also cause the compiler to complain at you if
    you pass a pointer to array of char to a function expecting a pointer to
    char.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jan 4, 2006
    #17
  18. Flash Gordon wrote:
    > Robert Harris wrote:
    >
    >> Superfox il Volpone wrote:
    >>
    >>> Sorry people, what I posted it's part of a more large program and the
    >>> printf with I verified it was wrong :)
    >>>
    >>> I solved but I don't understand one thing :
    >>> the sscanf works with '&mese' and 'mese' in the correct manner : maybe
    >>> it's the compiler (GCC) that does some correction ?

    >>
    >> No. If mese is an array, then mese and &mese are the same thing.

    >
    >
    > <snip>
    >
    > No they are not, they have different types. As a result of this, passing
    > the wrong one as one of the varidac parameters to sscanf *could* cause
    > it to fail, although I'm not aware of any implementations on which it
    > would. The failure could occur if pointer to array of char used a
    > different representation to pointer to char (say, an implementation
    > encoded the size of the array in pointer to array of char but not in
    > pointer to char). It will also cause the compiler to complain at you if
    > you pass a pointer to array of char to a function expecting a pointer to
    > char.

    Yes they are. For the array mese passed as a parameter, paragraph
    6.3.2.1 of the C standard applies, and I quote:

    'Except when it is used as an operand of the sizeof operator or the
    unary & operator, or is a string literal used to initialize an array, an
    expression that has type "array of type" is convered to an expression
    with type "pointer to type" that points to the initial element of the
    array object ...'

    While for &mese passed as a parameter, paragraph 6.5.3.2 applies:

    'The unary & operation returns the address of its operand. If the
    operand has type "type", the result has type "pointer to type".'

    So they are the same (in the context of being passed as parameters to a
    function).

    Robert
     
    Robert Harris, Jan 4, 2006
    #18
  19. Superfox il Volpone

    Flash Gordon Guest

    Robert Harris wrote:
    > Flash Gordon wrote:
    >> Robert Harris wrote:
    >>
    >>> Superfox il Volpone wrote:
    >>>
    >>>> Sorry people, what I posted it's part of a more large program and the
    >>>> printf with I verified it was wrong :)
    >>>>
    >>>> I solved but I don't understand one thing :
    >>>> the sscanf works with '&mese' and 'mese' in the correct manner : maybe
    >>>> it's the compiler (GCC) that does some correction ?
    >>>
    >>> No. If mese is an array, then mese and &mese are the same thing.

    >>
    >> <snip>
    >>
    >> No they are not, they have different types. As a result of this,
    >> passing the wrong one as one of the varidac parameters to sscanf
    >> *could* cause it to fail, although I'm not aware of any
    >> implementations on which it would. The failure could occur if pointer
    >> to array of char used a different representation to pointer to char
    >> (say, an implementation encoded the size of the array in pointer to
    >> array of char but not in pointer to char). It will also cause the
    >> compiler to complain at you if you pass a pointer to array of char to
    >> a function expecting a pointer to char.

    > Yes they are. For the array mese passed as a parameter, paragraph
    > 6.3.2.1 of the C standard applies, and I quote:
    >
    > 'Except when it is used as an operand of the sizeof operator or the
    > unary & operator, or is a string literal used to initialize an array, an

    ^^^^^^^^^^^^^^^^
    > expression that has type "array of type" is convered to an expression
    > with type "pointer to type" that points to the initial element of the
    > array object ...'


    So it is still of array type when operated on by the & operator.

    > While for &mese passed as a parameter, paragraph 6.5.3.2 applies:
    >
    > 'The unary & operation returns the address of its operand. If the
    > operand has type "type", the result has type "pointer to type".'


    So the type above is array of whatever. So the type returned by & is
    pointer to array of whatever.

    > So they are the same (in the context of being passed as parameters to a
    > function).


    No, see above.

    The addresses are the same, but the types are not. So, for example, we
    get from gcc:
    markg@markgordon-lp ~
    $ cat t.c
    void foo(char *s)
    {
    }

    int main(void)
    {
    char fred[10];
    foo(fred);
    foo(&fred);
    }

    markg@markgordon-lp ~
    $ gcc -ansi -pedantic -O t.c
    t.c: In function `main':
    t.c:9: warning: passing arg 1 of `foo' from incompatible pointer type

    markg@markgordon-lp ~
    $ cat tt.c
    void foo(char *s)
    {
    }

    int main(void)
    {
    char fred[10];
    foo(fred);
    /* foo(&fred); */
    }

    markg@markgordon-lp ~
    $ gcc -ansi -pedantic -O tt.c

    markg@markgordon-lp ~
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jan 4, 2006
    #19
  20. Superfox il Volpone

    nelu Guest

    Robert Harris wrote:
    > Yes they are. For the array mese passed as a parameter, paragraph
    > 6.3.2.1 of the C standard applies, and I quote:
    >
    > 'Except when it is used as an operand of the sizeof operator or the
    > unary & operator, or is a string literal used to initialize an array, an
    > expression that has type "array of type" is convered to an expression
    > with type "pointer to type" that points to the initial element of the
    > array object ...'

    meaning
    char *var
    and
    char var[]
    are considered the same in this case. It does not say char var[] is the
    same as char *var[].

    >
    > While for &mese passed as a parameter, paragraph 6.5.3.2 applies:
    >
    > 'The unary & operation returns the address of its operand. If the
    > operand has type "type", the result has type "pointer to type".'

    Exactly.
    If the operand has type the result has type *type, thus:
    char argv[] -> char *arg[]

    > So they are the same (in the context of being passed as parameters to a
    > function).

    No
     
    nelu, Jan 4, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    0
    Views:
    486
    Anonymous
    Oct 30, 2003
  2. pramod
    Replies:
    22
    Views:
    1,859
    Lew Pitcher
    Jan 6, 2004
  3. Mike

    sscanf and c++!

    Mike, Jun 8, 2004, in forum: C++
    Replies:
    3
    Views:
    3,563
  4. Matt

    sscanf feature in C++?

    Matt, Oct 18, 2004, in forum: C++
    Replies:
    15
    Views:
    19,035
    Default User
    Oct 19, 2004
  5. Darrell Grainger

    Re: Unitialized Memory Read In sscanf()

    Darrell Grainger, Jun 24, 2003, in forum: C Programming
    Replies:
    1
    Views:
    824
    Alan Balmer
    Jun 24, 2003
Loading...

Share This Page