sscanf question

Z

Zach

What does the "%*s" in the sscanf call mean? Also when reading in the
values why does the int variable have the address-of operator (&) but
the char array str does not?

/* sscanf example */
#include <stdio.h>

int main (void)
{
char sentence []="Rudolph is 12 years old";
char str [20];
int i;

sscanf (sentence,"%s %*s %d",str,&i);
printf ("%s -> %d\n",str,i);

return 0;
}

Zach
 
Z

Zach

Two additional questions:

In the sscanf call what does this mean: "%31[^;]%n"
I am familiar with %n but not %31 nor what the [^;] indicates.

Also why is there not a semicolon after the sscanf call inside the
whale statement? I tried adding one but it then causes an error. Is
this defined somewhere in the C99 standard?


#include <stdio.h>

int main(void)
{
const char line[] = "2004/12/03 12:01:59;info1;info2;info3";
const char *ptr = line;
char field [ 32 ];
int n;
while ( sscanf(ptr, "%31[^;]%n", field, &n) == 1 )
{
printf("field = \"%s\"\n", field);
ptr += n; /* advance the pointer by the number of characters
read */
if ( *ptr != ';' )
{
break; /* didn't find an expected delimiter, done? */
}
/* to get around successive delimeters such as ";;" */
while ( *ptr == ';' )
{
++ptr; /* skip the delimiter */
}
}
return 0;
}

Zach
 
B

Barry Schwarz

What does the "%*s" in the sscanf call mean?  Also when reading in the
values why does the int variable have the address-of operator (&) but
the char array str does not?

/* sscanf example */
#include <stdio.h>

int main (void)
{
  char sentence []="Rudolph is 12 years old";
  char str [20];
  int i;

  sscanf (sentence,"%s %*s %d",str,&i);
  printf ("%s -> %d\n",str,i);

  return 0;

}

If these questions are not addressed in your current C reference, you
need to get a better one.

The * modifier to a format means "discard the result without
attempting to store that result."

str is an array. With only a few exceptions which don't apply here, an
expression of type array is converted to the address of the first
element of the array with type pointer to element type. Thus, the
third argument in the call to scanf is converted to the address of str
[0] with type pointer to char. Basically, str in this context is
being treated as if the argument were &str[0].

If the argument were coded &str, it would invoke undefined
behavior. This is because the %s format requires the corresponding
argument to have type char* but &str has type char(*)[20]. (The &
operator is one of the exceptions I alluded to above.)
 
B

Barry Schwarz

Two additional questions:

In the sscanf call what does this mean: "%31[^;]%n"
I am familiar with %n but not %31 nor what the [^;] indicates.

Also why is there not a semicolon after the sscanf call inside the
whale statement? I tried adding one but it then causes an error. Is
this defined somewhere in the C99 standard?

#include <stdio.h>

int main(void)
{
  const char line[] = "2004/12/03 12:01:59;info1;info2;info3";
  const char *ptr = line;
  char field [ 32 ];
  int n;
  while ( sscanf(ptr, "%31[^;]%n", field, &n) == 1 )
    {
      printf("field = \"%s\"\n", field);
      ptr += n; /* advance the pointer by the number of characters
read */
      if ( *ptr != ';' )
        {
          break; /* didn't find an expected delimiter, done? */
        }
      /* to get around successive delimeters such as ";;" */
      while ( *ptr == ';' )
        {
          ++ptr; /* skip the delimiter */
        }
    }
  return 0;

}

Ignoring the special case of the for statement, semi-colons are used
to indicate the end of a statement (or declaration). The call to
scanf in htis case is not a statement but merely an expression that is
part of the while statement. The value that scanf returns will be the
value of this expression and that value will then be compared to 1 to
determine if the body of thewhile loop should be executed or not.
 
B

Bill Cunningham

Han from China - Master Troll said:
Zach said:
Two additional questions:

Sure. Keep 'em coming, bro. The judge made me sit here answering C
questions for my community service.
In the sscanf call what does this mean: "%31[^;]%n"
I am familiar with %n but not %31 nor what the [^;] indicates.

The "%31[^;]" is another conversion specification. The number 31 after the

Han what about the ^ and ; together ?

[snip]
 
A

Antoninus Twink

Han from China - Master Troll said:
The "%31[^;]" is another conversion specification. The number 31 after the

Han what about the ^ and ; together ?

It's just like a regular expression. From the scanf manpage:

[ matches a non-empty sequence of characters from the specified set of
accepted characters; the next pointer must be a pointer to char, and
there must be enough room for all the characters in the string, plus a
terminating null byte. The usual skip of leading white space is
suppressed. The string is to be made up of characters in (or not in) a
particular set; the set is defined by the characters between the open
bracket [ character and a close bracket ] character. The set excludes
those characters if the first character after the open bracket is a
circumflex (^). To include a close bracket in the set, make it the first
character after the open bracket or the circumflex; any other position
will end the set. The hyphen character - is also special; when placed
between two other characters, it adds all intervening characters to the
set. To include a hyphen, make it the last character before the final
close bracket. For instance, [^]0-9-] means the set "everything except
close bracket, zero through nine, and hyphen". The string ends with the
appearance of a character not in the (or, with a circumflex, in) set or
when the field width runs out.
 
G

Guest

If these questions are not addressed in your current C reference, you
need to get a better one.

eg. try K&R or this link

http://www.dinkumware.com/manuals/

--
Nick Keighley

smart pointers are no picnic, as are virtually all
automatic devices with something like "smart",
"simple" or "fast" in their name.
(C++ Frequently Questioned Answers (FQA))
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top