多类型 [英] multi-type

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

问题描述

你好,


是否有可能发起一个包含更多类型的数组,比如int''

和char'?我认为可以动态制作一个多元的
维数组,然后对malloc的返回值进行类型转换()

函数:


int ** array =(int **)malloc(ROWS * sizeof(int *));

for(i = 0; i< ROWS; i ++)

array [0] =(int *)malloc(sizeof(int));

array [1] =(char *)malloc(sizeof(char));

array [2] =(float *)malloc(sizeof(float));




这是一种正确的方法吗?

如果不能有人解释?


感谢您的建议。

Hello,

Is there a possibility to initiate an array with more types, say int''s
and char''s? I think it is possible to dynamically make a multi
dimensional array, and then typecast the return value of malloc()
function:

int **array = (int **)malloc(ROWS * sizeof(int *));
for(i = 0; i < ROWS; i++)
array[0] = (int *)malloc(sizeof(int));
array[1] = (char *)malloc(sizeof(char));
array[2] = (float *)malloc(sizeof(float));
etc.

Is this a correct way to do this?
If not could someone explain?

Thanks for your advises.

推荐答案

*********** @ nedap.com 写道:
你好,

是否有可能发起一个包含更多类型的数组,比如int''s
和char''s?我认为可以动态地创建一个多维数组,然后对malloc()
的返回值进行类型转换 - 一般来说这不是一件好事。函数:

int ** array =(int **)malloc(ROWS * sizeof(int *));
for(i = 0; i< ROWS; i ++)
array [0] =(int *)malloc(sizeof(int));
array [1] =(char *)malloc(sizeof(char));
array [2] =( float *)malloc(sizeof(float));


BTW:你真的编译过这个吗?你的'C''编译器说了什么?这是一个正确的方法吗?
否。如果不能有人解释?
array [i]仍然是指向int的指针,因此不能只做你所做的事情。 ...
Hello,

Is there a possibility to initiate an array with more types, say int''s
and char''s? I think it is possible to dynamically make a multi
dimensional array, and then typecast the return value of malloc() - is not a good thing to do in general. function:

int **array = (int **)malloc(ROWS * sizeof(int *));
for(i = 0; i < ROWS; i++)
array[0] = (int *)malloc(sizeof(int));
array[1] = (char *)malloc(sizeof(char));
array[2] = (float *)malloc(sizeof(float));
etc.
BTW: Did you actually compile this? What did your `C'' compiler say? Is this a correct way to do this? No. If not could someone explain? array[i] is still a pointer to int, so can''t just do what you are
doing. ...



也许你可以制作一系列

联盟并让它按你想要的方式工作。


Maybe you can make an array of
unions and make it work the way you want.




我的c编译器没有说什么,但正如你所说它不起作用。

工会分配空间对于工会中最大的类型,这就是为什么我不想使用工会。


也许有人可以帮我解决另一个问题:


NUMBER_OF_CHARS 20

测试=(char **)malloc(ROWS * sizeof(char *));


for(i = 0; i< ROWS; i ++)

{

Test [i] =(char *)malloc(KOLUMS * NUMBER_OF_CHARS * sizeof(char));

}


测试[0] =" test1";

测试[0] [ 20] =" test2";

测试[0] [40] =" test3";


当我现在读取测试开始时的数据在Test [0] [0]它包含

" test1",我认为这是正确的,但是test2从Test [0] [8]开始。我认为它应该从Test [0] [20]开始,我错了吗?


有人对此有解释吗?


感谢您的帮助,

$ b $bAndré


My c compiler didn''t say a thing,but as you say it isn''t working.
Unions allocate space for the biggest type in the union, that is why I
don''t want to use unions.

Maybe someone can help me with another question:

NUMBER_OF_CHARS 20

Test = (char**) malloc(ROWS * sizeof(char*));

for(i=0 ; i<ROWS ; i++)
{
Test[i] = (char*) malloc(KOLUMS * NUMBER_OF_CHARS * sizeof(char));
}

Test[0] = "test1";
Test[0][20] = "test2";
Test[0][40] = "test3";

When I now read the data in Test starting at Test[0][0] it contains
"test1",which is correct I think, but "test2" begins at Test[0][8]. I
think it should start at Test[0][20], am I wrong?

Has someone a explanation for this?

Thanks for your help,

André


On 2005年7月4日星期一00:10:41 -0700,andre.koskamp写道:
On Mon, 04 Jul 2005 00:10:41 -0700, andre.koskamp wrote:
你好,

是否有可能发起更多阵列类型,比如int'的
和char'的?


不,数组的所有元素总是有相同的类型。

我认为可以动态制作多个
维数组,然后类型转换malloc的返回值()
函数:
int ** array =(int **)malloc(ROWS * sizeof(int *));
for(i = 0; i< ROWS; i ++)
array [0] =(int *)malloc(sizeof(int));
array [1] =(char *)malloc (的sizeof(char)的);


在这里你将malloc()的返回值强制转换为char *,然后尝试将char * *
分配给int *类型的对象,该对象无效。

array [2] =(float *)malloc(sizeof(float));

这是一个正确的方法吗?<如果不能有人解释?
Hello,

Is there a possibility to initiate an array with more types, say int''s
and char''s?
No, all of the elements of an array have the same type, always.
I think it is possible to dynamically make a multi
dimensional array, and then typecast the return value of malloc()
function:

int **array = (int **)malloc(ROWS * sizeof(int *));
for(i = 0; i < ROWS; i++)
array[0] = (int *)malloc(sizeof(int));
array[1] = (char *)malloc(sizeof(char));
Here you cast the return value of malloc() to char * then try to assign
that char * to an object of type int * which is invalid.
array[2] = (float *)malloc(sizeof(float));
etc.

Is this a correct way to do this?
If not could someone explain?




C是一种静态类型语言,你看到的就是你得到的。上面的

变量数组具有指向int的指针的类型指针,即它可以指向的是指向int的类型指针对象。就是这样。


你可能会使用一系列联盟更接近你想要的东西。但是,当你访问

元素时,仍然可以选择正确的工会成员,这对你来说不会有神奇的感觉。


劳伦斯



C is a statically typed language, what you see is what you get. The
variable array above has type pointer to pointer to int, i.e. what it can
point at are object of type pointer to int. That''s it.

You might get closer to what you want using an array of unions. But it is
still up to you to select the correct union member when accessing the
elements, this won''t magically happen for you.

Lawrence


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

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