0

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 ??
}
1
  • Start with C basics, pointers are essential in C. Commented Aug 30, 2010 at 13:17

3 Answers 3

5

{} 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. If you want the client array to be able to contain "null" values, you need to make it an array of pointers to structs, e.g. Client* client[max_connection] = {};. This sets all the values in client to NULL because when talking about pointers, 0 and NULL are synonyms. 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, e.g.

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]:

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:

memcpy(client[new_fd-4]->ip, &remoteIP, INET6_ADDRSTRLEN);
Sign up to request clarification or add additional context in comments.

4 Comments

I have a problem with line " memcpy(client[new_fd-4]->ip, &remoteIP, INET6_ADDRSTRLEN); ". Compiler warn me " ../src/server.c:362: warning: passing argument 1 of ‘memcpy’ makes pointer from integer without a cast ". After I run, it terminate after execute this line.
Where you have char* as the type for ip and username, it should just be char. You want an array of characters for those members, not an array of character-pointers. I overlooked this mistake initially.
Sorry, I forgot to set "char *ip[INET6_ADDRSTRLEN]; char *username[20];". It works now. But I found new problem, I just memcpy ip but 'ip' and 'username' change. IP : ::1 username : ::1
Sorry, It my mistake again. I forgot to initialize size of IP and username.
1

I'm assuming that you didn't mean to define the ip structure member as an array of char*, nor the username structure member.

Try:

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

And in the code:

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

Comments

1

Update: Client client[max_connections] = {}; will create an array of Client objects whose all fields are initialized to zeros. So the check should be something like: 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:

client[new_fd - 4].ip = remoteIP;

Otherwise, you should assign memory and do a memcpy.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.