热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C语言中的结构烦人的问题-AnnoyingproblemswithStructinC

structUsers{intid;charmsg[];};intnUsers;structUsersusers[10];voidcon
struct Users{
        int id;
        char msg[];
};

int nUsers;

struct Users users[10];

void connectUser(struct Users user){
     if(nUsers<10){
        for(int i=0;i<10;i++){
                if(users[i]==NULL){
                        users[i]=user;
                        printf("user %d connected!\n", user.id);
                        nUsers++;
                }
        }
    }else
        printf("number of users reached!\n");
}

That's my code and when I try to compile, comes with error:

那是我的代码,当我尝试编译时,会出现错误:

[s3450124@csitprdap01 ~]$ gcc -std=c99 socketserver.c -o socketserver
socketserver.c: In function ‘connectUser’:
socketserver.c:24: error: invalid operands to binary == (have ‘struct Users’ and ‘void *’)
socketserver.c:21: note: The ABI of passing struct with a flexible array member has changed in GCC 4.4
socketserver.c: In function ‘disconnectUser’:
socketserver.c:37: error: incompatible types when assigning to type ‘struct Users’ from type ‘void *’

Every time I try to compile, these errors comes up. Can you guys help me?

每次我尝试编译时,都会出现这些错误。你们能帮助我吗?

2 个解决方案

#1


The type of users[i] when say i = 0 is Struct Users. It is NOT a pointer so cannot have a value of NULL. That is what your compiler is complaining about. you are checking if the value is NULL when it cannot be. You only check for NULL when comparing pointers.

当i = 0时,用户[i]的类型是结构用户。它不是指针,因此不能具有NULL值。这就是你的编译器所抱怨的。你正在检查值是否为NULL时,它不能。您只比较指针时检查NULL。

// you already check here that you do not exceed bounds of array - 10
for(int i=0;i<10;i++){
    users[i]=user;
    etc

#2


As noticed by @CoolGuy, the problem is due to if(users[i]==NULL). Since users is an array of struct Users, users[i] is a struct Users and a struct Users cannot be compared to NULL.

正如@CoolGuy所注意到的,问题是由于if(users [i] == NULL)。由于users是struct Users的数组,因此users [i]是struct Users和struct,用户无法与NULL进行比较。

A pointer to a struct Users, declared as a struct Users* can be compared to NULL.

指向结构用户的指针,声明为结构用户*可以与NULL进行比较。

Now that the problem is found, how to solve it ?

既然发现了问题,如何解决呢?

  • The straightforward answer is to use a convention. For instance, let's say that users[i].id==0 means that this user is disconnected. At the beginning of the program, the users must be initialized :

    直截了当的答案是使用一个约定。例如,假设users [i] .id == 0表示此用户已断开连接。在程序开始时,必须初始化用户:

    for(i=0;i<10;i++){
      users[i].id=0;
    }
    

    As the user is connected a free slot must be found :

    在连接用户时,必须找到一个空闲插槽:

     for(int i=0;i<10;i++){
            if(users[i].id==0){
                    users[i]=user;
                    printf("user %d connected!\n", user.id);
                    nUsers++;
                    break;
            }
      }
    

    Do not forget the break statement : the user must be connected only once ! As the user is diconnected, users[i].id=0;

    不要忘记break语句:用户必须只连接一次!当用户被连接时,用户[i] .id = 0;

  • The other option is to declare struct Users *users[10];. Hence users is an array of pointers to struct Users. Again, these pointers must be initialized :

    另一种选择是声明struct Users * users [10] ;.因此,users是一个指向struct Users的指针数组。同样,必须初始化这些指针:

    for(i=0;i<10;i++){
      users[i]=NULL;
    }
    

    As a new user is connected, some memory must be allocated or a valid pointer must be provided.

    连接新用户时,必须分配一些内存或必须提供有效指针。

      for(int i=0;i<10;i++){
            if(users[i]==NULL){
                    users[i]=malloc(1*sizeof(struct user));
                    if(users[i]==NULL){printf("malloc failed\n");exit(1);}
                    users[i]->id=user.id;
                    printf("user %d connected!\n", user.id);
                    nUsers++;
                    break;
            }
      }
    

    Using users[i]=&user; would not be a good idea, because user is a local variable : it does not exist out of the function connectUser(struct Users user). If you do so, it can trigger an undefined behavior somewhere else.

    使用用户[i] =&user;这不是一个好主意,因为user是一个局部变量:它不存在于函数connectUser(struct Users user)之外。如果这样做,它可以在其他地方触发未定义的行为。

    As the user is disconnected, the memory must be freed and the pointer must be set to NULL : free(users[i]);users[i]=NULL;

    当用户断开连接时,必须释放内存并且必须将指针设置为NULL:free(users [i]); users [i] = NULL;


推荐阅读
author-avatar
广东没有名字的世界
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有