Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > SSCANF

Reply
Thread Tools

SSCANF

 
 
Superfox il Volpone
Guest
Posts: n/a
 
      01-03-2006
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

 
Reply With Quote
 
 
 
 
nelu
Guest
Posts: n/a
 
      01-03-2006

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.

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-03-2006
"Superfox il Volpone" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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/publ...ginner_s_c.htm

-Mike


 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      01-03-2006
Mike Wahler wrote:
> "Superfox il Volpone" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
>>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.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      01-03-2006
Mike Wahler <(E-Mail Removed)> 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.
 
Reply With Quote
 
Chris McDonald
Guest
Posts: n/a
 
      01-03-2006
Christopher Benson-Manica <(E-Mail Removed)> writes:

>Mike Wahler <(E-Mail Removed)> 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.
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-03-2006
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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-03-2006

"Michael Mair" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mike Wahler wrote:
>> "Superfox il Volpone" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>>
>>>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


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-03-2006
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
 
Reply With Quote
 
Robert Harris
Guest
Posts: n/a
 
      01-03-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sscanf feature in C++? Matt C++ 15 10-19-2004 04:36 PM
sscanf and c++! Mike C++ 3 06-09-2004 10:46 AM
endianness and sscanf/sprintf pramod C++ 22 01-06-2004 01:02 PM
Re: %n conversion in sscanf/VC++ 6-solved Anonymous C++ 0 10-30-2003 05:05 PM
Re: Unitialized Memory Read In sscanf() Darrell Grainger C Programming 1 06-24-2003 06:52 PM



Advertisments