简体   繁体   English

C语言中的结构和指针问题

[英]Problem with Struct and Pointer in C language

I'm newbie. 我是新手。 Pointer make my crazy T_T. 指针使我发疯。 Now, I'm do the socket programming project. 现在,我正在做套接字编程项目。 This is my code. 这是我的代码。

typedef struct {
        char *ip[INET6_ADDRSTRLEN];
        char *username[20];
        time_t login_time;
        enum client_state client_state;
        int no_login;
    } Client;

    Client client[max_connections] = {}; // set to null
char remoteIP[INET6_ADDRSTRLEN];
.
.
.
.
.
if(client[new_fd-4] == NULL) {  // if fist attempt, client always null
   // I want to setting client[new_fd-4].ip = &remoteIP
   // How to write the code ??
}

{} does not mean "null", it means "zero-initialized". {}并不表示“空”,而是表示“零初始化”。

You can't put null values into the client array because client is an array of structs, and structs can't be null. 您不能将null值放入client数组,因为client是一个结构数组,而structs不能为null。 If you want the client array to be able to contain "null" values, you need to make it an array of pointers to structs , eg Client* client[max_connection] = {}; 如果您希望client数组能够包含“空”值,则需要使其成为指向结构指针的数组,例如Client* client[max_connection] = {}; . This sets all the values in client to NULL because when talking about pointers, 0 and NULL are synonyms. 这会将client所有值设置为NULL因为在谈论指针时, 0NULL是同义词。 When pointers aren't involved, this is not true because nothing other than a pointer can be null. 当不涉及指针时,这是不正确的,因为除指针外其他任何东西都不能为空。

Note that since client will now contain pointers, you will have to allocate and deallocate the Client structs with malloc and free , eg 请注意,由于client现在将包含指针,因此您将必须使用mallocfree来分配和取消分配Client结构,例如

if(client[new_fd-4] == NULL) {  // if fist attempt, client always null
   client[new_fd-4] = malloc(sizeof(Client));
   client[new_fd-4]->no_login = 1; // For example. Note use of -> instead of .
}

And when you're done with some element client[i] : 当您完成了某些元素client[i]

free(client[i]);
client[i] = NULL; // Not strictly necessary, but a good idea

And I really doubt that you want to do this: 我真的怀疑您要这样做:

// I want to setting client[new_fd-4].ip = &remoteIP

I think what you really want is to copy the data contained in remoteIP into the ip member, like this: 我认为您真正想要的是 remoteIP包含的数据复制ip成员中,如下所示:

memcpy(client[new_fd-4]->ip, &remoteIP, INET6_ADDRSTRLEN);

Update: Client client[max_connections] = {}; 更新: Client client[max_connections] = {}; will create an array of Client objects whose all fields are initialized to zeros. 将创建一个Client对象的数组,其所有字段都初始化为零。 So the check should be something like: if(client[new_fd-4].ip == NULL) 因此检查应该类似于: if(client[new_fd-4].ip == NULL)

If remoteIP is a static array, or if it's a local array and client[new_fd - 4] won't be used after the current function is returned, you can just do: 如果remoteIP是静态数组,或者它是本地数组,并且在返回当前函数后将不使用client[new_fd - 4] ,则可以执行以下操作:

client[new_fd - 4].ip = remoteIP;

Otherwise, you should assign memory and do a memcpy. 否则,您应该分配内存并执行memcpy。

client[new_fd - 4].ip = malloc(INET6_ADDRSTRLEN);
strncpy(client[new_fd - 4].ip, remoteIP, INET6_ADDRSTRLEN);

I'm assuming that you didn't mean to define the ip structure member as an array of char* , nor the username structure member. 我假设您不是要将ip结构成员定义为char*数组,也不是将username结构成员定义为。

Try: 尝试:

typedef struct {
    char ip[INET6_ADDRSTRLEN];
    // ...
 } Client;

And in the code: 并在代码中:

strncpy(client[new_fd-4].ip, remote_ip, INET6_ADDRSTRLEN);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM