C语言 C - 错误:下标值既不是数组也不是指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4149338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:59:25  来源:igfitidea点击:

C - error: subscripted value is neither array nor pointer

c

提问by matthewmpp

/*
 * File:   main.c
 * Author: matthewmpp
 *
 * Created on November 7, 2010, 2:16 PM
 */

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

/*
prime numbers.
version4
should tell whether a number is prime or not prime.
by using other prime numbers.
 */

int input_func() {
    char line[100];
    int n_input;

    while (1) {
        printf("Please enter a whole number.\n");
        fgets(line, sizeof (line), stdin);
        sscanf(line, "%d", &n_input);

        if (n_input >= 0)
            break;

        return (n_input);
    }
}

int ifstatements_func(n_ifstate)
int n_ifstate;
{
    if (n_ifstate == 0) {
        printf("The number, %d, is not prime and has no factors.\n", n_ifstate);
        exit(1);
    }

    if (n_ifstate == 1) {
        printf("The number, %d, is not prime.\n", n_ifstate);
        printf("The factors of %d, is %d.\n", n_ifstate, n_ifstate);
        exit(1);
    }

    if (n_ifstate == 2) {
        printf("The number, %d, is a prime.\n", n_ifstate);
        printf("The factors of %d, are 1 and %d.\n", n_ifstate, n_ifstate);
        exit(1);
    }
    if (n_ifstate == 3) {
        printf("The number, %d, is a prime.\n", n_ifstate);
        printf("The factors of %d, are 1 and %d.\n", n_ifstate, n_ifstate);
        exit(1);
    }
    return (n_ifstate);
}

int square_root_func(n_prmfnc)
int n_prmfnc;
{
    int i; //counter

    float sq_root_f;
    int sq_root_i;

    int primes[100];
    int length_primes;

    primes[0] = 2; /*first prime is 2.*/
    primes[1] = 3; /*second prime is 3.*/
    length_primes = sizeof (primes);

    //printf ("before.sq_root.value of n_prmfnc=%d\n", n_prmfnc);
    sq_root_f = sqrt(n_prmfnc);
    sq_root_i = sq_root_f;
    //printf ("prmfnc.after.sq_root\n");
    //printf ("value of sq_root=%.3f\n", sq_root_f);
    //printf ("value of sq_root=%d\n", sq_root_i);

    return (sq_root_i);
}

int prime_func(sq_root_pf, n_pf)
int sq_root_pf;
int n_pf;
{
    //printf ("in.pf.beginning.of.func.sq_root_pf=%d\n", sq_root_pf);
    //printf ("in.pf.beginning.of.func.n_pf=%d\n", n_pf);

    int factor_counter = 0;
    int factor_temp;
    int prime_counter = 0;
    int prime_flag = 0;

    int primes_pf[100];
    int length_primes_pf = 0; //counter
    int i; //counter

    primes_pf[0] = 2;
    primes_pf[1] = 3;
    primes_pf[2] = 5;

    length_primes_pf = 3;
    //printf("length_primes_pf=%d\n", length_primes_pf);

    //printf ("before.for.in.pf\n");
    for (i = 0; i <= length_primes_pf; ++i) {
        //printf ("after.for.in.pf\n");
        if (primes_pf[i] == 0)
            printf("primes_pf=0");
        else {
            if (primes_pf[i] <= sq_root_pf) {
                //printf("primes_pf.i=%d, sq_root_pf=%d\n", primes_pf[i], sq_root_pf);
                //printf("before.modulus.in.pf\n");
                factor_temp = n_pf % primes_pf[i];
                //printf("after.modulus.in.pf\n");
                //printf("value.of.factor_temp=%d\n", factor_temp);
                if (factor_temp == 0) {
                    ++factor_counter;
                    //printf("value.factor_counter=%d\n", factor_counter);
                } else
                    ++prime_counter;

                if (factor_counter == 0 && prime_counter > 0) {
                    prime_flag = 1; /*yes, number is prime.*/
                    primes_pf[length_primes_pf + 1] = n_pf;
                    //printf("length_primes_pf=%d\n", length_primes_pf);
                }
            }

        }
    }

    if (prime_flag == 1) {
        printf("The number, %d, is prime.\n", n_pf);
        printf("The factors of %d, is 1 and %d.\n", n_pf, n_pf);
        exit(0);
    } else
        printf("The number, %d, is not prime.\n", n_pf);

    if (prime_flag == 0)
    pfactorization (primes_pf, length_primes_pf, n_pf);

    return (prime_flag);
}

int pfactorization(primes_fac, length_primes_fac, n_fac)
int primes_fac[];
int length_primes_fac;
int n_fac;
{
    int i;
    int j;

    int result;
    int n_temp;

    int z_array;
    int length_z_array=0;

    for (i=0; i<=length_primes_fac; ++i) {
        result = n_fac%primes_fac[i];
        if (result == 0) {
            n_temp = n_fac/primes_fac[i];
            z_array[length_z_array]=primes_fac[i];
            ++length_z_array;
        }
    }

    printf ("The prime factorization is:");
    for (j = 0; j < length_z_array; ++j)
        printf("%d\n", z_array[j]);
}

int factors_func(n_ff)
int n_ff;
{
    int i;
    int j;

    int result;

    int factors[100];
    int length_factors = 0;

    for (i = 2; i < n_ff; ++i) {
        result = n_ff % i;
        if (result == 0) {
            factors[length_factors] = i;
            ++length_factors;
        }
    }

    printf("The factors for %d are:\n", n_ff);
    printf("1\n");

    for (j = 0; j < length_factors; ++j)
        printf("%d\n", factors[j]);

    printf("%d\n", n_ff);

    return (EXIT_SUCCESS);
}

int main() {
    int n_main1; //number from input
    int n_main2; //number after if statements
    int sq_root_main; //square root of number from function
    int prime_flag_main; //value of 1 if it is a prime

    n_main1 = input_func();
    //printf("main.after.input.function=%d.\n", n_main1);

    n_main2 = ifstatements_func(n_main1);
    //printf("main.after.ifstatments.function=%d\n", n_main2);

    sq_root_main = square_root_func(n_main2);
    //printf("main.after.square_root_func_func=%d\n", sq_root_main);

    prime_flag_main = prime_func(sq_root_main, n_main2);
    //printf("main.after.prime_func=%d\n", prime_flag_main);

    factors_func(n_main2);



    return (EXIT_SUCCESS);
}

OUTPUT:

输出:

matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim prime6.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c prime6.c
prime6.c: In function ‘pfactorization':
prime6.c:171: error: subscripted value is neither array nor pointer
prime6.c:178: error: subscripted value is neither array nor pointer

STATEMENT; The function: pfactorization is supposed to find the prime factorization of a number. It is called at the bottom of function: prime_func. Data is being passed from prime_func to pfactorization. Code was working fine before I added this function.

陈述; 函数: pfactorization 应该找到一个数字的素数分解。它在函数底部调用:prime_func。数据正在从 prime_func 传递到 pfactorization。在我添加此功能之前,代码运行良好。

QUESTION: I don't understand this error message. What does it mean and how should I fix it?

问题:我不明白这个错误信息。这是什么意思,我该如何解决?

SOLVED: int z_array[100]; Thanks.

已解决:int z_array[100]; 谢谢。

回答by Cameron Skinner

z_arrayis declared to be type int. intis neither an array nor a pointer :)

z_array被声明为 type intint既不是数组也不是指针:)

I'm guessing you meant to make it an int*.

我猜你是想把它变成一个int*.

回答by Blagovest Buyukliev

z_arrayis just declared as an int, and you are trying to index it using square brackets like an array.

z_array只是声明为 an int,并且您尝试使用方括号(如数组)对其进行索引。

回答by frast

z_array is not an array of integers it is a single integer. You cannot subsript it like z_array[var]. To declare an array you can do int z_array[100] for example.

z_array 不是整数数组,它是一个整数。你不能像 z_array[var] 那样订阅它。例如,要声明一个数组,您可以执行 int z_array[100] 。

回答by McKay

I can't tell which line is the correct line, but what that means is that you're using the indexing operator "[]" on something that isn't the correct type.

我不知道哪一行是正确的行,但这意味着您在不正确的类型上使用了索引运算符“[]”。

blah[foo]

blah must be of type array, or pointer.

blah 必须是数组或指针类型。

Edit: Your code:

编辑:您的代码:

int z_array;
.....
      z_array[length_z_array]
.....
   printf("%d\n", z_array[j]);

z_array is declared as an int, int's can't be indexed

z_array 被声明为 int,int 不能被索引

回答by ephemient

int z_array;
z_array[length_z_array]=primes_fac[i];
printf("%d\n", z_array[j]);

z_arrayis not an int[]or an int*, thus z_array[i]is nonsensical.

z_array不是 anint[]或 an int*,因此z_array[i]是无意义的。

I suspect you may have intended to write

我怀疑你可能打算写

int *z_array = malloc((length_primes_fac + 1) * sizeof(int));
...
free(z_array);

or something similar. I haven't dug into the surrounding code to determine if that's realy the correct size or not.

或类似的东西。我还没有深入研究周围的代码来确定这是否真的是正确的大小。

回答by zwol

You seem to have solved your immediate problem, but your program is crawling with more subtle problems. I have taken the liberty of rewriting it for you. There is a reason for every change I made, even the ones that seem trivial. Please read it carefully and consider why I did what I did. I am happy to answer specificquestions about the changes.

您似乎已经解决了您眼前的问题,但是您的程序却在爬行着更微妙的问题。我冒昧地为你重写了它。我所做的每一次改变都是有原因的,即使是那些看似微不足道的改变。请仔细阅读并思考我为什么这样做。我很高兴回答有关更改的具体问题。

/* factor.c - produce the prime factorization of a number.
   Uses the Sieve of Eratosthenes.  */

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

static unsigned char *
compute_sieve(unsigned long root)
{
    unsigned char *sieve = malloc(root + 1);
    unsigned long i, j;

    /* all numbers are prime till proven otherwise */
    memset(sieve, 1, root + 1);

    /* 0 and 1 are not prime */
    sieve[0] = 0;
    sieve[1] = 0;

    for (i = 2; i <= root; i++) {
        if (!sieve[i])
            continue;
        for (j = 2; i*j <= root; j++)
            sieve[i*j] = 0;
    }

    return sieve;
}

static unsigned long *
compute_factors(unsigned long number, const unsigned char *sieve,
                unsigned long root, int *isPrime)
{
    unsigned long i;
    unsigned long *factors = calloc(root + 1, sizeof(unsigned long));

    *isPrime = 1;

    /* trial division by each prime in turn, starting with 2.  */
    for (i = 2; i <= root; i++) {
        if (!sieve[i])
            continue;
        while (number % i == 0) {
            *isPrime = 0;
            number /= i;
            factors[i]++;
        }
    }

    return factors;
}

static void
factor(unsigned long number)
{
    unsigned long root, i;
    unsigned char *sieve;
    unsigned long *factors;
    int isPrime;

    /* weed out base cases */
    if (number <= 3) {
        printf(" %lu\n", number);
        return;
    }

    /* sieve needs to go up to the square root of NUMBER */
    root = (unsigned long) floor(sqrt(number));
    sieve = compute_sieve(root);
    factors = compute_factors(number, sieve, root, &isPrime);

    if (isPrime)
        printf(" %lu\n", number);
    else {
        for (i = 2; i <= root; i++) {
            while (factors[i]) {
                printf(" %lu", i);
                number /= i;
                factors[i]--;
            }
        }
        if (number > 1)
          printf(" %lu", number);
        putchar('\n');
    }

    free(sieve);
    free(factors);
}

static void
usage(char **argv)
{
    fprintf(stderr, "usage: %s NUMBER\n"
            "NUMBER must be a non-negative integer\n",
            argv[0]);
}

int
main(int argc, char **argv)
{
    unsigned long number;
    char *endptr;

    if (argc != 2) {
        usage(argv);
        return 1;
    }

    number = strtoul(argv[1], &endptr, 10);
    if (endptr == argv[1] || *endptr != '##代码##') {
        usage(argv);
        return 1;
    }

    factor(number);
    return 0;
}