Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7617641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:12:02+00:00 2026-05-31T03:12:02+00:00

I want to fill an array with int values which are extracted from a

  • 0

I want to fill an array with int values which are extracted from a buffer. I want to do something like:

char buffer[BUFF_MAX];
int numbers[NUM_MAX];
int i = 0;    

fgets(buffer, BUFF_MAX, stdin);
while(sscanf(buffer, "%d", &numbers[i]) == 1)
{
    ++i;
}

but I am having all kinds of problems using this method. Perhaps it has something to do with the \n in the buffer? It seems like the same value is assigned to every element and if I type in anything greater than around 130, a garbage value is stored.

The user will be typing in something like:

12 543 5 234 9

and I want these values to be stored in numbers[] as 5 different elements. There will be different amounts of numbers in the buffer each time (depending on what the user types in).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:12:03+00:00Added an answer on May 31, 2026 at 3:12 am

    You can find out where sscanf() got to in parsing the string by using the %n conversion specifier, thus:

    char buffer[BUFF_MAX];
    int numbers[NUM_MAX];
    int i = 0;
    int offset = 0;
    int newlen;
    
    if (fgets(buffer, BUFF_MAX, stdin) != 0)
    {
        while (sscanf(buffer + offset, "%d%n", &numbers[i], &newlen) == 1 && i < NUM_MAX)
        {
            ++i;
            offset += newlen;
        }
    }
    

    Testing

    #include <stdio.h>
    enum { BUFF_MAX = 1024, NUM_MAX = 32 };
    
    int main(void)
    {
        char buffer[BUFF_MAX];
        int numbers[NUM_MAX];
        int i = 0;
        int offset = 0;
        int newlen;
    
        if (fgets(buffer, BUFF_MAX, stdin) != 0)
        {
            while (sscanf(buffer + offset, "%d%n", &numbers[i], &newlen) == 1 && i < NUM_MAX)
            {
                printf("Found: numbers[%d] = %d\n", i, numbers[i]);
                ++i;
                offset += newlen;
            }
        }
        return 0;
    }
    

    Input string:

    123 456 789 0123456
    

    Output:

    Found: numbers[0] = 123
    Found: numbers[1] = 456
    Found: numbers[2] = 789
    Found: numbers[3] = 123456
    

    The %n modifier was added in C89, but not many people know that it is there. It also ‘works’ in the printf() family of functions, but is even more lethal there because people aren’t used to seeing output parameters in a printf() format string. At least with scanf() et al, you are expecting inputs.

    Beware of ‘format string vulnerabilities’, a decent search term to use in your favourite search engine. You should be beware of them, independently of this question.

    [Updated to check that fgets() does not indicate an error or EOF. Always check that read operations do what you expected. Well, almost always — if you’re recovering from an error and don’t care what is read because you won’t be using it, maybe you don’t need to check. If you’re going to use the read data, you should check that you actually got the data to work with.]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a three dimensional array which I want to fill in with values
I want to fill an array in javascript that takes all the values out
i want to fill the data of the charts from values i have in
Normally I can do something like this to fill up the byte array with
i`m having this structure(array) which i want to fill with Class objects. What is
I want to fill a ListView without an array of all the same things.
Actually I want to populate an array to fill up my table view, but
I want to fill jqGrid from a stored procedure, there was 1 good example
I simply want to fill-up cells in my spreadsheet from a VBA function. By
I Had Drop down list and I want to fill it with data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.