struct中的void *问题 [英] problem with void* in struct

查看:585
本文介绍了struct中的void *问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在C项目中遇到了一些问题。我想要定义一个通用类型的矩阵。使用这种类型我可以保存任何类型的类型(int,double,float ...)。但我的代码中有一些错误。如果有人可以帮助我,我将非常感激。代码是:

hello,
I have some problem with my project in C. I want do define a generic type of matrix. with this type I can save any kind of type(int, double, float...). but I have some errors in my code. if someone can help me I''ll be very gratefull. the code is:

展开 | 选择 < span class =codeDivider> | Wrap | 行号

推荐答案

错误代码中一定有行号吗?

- > p_arr [m]

你不能索引void类型的指针。编译器在编译期间没有关于void *指向的数据性质的信息。把它投射到你知道的某种指针类型,例如

((float *)p_arr)[m]
There must have been line numbers along the error codes?
-> p_arr[m]
You can''t index pointers of type void. Compiler has no information during compile time about the nature of data the void* points to. Cast it to some pointer type you know, e.g.
((float*)p_arr)[m]


1)错误

1> c:\users\reubinoff \documents\visual studio 2010 \projects\test\test\test1.c(33):错误C2036:''void *'':未知大小

1> c:\users \ repubinoff \documents\visual studio 2010 \ project'\\ test\test\test1.c(33):错误C2120:''void' '非法使用所有类型

1> c:\users \ repubinoff \documents\visual studio 2010 \projects\test\test\test1.c(46):错误C2036:''void *'':未知大小

1> c:\ usersrs \ repubinoff \documents\visual studio 2010 \ project'\\ test\test\test1。 c(46):错误C2069:将''void''术语转换为非'''void''


2)当我试图在结构中强制转换编译器时说这不是会员。

要什么Ø?我该怎么做泛型结构
1) the errors
1>c:\users\reubinoff\documents\visual studio 2010\projects\test\test\test1.c(33): error C2036: ''void *'' : unknown size
1>c:\users\reubinoff\documents\visual studio 2010\projects\test\test\test1.c(33): error C2120: ''void'' illegal with all types
1>c:\users\reubinoff\documents\visual studio 2010\projects\test\test\test1.c(46): error C2036: ''void *'' : unknown size
1>c:\users\reubinoff\documents\visual studio 2010\projects\test\test\test1.c(46): error C2069: cast of ''void'' term to non-''void''

2) when I tried to cast the field in the struct the compiler say that is not a member.
what to do? how can I do generic struct


看看 Arrays Revealed ,提供有关如何处理动态分配的二维数组的建议。
Take a look at Arrays Revealed for suggestions on how to handle dynamically allocated 2-dimensional arrays.


这篇关于struct中的void *问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆