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

单个字符与字符数组比较报错:subscripted value is neither an array nor pointer nor vector

Fixing "subscripted value is neither an array nor pointer nor vector" Error in Your C Code

Hey there! Let's break down what's going wrong with your code and get it working properly.

The Root Cause of the Error

Your suffix function uses an outdated, implicit parameter declaration style that's confusing the compiler. When you write:

int suffix ( str, ch)

The compiler defaults both str and ch to int type (an old C quirk you should definitely avoid). So when you try to access str[i], you're trying to subscript an int—which isn't an array, pointer, or vector—hence the error you're seeing.

Other Issues Holding Your Code Back

Before fixing the main error, let's note a few other problems that would prevent your code from behaving as expected:

  • The global cnt variable will keep its value between function calls, which is almost certainly not what you want.
  • You're mixing strchr checks with manual character comparisons, leading to redundant work and missed edge cases.
  • The function is missing a closing brace, and you didn't show how you intended to call it.

Corrected Code

Here's a fixed version that addresses all these issues:

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

// Explicitly type parameters, make cnt local to the function
int suffix(const char *str, char ch) {
    int cnt = 0;
    int i = 0;
    // Loop through each character until we hit the string's null terminator
    while (str[i] != '\0') {
        if (ch == str[i]) {
            printf("\n%s\n", str + i);
            cnt++;
        }
        i++;
    }
    return cnt;
}

// Example main function to test the suffix function
int main() {
    char input_str[80];
    char target_ch;
    
    printf("Enter a string: ");
    fgets(input_str, sizeof(input_str), stdin);
    // Remove the newline character added by fgets
    input_str[strcspn(input_str, "\n")] = '\0';
    
    printf("Enter a character to search for: ");
    scanf("%c", &target_ch);
    
    int result = suffix(input_str, target_ch);
    printf("\nTotal occurrences found: %d\n", result);
    
    return 0;
}

Key Fixes Explained

  • Explicit Parameter Types: We declared str as const char * (since we don't modify the string) and ch as char, so the compiler correctly recognizes str as a pointer (which can be subscripted like an array).
  • Local cnt Variable: Instead of using a global variable, we declare cnt inside the function so it resets to 0 every time the function runs.
  • Simplified Loop: We replaced the strchr check with a straightforward loop over the string until we hit the null terminator ('\0'), making the logic clearer and avoiding redundant checks.
  • Added Test main Function: This shows how to properly call the suffix function with user input, including handling the newline character left by fgets.

Why Your Original Comparison Idea Was Correct

You're totally right that a single char and a character array element (also a char) can be directly compared with ==—the problem wasn't the comparison itself, it was the incorrect type of the str parameter that made str[i] invalid.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:53:28