# Thread: Problem understanding the working of sscanf()

1. ## Problem understanding the working of sscanf()

Hi,

I am trying to use sscanf() inside a for() loop to read integers into an array, as shown below.

Code:
```
char numString[100];    /* Numbers supplied by user. */
int number[6] = { 0, 0, 0, 0, 0, 0 };

/* Prompt user for input values. */
printf("Enter a series of six numbers:\n");

/* Capture user number string. */
fgets(numString, sizeof(numString), stdin);

/* Convert digit string to number and store in array. */
for (int i = 0; i < 6; ++i)
{
sscanf(numString, "%d", &number[i]);
}

for (int i = 0; i < 6; i++)
{
printf("The number is %d\n", number[i]);
}```
When I run the above code I get the following output

Enter a series of six numbers:
123 -254 -300 4096 5 -60000
The number is 123
The number is 123
The number is 123
The number is 123
The number is 123
The number is 123

From the above output it would seem that each call to sscanf() in the first for() loop causes sscanf() to start reading from the beginning of the number string entered by the user.

My problem is that I don't understand how to get sscanf() in the first for() loop to advance to the next digit string within the input string. For example, to move from reading 123 to reading -254, and so on. I have(!) done a web search but cannot find anything relating to this particular problem.

(I know that reading integers into an array can be done using scanf() but I remember reading somewhere that scanf() was either not secure or buggy and so should not be used.)

Could some kind soul please provide (or point me towards) a text description of how I can get sscanf() to do this advancing ?

Best regards,

Stuart

2. From the above output it would seem that each call to sscanf() in the first for() loop causes sscanf() to start reading from the beginning of the number string entered by the user.
That is correct.

My problem is that I don't understand how to get sscanf() in the first for() loop to advance to the next digit string within the input string.
Basically you don't. If you want to get multiple values from sscanf() you need to do it by adding specifiers and variables to place the information into.
Code:
`sscanf("%d%d", variable[0], variable[1]);`
(I know that reading integers into an array can be done using scanf() but I remember reading somewhere that scanf() was either not secure or buggy and so should not be used.)
Do you realize that when talking about the "deficiencies" of scanf() you're normally talking about the scanf() series of functions, scanf(), sscanf(), vscanf(), etc?

Could some kind soul please provide (or point me towards) a text description of how I can get sscanf() to do this advancing ?
You can't get sscanf() to advance, however you maybe able to "advance" the string by "removing" the first item by using something like strtok(). However this is, IMO, even more error prone than the basic well defined function scanf().

3. If you want to walk through the string with sscanf, then something like this.
Code:
```  /* Convert digit string to number and store in array. */
char *p = numString;
int pos;
for (int i = 0; i < 6; ++i)
{
sscanf(p, "%d%n", &number[i],&pos);
p += pos;
}```