سؤال

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. 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]. Also, init i for best practice (although gcc does init ints to zero if uninitialized, try it yourself and see).

Edit: Ignore the downvote, I am correct but I did make an error in forgetting to remove your second index, you can access a c string 2d array with object[i] or &object[i][0], both work. object[i] for accessing an entire string looks cleaner to me than using &object[i][0].

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top