有可能构建一些指针数组吗? [英] is possible to build some pointer array?

查看:61
本文介绍了有可能构建一些指针数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部:


我想记录一些从malloc返回的内存指针,可能是

如下代码?


int memo_index [10];

int i,j;

char * tmp;


for(i = 0; i< 10; i ++){

tmp = malloc(10);

memo_index [i] = tmp;

}


我想要实现的是使用memo_index [10]记录每个
可用内存指针,然后我只使用

memo_index [i]引用已分配内存中安装的每个东西

/


是否有可能或其他方式?


感谢您的任何评论。


bin

解决方案

yezi写道:

我想记录一些从malloc返回的内存指针,是否可能如下代码?

int memo_index [10];


你想在memo_index中记录变量的地址所以声明

它作为指针。

int * memo_index [ 10]。

int i,j;
char * tmp;

for(i = 0; i< 10; i ++){
tmp = malloc(10);
memo_index [i] = tmp;
}




malloc将返回一个void指针,因此输入它并分配它是

memo_index。

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

memo_index [i] =(int *)malloc (10);

类型转换取决于你想要使用指针的方式。


ru ******** @ rediffmail.com 写道:

[...]

malloc将返回一个void指针,因此键入它并将其分配给
memo_index。
for(i = 0; i< 10; i ++)
memo_index [i] = (int *)malloc(10);
类型转换取决于你想要使用指针的方式。




不,不,不。


malloc()返回void *类型的结果,它将隐式地将
转换为任何指向对象的指针类型。转换结果是不必要的,并且可以掩盖某些错误,例如忘记

" #include< stdlib.h>"或使用C ++编译器。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



ru * *******@rediffmail.com 写道:

yezi写道:

我想记录一些从malloc返回的内存指针,有可能
下面的代码?

int memo_index [10];



你想在memo_index中记录一个变量的地址所以声明
它作为指针。
int * memo_index [10]。

int i,j;
char * tmp;

for(i = 0; i< 10; i ++){
tmp = malloc(10);
memo_index [i] = tmp;
}



malloc将返回一个void指针,因此键入它并将其分配给
memo_index。
for(i = 0; i< 10; i ++)
memo_index [i] =(int * )malloc(10);
类型转换取决于你想要使用指针的方式。




因为tmp是一个char,它应该是:


c har * memo_index [10];


也就是说,一个包含10个char指针的数组。虽然指针都是相同大小的

,但是除非你真的需要,否则不要指向来自不同类型

指针的不同类型数据。


Hi, all:

I want to record some memory pointer returned from malloc, is possible
the code like below?

int memo_index[10];
int i,j;
char *tmp;

for (i=0;i<10;i++){
tmp = malloc(10);
memo_index[i] = tmp;
}

what I want to realize is "use the memo_index [10] to record each
usable memory pointer, then I just use
memo_index[i] to refer to each stuff installed in the allocated memory
/

Is it possible or some else way?

Thanks for any comments.

bin

解决方案

yezi wrote:

I want to record some memory pointer returned from malloc, is possible
the code like below?

int memo_index[10];
you want to record the address of a variable in memo_index so declare
it as a pointer.
int *memo_index[10].
int i,j;
char *tmp;

for (i=0;i<10;i++){
tmp = malloc(10);
memo_index[i] = tmp;
}



malloc will return a void pointer so type cast it and assign it to
memo_index.
for (i=0;i<10;i++)
memo_index[i]=(int *)malloc(10);
type casting depends on the way you want to use pointer.


ru********@rediffmail.com writes:
[...]

malloc will return a void pointer so type cast it and assign it to
memo_index.
for (i=0;i<10;i++)
memo_index[i]=(int *)malloc(10);
type casting depends on the way you want to use pointer.



No, no, no.

malloc() returns a result of type void*, which will be implicitly
converted to any pointer-to-object type. Casting the result is
unnecessary, and can mask certain errors such as forgetting to
"#include <stdlib.h>" or using a C++ compiler.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.



ru********@rediffmail.com wrote:

yezi wrote:

I want to record some memory pointer returned from malloc, is possible
the code like below?

int memo_index[10];



you want to record the address of a variable in memo_index so declare
it as a pointer.
int *memo_index[10].

int i,j;
char *tmp;

for (i=0;i<10;i++){
tmp = malloc(10);
memo_index[i] = tmp;
}



malloc will return a void pointer so type cast it and assign it to
memo_index.
for (i=0;i<10;i++)
memo_index[i]=(int *)malloc(10);
type casting depends on the way you want to use pointer.



since tmp is a char, it should really be:

char *memo_index[10];

That is, an array of 10 char pointers. Although pointers are all the
same size, don''t point to different typed data from different typed
pointers unless you really need to.


这篇关于有可能构建一些指针数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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