-1

I am trying to understand how sscanf() works. I ran some examples from this page: http://docs.roxen.com/pike/7.0/tutorial/strings/sscanf.xml and they don't work on my platform. I can't understand why. For instance: "sscanf("4711bar", "%d%s", a, b);" makes the program exit with an error...

Here is one of the examples that work: "sscanf("foo", "f%s", a);". Does anybody know why? Do they work on your platforms? Thank you.

This is my code:

 int main(void){

   char *b = (char*)malloc(sizeof(char)*100);       
   int a = 0;

   sscanf("4711bar", "%d%s", a, b);
   printf("%d", a);
   printf("%s", b);
 }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Carmen Cojocaru
  • 333
  • 1
  • 7
  • 16
  • I bet you don't pass valid pointers to variables... –  Nov 09 '13 at 20:08
  • 3
    The documentation you are looking at **is not for C or C++ but for an entirely different language**. – n. m. could be an AI Nov 09 '13 at 20:12
  • Also, [don't cast the return value of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). –  Nov 09 '13 at 20:15
  • @CarmenCojocaru Because it is an error to do so, and you don't want to have errors in your code (care to read the answer behind the link?) –  Nov 09 '13 at 20:22
  • @H2CO3 This comes from the same article: "True. However, in C++ the cast is required, so if you want your code to work in **both**, you'll have to compromise. But in pure C, don't do the cast for the reasons you stated. " – Carmen Cojocaru Nov 09 '13 at 20:25
  • @CarmenCojocaru But no sane programmer uses `malloc()` in C++. In C++, one uses `vector` or at most `operator new[]`. If you *need* the cast, that indicates that you are doing something terribly wrong. –  Nov 09 '13 at 20:26

2 Answers2

1

You should write: sscanf("4711bar", "%d%s", &a, b); The & makes &a is a pointer to a and sscanf requires pointers.

The second example works, because a is an array. And array a of type char[100] is implictly converted to pointer of type char * pointing to the array's first element (a[0]). So a is the same as &(a[0])

int a;
char b[100];
sscanf("4711bar", "%d%s", &a, b);

-

char a[100];
sscanf("foo", "%s", a);
wkordalski
  • 938
  • 8
  • 9
  • Why it doesn't work with a pointer to char, like char *b and it does with an array of chars? – Carmen Cojocaru Nov 09 '13 at 20:22
  • "And name of array is pointer to its first element." - it isn't, but it is **implicitly converted into** a pointer to its first element. –  Nov 09 '13 at 20:24
  • @CarmenCojocaru Because the uninitialized pointer doesn't point to any valid memory. –  Nov 09 '13 at 20:24
  • @Zifre No problem, it's a mistake easy to make. (At least you didn't insist! How many people do...) –  Nov 09 '13 at 20:29
0

You should pass valid pointers to sscanf, &a for integer and b for string:

sscanf("4711bar", "%d%s", &a, b);
                          ^

Moreover, your code is C-ish not C++. Using C++ it should looks something like:

std::stringstream s;
s << "4711bar";

int a;
std::string b;

s >> a >> b;
masoud
  • 55,379
  • 16
  • 141
  • 208