Array of void pointers in C

妖精的绣舞 提交于 2019-12-24 04:51:28

问题


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,


回答1:


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.




回答2:


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



回答3:


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;



回答4:


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;


来源:https://stackoverflow.com/questions/7262012/array-of-void-pointers-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!