vallllll
vallllll

Reputation: 2761

Array of void pointers in C

I would like to create arrays of void pointers.

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

int main(){

void * A[3];
void * D[3];
void * F[2];
void * G[4];
void * H[4];
void * J[3];
void * K[5];
void * L[4];
void * M[5];


A={D, H, K};
D ={A, G, H};
F ={K, L};
G={D, H, J, M};
H={A, G, L, M};
J={G, L, M};
K={A, F, H, L, M};
L={F, J, K, M};
M={G, H, J, K, L};
return 0;
}

The problem is the code won't compile it says: "expected expression before { token"

What is wrong? I am using these pointers because their value doesn't matter I just want them to point to each other. For example M has to point to G, H, J, K and L.

Thank you very much for any help or advice,

Upvotes: 2

Views: 9363

Answers (4)

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43558

Arrays are not allowed on the left side of an assignment except when declared, and array initializers could be used only during declarations as well.

So you would have to write for example:

A[0] = D;
A[1] = H;
A[2] = K;

Upvotes: 2

Hogan
Hogan

Reputation: 70538

This is the way C works.

I believe you want code that looks like this:

A[0] = D;
A[1] = H;
A[2] = K; 

or code that looks like this:

void * A[3]={D,H,K};

Upvotes: 4

David Heffernan
David Heffernan

Reputation: 613521

You can't use array initializers as assignments. You'll need to do the assignments element wise:

A[0] = D;
A[1] = H;
A[2] = K;

Upvotes: 2

Jesus Ramos
Jesus Ramos

Reputation: 23266

You have to do it on the same line if you want to use that notation. In your case you would have to do

A[0] = D;
A[1] = H;
A[2] = K;

and so on because you need to add void * to symbols that havent been resolved at that point yet.

Upvotes: 6

Related Questions