0

I am using fgets inside another function to return valid data, as I have a menu list and it becomes repetitive to write out fgets for each function. I pass through a character array result[] which seems to always be the sizeof 8, which is causing my data to get clipped.

void get_input(char result[]) {
    if(fgets(result, sizeof(result), stdin) == NULL) { // sizeof is ALWAYS 8!
        read_rest_of_line();
        printf("\n");
    } else
        clearbuffer(result);
}

void get_valid_input(char result[]) {
    get_input(result);

    while(result == NULL || result[0] == '\n') {
        printf("Enter a valid input or quit with CTRL+D\n");
        get_input(result);
    }

    /* remove newline replace with nul terminator */
    result[strlen(result)-1] = '\0';
}

It being executed:

char equipment_id[7];
char equipment_name[31+1];
char equipment_quantity[sizeof(long)];
long quantity;

printf("New ID: ");
get_valid_input(equipment_id);
printf("Please enter the equipment name: ");
get_valid_input(equipment_name);
printf("Please enter the total quantity: ");
get_valid_input(equipment_quantity);
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Johnny Doey
  • 93
  • 1
  • 8

2 Answers2

4

When an array is passed as function argument, it decays to a pointer to its first element.

In your example, char result[] in the function parameter is actually a pointer, thus sizeof gives the size of a pointer, not the array.

To fix the problem, you can pass the size of the array explicitly.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Is that the only solution to this, or is there a workaround that doesn't involve getting more advanced, such as a struct or something? – Johnny Doey Feb 01 '15 at 02:17
  • 2
    @JohnnyDoey Depends on what you mean by using `struct`. I think passing the size explicitly is in general a simple solution. – Yu Hao Feb 01 '15 at 02:20
  • In fact, as a parameter declaration (and *only* as a parameter declaration), `char result[]` actually means `char *result`. Suggested reading: Section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson Feb 01 '15 at 02:53
0

Here is a simple example of passing the length of the current input buffer and using that length:

void get_input(char *result, int maxSize)
{
    if(fgets(result, maxSize, stdin) == NULL)
    { 
        read_rest_of_line();
        printf("\n");
    }

    else
    {
        clearbuffer(result);
    }
} // end function: get_input

void get_valid_input(char *result, int maxSize)
{
    memset( result, 0x00, maxSize );
    get_input(result, maxSize);

    while(result[0] == '\n')
    { 
        printf("Enter a valid input or quit with CTRL+D\n");
        get_input(result, maxSize);
    }

    /* remove newline replace with nul terminator */
    result[strlen(result)-1] = '\0';
} // end function: get_valid_input



char equipment_id[7];
char equipment_name[31+1];
char equipment_quantity[sizeof(long)];
long quantity;

printf("New ID: ");
get_valid_input(equipment_id, sizeof(equipment_id);
printf("Please enter the equipment name: ");
get_valid_input(equipment_name, sizeof(equipment_name);
printf("Please enter the total quantity: ");
get_valid_input(equipment_quantity, sizeof( equipment_quantity);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user3629249
  • 16,402
  • 1
  • 16
  • 17