rashok
rashok

Reputation: 13424

Why sscanf doesn't fill the variable?

I wonder why the value of i is 0 after sscanf has been previously called. I was expecting value of i to be modified.

I am using cygwin gcc in windows 7, version 3.4.4

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    unsigned char a[] = {1, 2, 3, 4, 5};
    int i = 0;

    printf("\nread value %d\n", sscanf(&a, "%d", &i));
    printf("\nvalue is %d\n", i);
    printf("\nbuffer is %s\n", a);
}

output is

read value is 0

value is 0

buffer is some special characters

Upvotes: 0

Views: 3312

Answers (4)

LihO
LihO

Reputation: 42083

There's no unexpected behavior in your code. Note that function sscanf reads data from given string and "returns the number of variables filled". You are passing a char array a as a source to retrieve data from it and not only that it does not contain any digits (1 is not the same as '1') it is also not null-terminated ('\0' is missing).

By "%d" you specify that decimal integer should be read, but there are no decimal integers in the given array thus sscanf doesn't read any numbers and i stays untouched. Function returns 0 indicating that no variable was filled.

Change the declaration of the array you are passing to the sscanf to valid string and it will yield the desired behavior. Try char a[] = "1 2 3".

Upvotes: 1

Levon
Levon

Reputation: 143047

This will give you what you expected.

a is already a pointer, so simply a is sufficient as first parameter for sscanf. It's probably a good idea to put 0 (i.e, \0) as last entry in a since you are passing it off to both sscanf and printf which expect to print a null-terminated string.

I also changed the type of i to unsigned char to match the data your are reading from. 

int main()
{
    unsigned char a[] = {1, 2, 3, 4, 5, 0};
    unsigned char i = 0;

    printf("\nread value %d\n", sscanf(a, "%c", &i));
    printf("\nvalue of i is %d\n", i);
    printf("\nbuffer is %s\n", a);
    return 0;
}

Note: You were previously getting 0 because sscanf failed to "read" any data for you from a.

Upvotes: 0

sepp2k
sepp2k

Reputation: 370132

First of all the first argument to sscanf should be a pointer to char, but you're passing in a pointer to a char array, which is the wrong type. That's only tangential to the question though.

To answer your question: sscanf (like the other *scanf functions, too) only writes a value if the format specifier for that value is matched in the string. So if you have the format specifier "%d", an int will be written to the given int pointer only if the string starts with a number. As you said yourself, your string only contains garbage: the (unprintable) characters with values 1 through 5 followed by whatever's in memory after a. So it doesn't start with a number and thus no number is written.

Upvotes: 2

Laurent W.
Laurent W.

Reputation: 3783

sscanf is for STRINGS, not an array of chars without terminating character \0.

http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/

Read formatted data from string Reads data from str and stores them according to the parameter format into the locations given by the additional arguments. Locations pointed by each additional argument are filled with their corresponding type of value specified in the format string.

Upvotes: 1

Related Questions