C 中的 void 指针数组
我想创建空指针数组。
# 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;
}
问题是代码无法编译,它说:“expected expression before { token”
有什么问题吗? 我使用这些指针是因为它们的值并不重要,我只是希望它们彼此指向。例如M必须指向G、H、J、K和L。
非常感谢您的帮助或建议,
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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想使用该表示法,则必须在同一行上执行此操作。在你的情况下,你必须
这样做,因为你需要将 void * 添加到当时尚未解析的符号中。
You have to do it on the same line if you want to use that notation. In your case you would have to do
and so on because you need to add void * to symbols that havent been resolved at that point yet.
这就是 C 的工作方式。
我相信您想要如下所示的代码:
或如下所示的代码:
This is the way C works.
I believe you want code that looks like this:
or code that looks like this:
您不能使用数组初始值设定项作为赋值。您需要明智地进行分配:
You can't use array initializers as assignments. You'll need to do the assignments element wise:
除非在声明时,否则不允许在赋值的左侧使用数组,并且数组初始值设定项也只能在声明期间使用。
所以你必须写例如:
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: