我知道此错误消息还有其他问题,但没有一个能解决我的情况下的错误消息 这里的代码找到第一个具有超过 N 个除数的三角形数的值,其中 N 是输入(这个问题通常在欧拉项目中发现)我使用 sieve of Eratosthenes生成素数列表,并由 Divisor function 使用 我缓存了 N = 500->1000 的三角数的值 为了省时间。在 GCC 上编译时我发现了这个错误消息
error: subscripted value is neither array nor pointer nor vector
if(n>=500) printf("%li\n",tri[n-500]);
^
这是完整的代码
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000000
#define NofPrime 78498
int main(){
long tri[] = {76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,76576500,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,103672800,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,236215980,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320,842161320};
unsigned char *table = malloc(sizeof(char)*MAX);
int i,j,c=0;
unsigned int *prime = malloc(sizeof(unsigned int)*NofPrime);
for(i=0;i<MAX;i++)table[i] = 0;
for(i=2;i<MAX;i++){
if(table[i]==0){
prime[c] = i;
c++;
for(j=2;i*j<MAX;j++) table[i*j] = 1;
}}
free(table);
int t;
scanf("%i",&t);
while(t>0){
unsigned long max=1,counter,tri,n,tricpy;
scanf("%li",&n);
if(n>=500) printf("%li\n",tri[n-500]);
else while(max++){
tri = (1+max)*max/2;
tricpy = tri;
counter=1;
int i,j;
for(i=0;prime[i] <= tricpy;i++){
j =1;
while(tricpy%prime[i]==0){
j++;
tricpy/=prime[i];
}
counter*=j;
}
if(counter>n){
printf("%lu,",tri);
break;
}
}
t--;
}
free(prime);
}
最佳答案
tri的定义
unsigned long max=1,counter,tri,n,tricpy;
将是
中使用的那个if(n>=500) printf("%li\n",tri[n-500]);
所以 tri 确实不是数组或指针。
关于c - long int 数组导致错误 : subscripted value is neither array nor pointer nor vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30681077/