开发者

Array of void pointers in C

开发者 https://www.devze.com 2023-04-01 12:12 出处:网络
I would like to create arrays of void pointers. # include <stdio.h> # include <stdlib.h> # include <unistd.h>

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 i开发者_如何学编程s 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,


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.


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};


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;


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消