0

I have two structures as shown below

struct server{
    // some members
};

struct msg{
    struct server* servers;
};

Then I do this.

struct msg msg1;
struct server s1,s2;

msg1.servers = (struct server *)malloc(2*sizeof(struct server));
msg1.servers[0] = &s1; // compilation error 
msg1.servers[1] = &s2;  // compilation error

This code does not compile and giving the following error : incompatible types when assigning to type ‘struct server’ from type ‘struct server *’.

What am I doing wrong?

5 Answers 5

3

The problem here is that the expression msg1.servers[0] producets a struct server but you're providing a struct server* (a pointer type vs a non-pointer type). There are two ways to fix this

The first is to simply provide the struct server instances by value as the code expects

msg1.servers[0] = s1;
msg1.servers[1] = s2;

This will work if struct server is a type that behaves properly when copied around.

The second is necessary if you want to continue using struct server* in the struct msg. In this case you need a double pointer to store the server pointers. And you need to adjust your malloc statement appropriately

struct msg{
    struct server** servers;
};

msg1.servers = malloc(2*sizeof(struct server*));
msg1.servers[0] = &s1; 
msg1.servers[1] = &s2;
Sign up to request clarification or add additional context in comments.

Comments

2

You don't need the ampersands:

msg1.servers[0] = s1;
msg1.servers[1] = s2;

Saying &s1 would give you a pointer to a struct server, but msg1.servers[0] is a single element in the array you just allocated.

Comments

1

msg.servers is a pointer to the type server. When you use msg.servers[0] you're dereferencing the pointer, so its type is now server, not server *, obviously you cannot assign the address of a server instance to it.

You probably want the following:

struct msg{
  struct server** servers;
};

struct msg msg1;
struct server s1,s2;

msg1.servers = malloc(2 * sizeof(struct server *));
msg1.servers[0] = &s1;
msg1.servers[1] = &s2;

Comments

0
msg1.servers[0] = &s1;

An array subscript implicitly dereferences the pointer (a[i] == *(a + i), so the type of the expression msg1.servers[0] is struct server, not struct server *.

Change your struct msg definition to

struct msg {
  struct server **servers; // servers will be an array of pointers
};

and the malloc call to

msg1.servers = malloc(2 * sizeof *msg1.servers); 

and then the assignments will work.

EDIT

Or, like everyone else is pointing out, drop the & from the assignment. It depends on what you intend servers to represent; is it an array of struct server, or an array of pointers to struct server?

Comments

0

You need a array of pointers

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    struct server{
        // some members
    };  

    struct msg{
        struct server ** servers;
    };  

    struct msg msg1;
    struct server s1,s2;

    if ((msg1.servers = malloc(2 * sizeof(struct server *))) == NULL) {
        printf("unable to allocate memory \n");
        return -1; 
    }   
    msg1.servers[0] = &s1; // compilation error 
    msg1.servers[1] = &s2;  // compilation error

    free(msg1.servers);

    return 0;
}

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.