473,779 Members | 2,023 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 2963
In article <dj************ *******@fe1.new s.blueyonder.co .uk>
Robert Harris <ro************ *@blueyonder.co .uk> wrote:
... If mese is an array, then mese and &mese are the same thing.


In much the same way that 3 and 3.1415926535897 93 are "the same
thing", i.e., they compare equal when converted to some particular
common type (int, in this case).

The problem is that "&mese" has the wrong type. It is hard to
compare two values of different types (are 3 and 3.14 equal?); the
first step in such a comparison is to choose some additional type
-- perhaps one of the original two, or perhaps a third -- and
convert all the values to the same type, after which they can
finally be compared.

In the case of 3 and 3.14, if the type chosen for comparison is
"int", they are equal. If the type chosen for comparison is
"double", they are not equal. So 3 and 3.14 are equal, and yet
are also not equal.

I use the above example to make it clear that it is not sufficient
to find *a* type under conversion to which comparison shows the
original values as equal: while (int)3 == (int)3.14, I think most
people would say that 3 and 3.14 are *not* equal, and indeed,
(double)3 != (double)3.14.

You would have a much stronger case if you could prove that, for
some large set of C types \elem T, (T)mese == (T)&mese; but I think
this is impossible to prove in "Standard C Virtual Machine", due
to lack of information. (It actually happens to be true on many
real machines, if only because so many real machines have only one
hardware "pointer" type, or at least, only one that is used by C
compilers. The test is more interesting -- not a "degenerate case"
as a mathematician might put it -- when performed on machines with
actual different hardware pointer types, such as a Data General
MV/10000, or a 1960s PR1ME, or some such.)

In any case, Standard C permits an 80x86 C compiler to pass mese
(or &mese[0]) in a register, but put &mese on the stack (or vice
versa), simply because the types differ. If an 80x86 C compiler
did this, calls using the wrong type would in fact fail, even on
the ordinary 80x86. (In this particular case, &mese[0] has type
(char *), but &mese has type (char (*)[5]).)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jan 4 '06 #21
Flash Gordon <sp**@flash-gordon.me.uk> writes:
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.


Perhaps more realistically, byte pointers could be bigger than word
pointers. This could happen on a system where native machine
addresses point to words, and the C implementation needs a word
pointer and an offset to refer to a byte within a word. (The C
implementation for Cray vector machines *almost* does this, but it
puts the offset into the otherwise unused high-order bits of the word
pointer, so a pointer to the first byte of a word still happens to
have the same representation as a pointer to the entire word.)

--
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 #22
Flash Gordon wrote:
Robert Harris wrote:

<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

Sorry, You're right.

Robert
Jan 4 '06 #23

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
21443
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
11629
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
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
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
10302
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...
1
10071
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,...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7478
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
6723
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
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.