473,779 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SSCANF

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

Jan 3 '06
22 2964

"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> wrote in message
news:43******** *************** @nan-newsreader-07.noos.net...
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
Jan 3 '06 #11
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 :)

Jan 4 '06 #12
"Superfox il Volpone" <at***@email.it > 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_Keit h) ks***@mib.org <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.
Jan 4 '06 #13
Chris McDonald <ch***@csse.uwa .edu.au> 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)cybers pace.org | don't, I need to know. Flames welcome.
Jan 4 '06 #14
Christopher Benson-Manica said:
Chris McDonald <ch***@csse.uwa .edu.au> 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)
Jan 4 '06 #15
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 :)

Jan 4 '06 #16
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.
Jan 4 '06 #17
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
Jan 4 '06 #18
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@markgordo n-lp ~
$ cat t.c
void foo(char *s)
{
}

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

markg@markgordo n-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@markgordo n-lp ~
$ cat tt.c
void foo(char *s)
{
}

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

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

markg@markgordo n-lp ~
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 4 '06 #19

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

Jan 4 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
473
by: Allan Bruce | last post by:
If I have sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName); and the last string contains spaces, e.g. my complete string "FL:1234ABCD:3:FileName With Spaces.txt\n" does sscanf just make lFileName the string up to the whitespace? even though I tell it the end of string as at the \n ? Thanks
4
4259
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
10
5473
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
4
2030
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
5
6405
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less precise than I had expected:
8
2389
by: Artemio | last post by:
Dear folks, I need some help with using the sscanf() function. I need to parse a string which has several parameters given in a "A=... B=... C=..." way, and each has a different type (one is a text string, another is a decimal, next one is float, etc.). I have GCC 4.0.1 on Mac OS X Tiger. Here is an example of what I am trying to do.
20
21444
by: AMP | last post by:
Hello, Anybody know if anything exists like sscanf in c. I found a few things OL but most were pretty old. Maybe something has come along since 2004? Thanks Mike
5
3506
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I would like to retrieve the "799829" from the data, but it always failed. I thought that the "%*sþ0þ" would work as if I was
7
11630
by: gio | last post by:
suppose I have: .... char str1; char str2; int ret; fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0' ret=sscanf(str1, "%s", str2); ....
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10305
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10137
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7483
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.