chlm007 发表于 2019-5-30 12:04:58

warning: shadowed declaration is here 啥意思???

>"D:\gcc\bin/g++" -Os -mconsole -Wall -Wshadow -fno-common -static-libgcc -static-libstdc++ -std=gnu++11 -fpermissive"26.c" -o "26.exe"
26.c: In function 'void spilt(char*)':
26.c:35:22: warning: declaration of 'name' shadows a global declaration [-Wshadow]
void spilt(char *name)
                      ^
26.c:7:6: warning: shadowed declaration is here [-Wshadow]
char name;

这俩warning啥意思???有影响么
还有为啥我输入stark,Tony没问题,输入America,Captain还是出现tony Stark??
      ^
>Exit code: 0



#include <stdio.h>
#include <string.h>
char guy1[]="50:Tony Stark";
char guy2[]="20:Peter Benjamin Parker";
char guy3[]="1500:Thor";
char guy4[]="99:Captain America";
char name;

void spilt(char *);
void input();
void check();
int main()
{
input();
check();
printf("/n");
       
}

// 1. user input the name function
void input(){
        int i =0;
printf("Type in a file name: names.txt\n");
printf("Enter the name of a person: ");
scanf("%s", name);
        for (i=0;i<30;i++){
                if(name==44){//ASCII , = 44 spilt the name if type: Secondname ,Firstname
                        spilt(name);
                        //printf("S,F success");
                        }
                }
}

/* 2. spilt the name*/
void spilt(char *name)
{
    int count = 0;
    while((name = getchar()) != ',' && name != '\n')
    ++count;
    name = '\0';
}



/*3. check the input name right or not.*/
void check(){
        if((strstr(guy1, name))!= NULL ){
                printf("Tony Stark is not a pensioner.\n");
        }
        else if((strstr(guy2, name))!= NULL){
                printf("Peter Benjamin Parker is not a pensioner.\n");
        }
        else if((strstr(guy3, name))!= NULL){
                printf("Thor is a pensioner.\n");
        }
        else if((strstr(guy4, name))!= NULL){
                printf("Captain America is a pensioner.\n");
        }else{
                printf(" No record of this person.");
        }
}

chlm007 发表于 2019-5-30 12:14:49

我想着把name 改成void spilt 中的一个变量,再设计一个*fullname全局指针,然后出现什么无法转换warning,昏啊
a5test.c: In function 'void input()':
a5test.c:31:14: warning: invalid conversion from 'char*' to 'char' [-fpermissive]
    *fullname = name;
>Exit code: 1

#include <stdio.h>
#include <string.h>
char guy1[]="50:Tony Stark";
char guy2[]="20:Peter Benjamin Parker";
char guy3[]="1500:Thor";
char guy4[]="99:Captain America";
char *fullname;

void spilt(char *);
void input();
void check();
int main()
{
input();
check();
printf("/n");
       
}

// 1. user input the name function
void input(){
        int i =0;
        char name;
printf("Type in a file name: names.txt\n");
printf("Enter the name of a person: ");
scanf("%s", name);
fullname = name;
        for (i=0;i<30;i++){
                if(name==44){//ASCII , = 44 spilt the name if type: Secondname ,Firstname
                        spilt(name);
                        *fullname = name;
                        //printf("S,F success");
                        }
                }
}

/* 2. spilt the name*/
void spilt(char *name)
{
    int count = 0;
    while((name = getchar()) != ',' && name != '\n')
    ++count;
    name = '\0';
}



/*3. check the input name right or not.*/
void check(){
        if((strstr(guy1, fullname))!= NULL ){
                printf("Tony Stark is not a pensioner.\n");
        }
        else if((strstr(guy2, fullname))!= NULL){
                printf("Peter Benjamin Parker is not a pensioner.\n");
        }
        else if((strstr(guy3, fullname))!= NULL){
                printf("Thor is a pensioner.\n");
        }
        else if((strstr(guy4, fullname))!= NULL){
                printf("Captain America is a pensioner.\n");
        }else{
                printf(" No record of this person.");
        }
}

bin554385863 发表于 2019-5-30 12:16:00

>"D:\gcc\bin/g++" -Os -mconsole -Wall -Wshadow -fno-common -static-libgcc -static-libstdc++ -std=gnu++11 -fpermissive"26.c" -o "26.exe"
26.c: In function 'void spilt(char*)':
26.c:35:22: warning: declaration of 'name' shadows a global declaration [-Wshadow]//警告: name的隐藏声明是全局声明
void spilt(char *name)
                      ^
26.c:7:6: warning: shadowed(隐藏) declaration(声明) is here [-Wshadow]隐藏声明在这里
char name;

bin554385863 发表于 2019-5-30 12:22:17

chlm007 发表于 2019-5-30 12:14
我想着把name 改成void spilt 中的一个变量,再设计一个*fullname全局指针,然后出现什么无法转换warning, ...

你的变量name是默认全局变量,怎么能变成局部变量?
而且构造函数的参数只是个形式参数,两者本质都不一样,怎么转换呢.
兄弟!

chlm007 发表于 2019-5-30 14:22:28

bin554385863 发表于 2019-5-30 12:22
你的变量name是默认全局变量,怎么能变成局部变量?
而且构造函数的参数只是个形式参数,两者本质都不一样, ...

谢谢啦。所以是什么不用改喽?另外strstr()我用的对么?

bin554385863 发表于 2019-5-30 14:32:59

本帖最后由 bin554385863 于 2019-5-30 14:36 编辑

chlm007 发表于 2019-5-30 14:22
谢谢啦。所以是什么不用改喽?另外strstr()我用的对么?

strstr(guy1, name))!= NULL

如果为真,则表示name就是guy1的子串,返回name在guy1中首次出现的地址。

如果为假,则表示name不是guy1的子串。
页: [1]
查看完整版本: warning: shadowed declaration is here 啥意思???