0

I have the following struct:

struct clientDetails 
{
    int socket;  
    char* port;
    char* IP;
    char* hostName;
    int msgSentCount;
    int msgRecvCount;
    char* status;
    char bufferMsg[BUFFER_SIZE];
    char blockedUser[4];
    int blockedCount;   
};

And I have an array of pointers:

struct clientDetails* allClients[4];

How can I initialize all the array elements of allClients to have default values?

I have tried the following but I am getting 'incomplete definition of type struct':

for(int i = 0; i<4; i++) {
        allClients[i]->socket = 0;  
        allClients[i]->port = NULL;
        allClients[i]->IP = NULL;
        allClients[i]->hostName = NULL;
        allCLients[i]->msgSentCount = 0;
        allClients[i]->msgRecvCount = 0;    
        allClients[i]->status = NULL;
        allClients[i]->bufferMsg = "";
        allClients[i]->blockedUser = {"","","",""};
        allClients[i]->blockedCount = 0;    
}
4
  • On what line is the error? Provide a minimal self-containing example, so we could reproduce the problem. Commented Oct 7, 2017 at 16:10
  • You can initialize a pointer by making it point to something. BTW: Are you using a C++compiler? Commented Oct 7, 2017 at 16:12
  • Yes. I am using gcc. Commented Oct 7, 2017 at 16:16
  • 2
    This: 'allClients[i]->blockedUser = {"","","",""};' is initializing with four empty strings rather than four chars. Commented Oct 7, 2017 at 16:30

1 Answer 1

2

For one thing, you need to allocate storage for that struct... right now all you have a single pointer. Right at the top of your loop, you should do something like:

allClients[i] = malloc(sizeof(clientDetails));

It's been a while since I've done structs in "C", but you could/should probably typedef your struct as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, if you use calloc (instead of malloc) all this initialization in your case is done for you.

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.