Skip to content

Fix Suggestion #2

@sebetci

Description

@sebetci

Hi @AndrewLaing,

I think there is a misuse in the code block below in the ex11_12.c file:

Click to see the source code in the ex11_12.c

struct tool
{ 
    int recordNum;
    char toolName[ 30 ];
    int quantity;
    double cost;
};

void addRecord( FILE *fPtr )
{
    struct tool blankTool = { 0, "", 0, 0.0 };
    ...
    scanf( "%s%d%lf", &blankTool.toolName, &blankTool.quantity, &blankTool.cost ); 
    ....
}

void updateRecord( FILE *fPtr )
{
    struct tool blankTool = { 0, "", 0, 0.0 };
    ....
    scanf( "%s%d%lf", &blankTool.toolName, &blankTool.quantity, &blankTool.cost );   
    ....
}

Since the toolName variable of the blankTool structure instance is a character array member of the tool structure, I think the "&" operator should not be used when reading from the stdin stream with scanf(). Therefore, this program line should be changed by choosing one of the following uses:

Click to see recommended source code.

    scanf( "%s%d%lf", blankTool.toolName, &blankTool.quantity, &blankTool.cost );   
    scanf( "%s%d%lf", &blankTool.toolName[ 0 ], &blankTool.quantity, &blankTool.cost );   

Also in your use GCC (ISO C90, ISO C99 and ISO C11) gives the following warning:

Click to see the GCC warning.

warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[30]' [-Wformat=]
  130 |         scanf( "%s%d%lf", &blankTool.toolName, &blankTool.quantity, &blankTool.cost );
      |                 ~^        ~~~~~~~~~~~~~~~~~~~
      |                  |        |
      |                  char *   char (*)[30]

warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[30]' [-Wformat=]
  160 |         scanf( "%29s%d%lf", &blankTool.toolName, &blankTool.quantity, &blankTool.cost );
      |                 ~~~^        ~~~~~~~~~~~~~~~~~~~
      |                    |        |
      |                    char *   char (*)[30]

Thank you for your contributions to the community. I wish you good work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions