简体   繁体   English

Segfault-扫描到C中的数组

[英]Segfault - sscanf to arrays in C

I just need an extra set of eyes to help me find out why this code is segfaulting. 我只需要额外的眼睛来帮助我找出为什么此代码存在段错误。

//------------------------Preprocessor Instructions. ------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#define NUMBER 128  //Maxmimum number of items/lines in file.
#define BUFFER 120  //Buffer length (For user input)
#define LENGTH 32   //Maximum length of lines in file.

//------------------------Global stuff. ---------------------------------------------
int iterations=0;   //Will count number of times the calculation function is called. 

int weight[NUMBER];     
int value[NUMBER];      
char object[NUMBER][LENGTH];        


//------------------------Function Definitions. -----------------------------------------
void printarr();


//------------------------Printarr -- Array printing function. --------------------------
void printarr()
{
    int i,j;

    printf("\n");
    printf("Weight \t Value \t Object \n");
    for(i=0;i<4;i++){
        printf("%d \t %d \t %s \n", weight[i], value[i], &object[i][0]);

    }
}


//------------------------Main. ---------------------------------------------------------
int main(int argc, char **argv)
{
    FILE *fp;   //File pointer.
    char buffer[BUFFER];    //Temporary storage
    int result; //sscanf return value.
    int capacity;   //User input.
    int i,j=0;  //Loop counters.

//Command Line Argument Parsing: Assigns input value to capacity.
    if (argc != 2){
        printf("Usage: %s number. Max 1024. \n",argv[0]);   //Usage: *program* *num*
        return(1);
    }

    if (1 != sscanf(argv[1],"%d",&capacity)){
        printf("Usage: %s number. Max 1024. \n",argv[0]);
        return(1);
    }

//File reading. 
    fp=fopen("knapsack.data","r");
    if(NULL==fp){
        printf("Error opening file. \n");
        exit(0);
    }   

//Write to arrays.  
    while(NULL != fgets(buffer, BUFFER, fp)){
        result=sscanf(buffer, "%d %d %s", &weight[i], &value[i], &object[i][0]);
        i++;
    }

//Print the arrays.
    printarr();

fclose(fp);
}

According to GDB it segfaults when it hits the sscanf statement. 根据GDB的说法,当它触到sscanf语句时,它将发生段错误。 But as far as I can tell there's nothing wrong with the way I'm accessing the locations... clearly I'm mistaken. 但据我所知,访问位置的方式没有任何问题……显然我错了。 Any help would be appreciated. 任何帮助,将不胜感激。

Edit: I was half right, fix this line: 编辑:我是正确的一半,修复此行:

result=sscanf(buffer, "%d %d %s", &weight[i], &value[i], &object[i][0]);

to look like this: 看起来像这样:

result=sscanf(buffer, "%d %d %s", &weight[i], &value[i], object[i]);

You are reading in a whole string, so you need to write to the c string location, in this case, object[i]. 您正在读取整个字符串,因此需要写入c字符串位置,在这种情况下为object [i]。 Also, init i for best practice (although gcc does init ints to zero if uninitialized, try it yourself and see). 另外,将i设为最佳实践(尽管如果gcc未初始化的话,它会将int ints设置为零,请自己尝试并查看)。

Edit: Ignore the downvote, I am correct but I did make an error in forgetting to remove your second index, you can access ac string 2d array with object[i] or &object[i][0], both work. 编辑:忽略下降投票,我是正确的,但是在忘记删除第二个索引时确实犯了一个错误,您可以使用object [i]或&object [i] [0]访问ac字符串2d数组,两者均有效。 object[i] for accessing an entire string looks cleaner to me than using &object[i][0]. 与使用&object [i] [0]相比,用于访问整个字符串的object [i]看起来更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM