可以使用array [] = malloc()编辑吗? [英] Can array[]=malloc()ed?

查看:64
本文介绍了可以使用array [] = malloc()编辑吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我们不能malloc一个数组吗?

所以,我尝试了以下代码:


int main( void)

{

unsigned int y [3] = {1,3,6},i,j;

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

printf(" before =%d \ n",y [i]);

* y = 7; / * 1 * /

for(j = 0; j <3; j ++)

printf(" after =%d \ n",y [j] );

返回0;

}


在上面我在1指示的行中取消了* y(如果我在我用
使用了一个错误的术语,因为指针是以这种方式取消的。但是上面的

我使用了数组,但是因为
$ b而无法作为指针$ b数组总是衰减为指针。)数组的第一个元素值

发生变化。考虑到这是一种证明* y指向数组中的第一个元素,我想要对数组y进行malloc。我尝试了各种语法

但我只收到警告。

* y = malloc(sizeof y);

尽管我有警告编译代码和代码我没有

有任何运行时错误。有人可以告诉我正确的语法吗?

通过上面的malloc数组的'第一个元素'的mem得到

分配,我是否正确?


我使用多维数组会发生什么?

解决方案

da *********** @ yahoo.com 写道:

朋友,
我们不能malloc一个数组?
所以,我尝试了下面的代码:

int main(void)
{
unsigned int y [3 ] = {1,3,6},i,j;
for(i = 0; i< 3; i ++)
printf(" before =%d \ n",y [i ]);
* y = 7; / * 1 * /
for(j = 0; j <3; j ++)
printf(" after =%d \ n",y [j]);
返回0 ;


在上面我在1表示的行中取消了* y(如果我使用了错误的术语,请更正我,因为指针是以这种方式取消的。但是<在上面我已经使用了数组,但是因为数组总是衰减成指针而被指代为指针。)数组的第一个元素值
会发生变化。考虑到这是一种证明* y指向数组中的第一个元素我想要对数组y进行malloc。我尝试了各种语法,但我只收到警告。
* y = malloc(sizeof y);


因为y是一个指针而且* y是一个整数,所以编译器抱怨你要将一个指针从malloc()赋给一个int。


试试这个:

#define QUANTITY 4

int * x; / *每行一个变量是首选* /

x = malloc(QUANTITY * sizeof(int));

/ *或* /

x = malloc(QUANTITY * sizeof(* x));


我不确定你是否被允许使用y指向什么

else在上面的代码中。我知道这不是一个安全的做法

,因为当你为''y'分配一个新数组时,原始数组将会丢失。\\ b
(动态分配)。


尽管有警告我编译了代码和代码我没有任何运行时错误。有人可以告诉我正确的语法吗?
通过上面的malloc数组的'第一个元素'的mem得到
分配,我是否正确?
编号malloc函数返回一个动态分配的内存指针

内存;不是记忆本身。

我们使用多维数组会发生什么?



你需要更多空间。

阅读下面的C FAQ解释了如何为
a多维数组分配内存。


-

Thomas Matthews

C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top。 html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍


< blockquote> da***********@yahoo.com 写道:< blockquote class =post_quotes>
朋友们,
我们不能malloc一个数组吗?
所以,我尝试了以下代码:

int main(void) printf(") before =%d \ n",y [i]);
* y = 7; / * 1 * /
for(j = 0; j <3; j ++)
printf(" after =%d \ n",y [j]);
返回0 ;


在上面我在1表示的行中取消了* y(如果我使用了错误的术语,请更正我,因为指针是以这种方式取消的。但是<在上面我已经使用了数组,但是因为数组总是衰减成指针而被指代为指针。)数组的第一个元素值
会发生变化。考虑到这是一种证明* y指向数组中的第一个元素我想要对数组y进行malloc。我尝试了各种语法,但我只收到警告。
* y = malloc(sizeof y);


如果仍然如上所示声明'y'','* y''是'int''。

malloc()不返回一个int值; malloc()返回一个

指针。你不能将指针值存储在'int''变量中。

尽管有警告我编译了代码和代码我没有任何运行时错误。


在给你诊断后,编译器显然是

试图通过将你的代码改为正确的形式来帮助你,

也许类似于`* y =(int)malloc(sizeof y)''。如果这是发生了什么,结果就是强行将malloc()返回的指针

转换成相应的`int''值,然后存储

转换为'* y''的值。转换可能是也可能不是
有意义;它甚至可能不会产生合法的int值;

你的程序在这一点上可能完全爆炸 - 或者它/ b $ b可能看起来做了一些合理的事情。现在的代码是

不正确,并且真的没有告诉它可能会做什么。

有人能告诉我正确的语法吗?


不,因为你还没有解释你想要的东西

呢。编译器(显然)选择了一次尝试的修正

来从你的代码中获得一些感觉,但我和你的真实意图一样无知

编译器和我的更正

不太可能比编译器更合适。

我们都不会读你的想法。

通过上面的malloc分配数组的第一个元素'mem mem
,我是否正确?


对不起:我根本无法理解这个问题。

我使用多维数组会发生什么?




一切都取决于你如何使用它。


你对阵列和内存分配的理解似乎是不完整的b / b
。我建议您在comp.lang.c中学习第6节和第7节

常见问题(FAQ)列表

http://www.eskimo.com/~scs/C-faq/top.html


第4节和第5节也可能有所帮助。祝你好运!


-
Er ***** ****@sun.com


Thomas Matthews写道:

da *********** @ yahoo.com 写道:

朋友们,
我们不能malloc一个阵列吗?
所以,我尝试了下面的代码:

int main(void)
{
unsigned int y [3] = {1,3,6},i,j;
for(i = 0; i< 3; i ++)
printf(" before =%d \ n", y [i]);
* y = 7; / * 1 * /
for(j = 0; j <3; j ++)
printf(" after =%d \ n",y [j]);
返回0 ;
}
在上面我在1表示的行中取消了* y(如果我使用了错误的术语,请更正我,因为指针是以这种方式取消的。但是
in上面我已经使用了数组,但是因为数组总是衰减成指针而被引用为指针。)数组的第一个元素值
会发生变化。考虑到这是一种证明* y指向数组中的第一个元素我想要对数组y进行malloc。我尝试了各种语法,但我只收到警告。
* y = malloc(sizeof y);

我不确定你是否被允许使用y指向上面代码中的其他东西。



不,他不能。数组不是有效的左值,这意味着它永远不会出现在赋值运算符的左侧。如果你想要
调整数组的大小,你必须从一开始就使用堆内存。


他能做的是类似于:

unsigned int * y,i,j;

y = malloc(3 * sizeof(int));

y [0] = 1 ; y [1] = 3; y [2] = 6;

for(i = 0; i< 3; i ++)print y [i];

y = realloc(y,4 * sizeof (int));

y [3] = 7; // * y = 7实际上是y [0] = 7.

for(j = 0; j <4; j ++)print y [j];


指针语法和数组语法是可以互换的,只要你使用的是b
指针即可。如果它是一个数组,你仍然可以使用* arr访问

,但你不能以任何方式操纵arr。

我使用多维数组会发生什么?




事情变得复杂。


NR


Friends,
cannot we malloc a array?
So, I tried the following code:

int main(void)
{
unsigned int y[3]={1,3,6},i,j;
for(i=0;i<3;i++)
printf("before =%d\n",y[i]);
*y = 7; /* 1*/
for(j=0;j<3;j++)
printf("after =%d\n",y[j]);
return 0;
}

In the above I derefered *y in the line indicated by1 (correct me if I
have used a wrong term since pointers are derefered in this way. But
in the above I have used array but derefered as a pointer since
an array always decays into pointers.) the array''s first element value
changes. Considering this as a kind of proof that *y pointes to the
first element in a array I wanted to malloc the array y. I tried
various syntax but I only get warnings.
*y = malloc(sizeof y);
In spite of the warnings I have compiled the code and the code I not
have any run time error. Can somebody tell me the correct syntax?
By the malloc of above the array''s first element''s mem get
allocated, Am I correct?

What happens I we use a multi dimensional array?

解决方案

da***********@yahoo.com wrote:

Friends,
cannot we malloc a array?
So, I tried the following code:

int main(void)
{
unsigned int y[3]={1,3,6},i,j;
for(i=0;i<3;i++)
printf("before =%d\n",y[i]);
*y = 7; /* 1*/
for(j=0;j<3;j++)
printf("after =%d\n",y[j]);
return 0;
}

In the above I derefered *y in the line indicated by1 (correct me if I
have used a wrong term since pointers are derefered in this way. But
in the above I have used array but derefered as a pointer since
an array always decays into pointers.) the array''s first element value
changes. Considering this as a kind of proof that *y pointes to the
first element in a array I wanted to malloc the array y. I tried
various syntax but I only get warnings.
*y = malloc(sizeof y);
Since y is a pointer and *y is an integer, the compiler complained
that you were assigning a pointer from malloc() to an int.

Try this:
#define QUANTITY 4
int * x; /* one variable per line is preferred */
x = malloc(QUANTITY * sizeof(int));
/* or */
x = malloc(QUANTITY * sizeof(*x));

I''m not sure whether you are allowed to use y to point to something
else in your above code. I do know that it is not a safe practice
since the original array will be lost when you assign a new array
(dynamically allocated) to ''y''.

In spite of the warnings I have compiled the code and the code I not
have any run time error. Can somebody tell me the correct syntax?
By the malloc of above the array''s first element''s mem get
allocated, Am I correct? No. The malloc function returns a pointer to dynamically allocated
memory; not the memory itself.

What happens I we use a multi dimensional array?


You need more space.
Read the C FAQ below which explains how to allocate memory for
a multi-dimensional array.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


da***********@yahoo.com wrote:


Friends,
cannot we malloc a array?
So, I tried the following code:

int main(void)
{
unsigned int y[3]={1,3,6},i,j;
for(i=0;i<3;i++)
printf("before =%d\n",y[i]);
*y = 7; /* 1*/
for(j=0;j<3;j++)
printf("after =%d\n",y[j]);
return 0;
}

In the above I derefered *y in the line indicated by1 (correct me if I
have used a wrong term since pointers are derefered in this way. But
in the above I have used array but derefered as a pointer since
an array always decays into pointers.) the array''s first element value
changes. Considering this as a kind of proof that *y pointes to the
first element in a array I wanted to malloc the array y. I tried
various syntax but I only get warnings.
*y = malloc(sizeof y);
If `y'' is still declared as shown above, `*y'' is an `int''.
malloc() does not return an `int'' value; malloc() returns a
pointer. You cannot store a pointer value in an `int'' variable.
In spite of the warnings I have compiled the code and the code I not
have any run time error.
After giving you the diagnostic, the compiler apparently
tried to help you by altering your code to a correct form,
perhaps something like `*y = (int)malloc(sizeof y)''. If this
is what happened, the result was to forcibly convert the pointer
returned by malloc() into a corresponding `int'' value, and store
that converted value in `*y''. The conversion might or might not
make sense; it might not even produce a legitimate `int'' value;
your program might perfectly well explode at this point -- or it
might appear to do something sensible. The code as it stands is
incorrect, and there''s really no telling what it might do.
Can somebody tell me the correct syntax?
No, because you haven''t explained what you are trying to
do. The compiler (apparently) chose a correction in an attempt
to get some sense out of your code, but I am just as ignorant
as the compiler about your true intention, and my "correction"
is no more likely to be the right one than the compiler''s was.
Neither of us can read your mind.
By the malloc of above the array''s first element''s mem get
allocated, Am I correct?
I''m sorry: I cannot understand this question at all.
What happens I we use a multi dimensional array?



Everything depends on how you use it.

Your understanding of arrays and of memory allocation seems
to be incomplete. I recommend that you study Sections 6 and 7
in the comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

Sections 4 and 5 might also be helpful. Good luck!

--
Er*********@sun.com


Thomas Matthews wrote:

da***********@yahoo.com wrote:

Friends,
cannot we malloc a array?
So, I tried the following code:

int main(void)
{
unsigned int y[3]={1,3,6},i,j;
for(i=0;i<3;i++)
printf("before =%d\n",y[i]);
*y = 7; /* 1*/
for(j=0;j<3;j++)
printf("after =%d\n",y[j]);
return 0;
}
In the above I derefered *y in the line indicated by1 (correct me if I
have used a wrong term since pointers are derefered in this way. But
in the above I have used array but derefered as a pointer since
an array always decays into pointers.) the array''s first element value
changes. Considering this as a kind of proof that *y pointes to the
first element in a array I wanted to malloc the array y. I tried
various syntax but I only get warnings.
*y = malloc(sizeof y);

I''m not sure whether you are allowed to use y to point to something
else in your above code.



No, he cannot. An array is not a valid lvalue, meaning it can never
appear on the left hand side of an assignment operator. If you want to
resize an array you must be using heap memory from the start.

What he can do is something akin to this:

unsigned int *y, i, j;
y = malloc(3 * sizeof(int));
y[0] = 1; y[1] = 3; y[2] = 6;
for (i = 0; i < 3; i++) print y[i];
y = realloc(y, 4 * sizeof(int));
y[3] = 7; // *y = 7 actually assignes y[0] = 7.
for (j = 0; j < 4; j++) print y[j];

Pointer syntax and array syntax are interchangeable as long as what you
are working with is a pointer. If it is an array you can still access
with *arr but you cannot manipulate arr in any way.
What happens I we use a multi dimensional array?



Things get complicated.

NR


这篇关于可以使用array [] = malloc()编辑吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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