4

so sorry if I am a little confused.

I am trying to fill out an array of structs with values I read in from an input file. I am having no trouble reading in the values from the file. But when the file is very small and does not fill the array completely, the remaining structs have random values in them, and I would like to completely set these structs to NULL. I am attempting to do this because I would like to run through this filled out array of structs and print its values, and I need to see which array values are legitimately from the file.

Here is my code so far

struct function {
    char name[20];
    int parameterNumer;

};


int main(int argc, const char * argv[])
{
    struct function functionList[10];
    int i =0, j;
    int portNumber;
    char *configFile = argv[1];
    FILE *fp;

    fp = fopen(configFile, "r");
    if(fp == NULL) {
        perror("File not found");
        exit(1);
    }

    fscanf(fp, "%d", &portNumber);
    while(fscanf(fp, "%s %d", functionList[i].name, &functionList[i].parameterNumer) == 2) {
        i++;
    }
    functionList[i] = NULL; //getting an error here

    for(j = 0; functionList[j] != NULL; j++) {  //and here
        printf("%s %d", functionList[j].name, &functionList[j].parameterNumer);
    }


    return 0;

}
user2158382
  • 4,430
  • 12
  • 55
  • 97
  • You're not checking if i is within your array bounds (0 <= i <= 9 in this case). The while loop could increase i to 10 and functionList[10] does not exist. – ccKep Apr 19 '13 at 19:09
  • You can't set a struct to `NULL`; `NULL` is a pointer value. Either define some distinguished value for your struct that denotes missing data, or keep track of how many (or which) elements of your array are currently valid. – Keith Thompson Apr 19 '13 at 19:21

3 Answers3

3

Initialize the array:

/* Remaining elements zero-d. */
struct function functionList[10] = { { "", 0 } };

if an empty string or a zero indicates an unpopulated entry in the array and then use either an empty string or zero int to terminate the for:

for(j = 0; strlen(functionList[j].name); j++) {

for(j = 0; functionList[j].parameterNumber; j++) {

Additionally, prevent out of bounds access on functionList in the while:

while(i < 10 && fscanf(fp,
                       "%s %d",
                       functionList[i].name,
                       &functionList[i].parameterNumer) == 2)
{
    i++;
}

Note that the value of i after this while would also provide a terminating condition for the subsequent for loop:

for (j = 0; j < i; j++) {
hmjd
  • 120,187
  • 20
  • 207
  • 252
2

You can also use memset:

memset(functionList, 0, sizeof(functionList));
Kevin
  • 53,822
  • 15
  • 101
  • 132
izogfif
  • 6,000
  • 2
  • 35
  • 25
2

You could create the array with a calloc()

struct function* functionList = calloc(sizeof(struct function), 10);

and change to this pointer where the array is referenced, in this way the struct is created with all zero in it.

gipi
  • 2,432
  • 22
  • 25
  • 2
    But don't cast the return value of calloc unless you want this to compile as C++ (just use `functionList = calloc(...`) – James M Apr 19 '13 at 19:11
  • 2
    @JamesMcLaughlin you are right, I update the answer (for reference of future reader see http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – gipi Apr 19 '13 at 19:16