指针上的分段错误

时间:2014-05-31 09:02:18

标签: c

#include<stdio.h>
#include<malloc.h>

int main(){
    int count,i,j;
    int* val;
    scanf("%d",count);
    val = (int*)malloc(sizeof(int)*count);
    if(NULL == val){
        printf("\n memory is NULl \n");
    }
    for(i=0;i<count;i++){
        scanf("%d",val[i]);
    }
    for(i=0;i<count;i++){
        for(j=1;j<=val[i];j++){
            if((j%3) == 0){
                printf("Fizz\n");
            }
            else if((j%5) == 0){
                printf("Buzz\n");
            }
            else{
                printf("%d\n",j);
            }
        }
    }
    return 0;
}

O / P:

2
Segmentation fault

3 个答案:

答案 0 :(得分:3)

scanf("%d",count);

没有&amp;在scanf中。它应该是

scanf("%d",&count);

答案 1 :(得分:2)

scanf("%d",count)更改为scanf("%d",&count)

同时更改

for(i=0;i<count;i++){
    scanf("%d",val[i]);

for(i = 0; i < count; i++){
    scanf("%d", &val[i]);

答案 2 :(得分:1)

替换

  

“的scanf(” %d “计数);” “scanf(”%d“,&amp; count);”

  

“的scanf(” %d “VAL [I]);” “scanf(”%d“,&amp; val [i]);”