SSCANF

  • Thread starter Superfox il Volpone
  • Start date
S

Superfox il Volpone

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 :)
 
N

nelu

Superfox said:
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.
 
M

Mike Wahler

Superfox il Volpone said:
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
 
M

Michael Mair

Mike said:
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
 
C

Christopher Benson-Manica

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?
 
C

Chris McDonald

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?
 
E

Emmanuel Delahaye

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.
 
M

Mike Wahler

Michael Mair said:
Mike said:
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
 
E

Emmanuel Delahaye

Mike Wahler a écrit :
"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 * ?
 
R

Robert Harris

Superfox said:
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
 
M

Mike Wahler

Emmanuel Delahaye said:
Mike Wahler a écrit :

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
 
S

Superfox il Volpone

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 :)
 
K

Keith Thompson

Superfox il Volpone said:
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]").
 
C

Christopher Benson-Manica

Chris McDonald said:
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.
 
R

Richard Heathfield

Christopher Benson-Manica said:
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".
 
R

Robert Harris

Superfox said:
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.
 
F

Flash Gordon

Robert said:
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.
 
R

Robert Harris

Flash said:
<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
 
F

Flash Gordon

Robert said:
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 ~
 
N

nelu

Robert said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top