PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 27, 2022

[FIXED] Why gcc gives to me warnings?

 April 27, 2022     c, gcc, warnings     No comments   

Issue

Why when I compile with gcc I get some warnings about the compiled code?

I have tried to change some parts of the code to suppress the warnings, but when I get suppress the warnings when compile then when it is runing there are errors and the debug session stop.

Any help to understand why is happening this would be welcome.

The warnings I get are the followings:

gcc Question_1.c -o Question_1
Question_1.c: In function ‘main’:
Question_1.c:43:10: warning: passing argument 1 of ‘menu’ from incompatible pointer type [-Wincompatible-pointer-types]
 menu(&name_file_in,&name_file_out);
      ^
Question_1.c:5:6: note: expected ‘char *’ but argument is of type ‘char (*)[17]’
void menu(char *name_file_in,char *name_file_out)
  ^~~~
Question_1.c:43:24: warning: passing argument 2 of ‘menu’ from incompatible pointer type [-Wincompatible-pointer-types]
 menu(&name_file_in,&name_file_out);
                    ^
Question_1.c:5:6: note: expected ‘char *’ but argument is of type ‘char (*)[16]’
void menu(char *name_file_in,char *name_file_out)
  ^~~~

The C code that produce it is the following:

#include <stdio.h>
#include <string.h>

void menu(char *name_file_in,char *name_file_out)
{

int option;

do
{
    printf("\n1. To write a name for an input file.");
    printf("\n2. To write a name for an output file.");
    printf("\n3. Exit.");
    printf("\n\nWrite an option (1-3): ");
    scanf("%d",&option);

    switch(option)
    {
        case 1: printf("\nWrite a name for the input file, (1 default): ");
                scanf("%s",&*name_file_in);
                if(strcmp(&*name_file_in,"1")==0)
                    strcpy(&*name_file_in,"data_in.dat");
                printf("\nThe input filename is: %s\n",&*name_file_in);
                break;

        case 2: printf("\nWrite a name for the output file, (1 default): ");
                scanf("%s",&*name_file_out);
                if(strcmp(&*name_file_out,"1")==0)
                    strcpy(&*name_file_out,"data_out.dat");
                printf("\nThe output filename is: %s\n",&*name_file_out);
                break;

     }

} while(option!=3);

}

int main()
{
    char name_file_in[17],name_file_out[16];
    menu(&name_file_in,&name_file_out);
    return 0;
}

Solution

menu(&name_file_in,&name_file_out);

When you use & you are trying to pass address of complete array which is char (*)[17]

Pass the address of first element in the array as below.

  menu(name_file_in, name_file_out);

Or

Expect the array with fixed size at the function as below.

void menu(char (*name_file_in)[17], char (*name_file_out)[16])


Answered By - kiran Biradar
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing