You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何接收用户逗号分隔数字输入并存入数组?遇格式错误求解决

Solution for Storing 20 Comma-Separated Numbers in an Array

Hey there! Let's tackle your problem head-on. First, let's clear up that confusing warning, then walk through two solid solutions that'll get your numbers into the array cleanly—no leftover separators.

First: Fixing That Format Warning

The error format specifies type 'int *' but the argument has type 'int (*)[10]' (or int (*)[20] in your case) happens because you passed the address of the entire array to scanf instead of the address of a single array element.

For example, if you wrote something like:

int arr[20];
scanf("%d", &arr); // Wrong! &arr is a pointer to the whole array, not an int*

That's the root issue. You need to pass the address of each individual element with &arr[i] instead. Now let's look at working solutions.


Solution 1: Directly Read with scanf (Simplest Approach)

scanf can handle comma separators directly in its format string, so you don't need extra string processing. Here's how to do it properly:

#include <stdio.h>

int main() {
    int num_array[20];
    int i;

    printf("Enter 20 comma-separated numbers (e.g., 12, 34, 56...): ");
    
    // Read 19 numbers followed by a comma (handles optional spaces too)
    for (i = 0; i < 19; i++) {
        scanf("%d ,", &num_array[i]);
    }
    // Read the 20th number without a trailing comma
    scanf("%d", &num_array[19]);

    // Verify the result
    printf("\nYour array contains:\n");
    for (i = 0; i < 20; i++) {
        printf("%d ", num_array[i]);
    }
    printf("\n");
    return 0;
}

How this works:

  • The format string "%d ," tells scanf to read an integer, then skip any spaces followed by a comma. This handles inputs like 12,34 or 12 , 34 seamlessly.
  • We split the loop into 19 comma-separated reads and one final read for the last number, avoiding issues with a trailing comma.
  • The array will only hold integers—no commas or spaces leftover.

Solution 2: Use strtok + atoi (For String-First Processing)

If you prefer to read the entire input as a string first (maybe for validation later), this method works perfectly and ensures no separators end up in your array:

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

int main() {
    char input_str[256]; // Big enough for 20 numbers + separators
    int num_array[20];
    int index = 0;
    char *token;

    printf("Enter 20 comma-separated numbers: ");
    fgets(input_str, sizeof(input_str), stdin);

    // Remove the trailing newline from fgets
    input_str[strcspn(input_str, "\n")] = '\0';

    // Split the string using commas AND spaces as separators
    token = strtok(input_str, ", ");
    while (token != NULL && index < 20) {
        num_array[index++] = atoi(token); // Convert string to integer
        token = strtok(NULL, ", "); // Get next token
    }

    // Verify the result
    printf("\nYour array contains:\n");
    for (index = 0; index < 20; index++) {
        printf("%d ", num_array[index]);
    }
    printf("\n");
    return 0;
}

How this works:

  • fgets reads the entire input line into a string. We strip the newline character so it doesn't interfere with splitting.
  • strtok splits the string into "tokens" using ", " as delimiters—so it ignores both commas and spaces, handling messy input gracefully.
  • atoi converts each token (a numeric string) to an integer, which we store directly in the array. No separators make it into the array at all.

Either of these methods will solve your problem. The first is quicker for straightforward input, while the second gives you more control if you need to manipulate the input string before converting to integers.

内容的提问来源于stack exchange,提问作者user9741196

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:49:12