这有什么问题 [英] what is wrong with this

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

问题描述



我已经声明了指针的指针** a;

所以在循环中我将一个块分配给指针并在其中放置一个值

然后我想打印这些值。


我的以下程序无效。


另外如何做同样的事情程序用int * a []; (指针数组)。

任何帮助都会有所帮助。


#include< stdio.h>

#include < conio.h>

#include< malloc.h>


void main()

{

int ** a;

int v = 0;


for(v = 0; v< 5; v ++)

{

*(a + v)=(int *)malloc(sizeof(int));

**(a + v)= v ;

}


for(v = 0; v< 5; v ++)

{

printf("%d \ n",**(a + v));

}


退出(0);


}

Hi,
I have declared pointer of pointers **a;
so In a loop I assign a block to a pointer and put a value in it
and then I want to print these values.

My following program doesnt work.

Also How to do same program with int *a[]; (array of pointers).
Any help would really help.

#include <stdio.h>
#include <conio.h>
#include <malloc.h>

void main()
{
int **a;
int v=0;

for (v=0;v<5;v++)
{
*(a+v)=(int *)malloc(sizeof(int));
**(a+v)=v;
}

for (v=0;v<5;v++)
{
printf("%d\n",**(a+v));
}

exit(0);

}

推荐答案

fr ******* @ yahoo.com 在新闻中写道:1140783951.891486.192110

@ t39g2000cwt.googlegroups.com:
fr*******@yahoo.com wrote in news:1140783951.891486.192110
@t39g2000cwt.googlegroups.com:
任何帮助都会有所帮助。


好​​吧,那么。

#include< stdio.h>
#include< conio.h>


conio.h是一个非标准的标题。不要看下面有任何非标准的

构造,所以省略这个。

#include< malloc.h>


malloc.h是一个非标准的标题。你应该:


#include< stdlib.h>

$ c $ b for malloc。

void main()


这是main的非标准原型。在这种情况下,你应该使用:


int main(无效)

{
int ** a;
int v = 0;

for(v = 0; v< 5; v ++)
{
*(a + v)=(int *)malloc(sizeof(int));


不要施放malloc的返回值。它可以隐藏不包括

stdlib.h。

**(a + v)= v;


BAM!一个本身指向无处。它应该指向一大块内存

足以容纳五个指向int的指针。

}

for(v = 0; v< ; 5; v ++)
{
printf("%d \ n",**(a + v));
}

退出(0 );
Any help would really help.
OK, then.
#include <stdio.h>
#include <conio.h>
conio.h is a non-standard header. Don''t see any need for any nonstandard
constructs below, so omit this.
#include <malloc.h>
malloc.h is a non-standard header. You should:

#include <stdlib.h>

for malloc.
void main()
This is a non-standard prototype for main. In this case, you should use:

int main(void)
{
int **a;
int v=0;

for (v=0;v<5;v++)
{
*(a+v)=(int *)malloc(sizeof(int));
Don''t cast the return value of malloc. It can hide failure to include
stdlib.h.
**(a+v)=v;
BAM! a itself points to nowhere. It should point to a chunk of memory
large enough to hold exactly five pointers to int.
}

for (v=0;v<5;v++)
{
printf("%d\n",**(a+v));
}

exit(0);




返回0;

}


更传统。


思南

-

-

A. Sinan Unur< 1u ** @ llenroc.ude .invalid>

(反转每个组件并删除.invalid的电子邮件地址)



return 0;
}

is more conventional.

Sinan
--
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)


也许我知道你在哪里弄错了。你宣布

int ** a;

你能看到上面的声明吗,它说的是什么?它表示a是

指向指向int的指针。所以a足够大,可以保持一个指向

整数的指针。但是在你的代码中,你认为它足够大,可以保持5个指针

到整数,即。 a不是指向int的5个指针的数组,而只是指向int的指针的
(单个)指针。为了使你的程序工作,我们必须




int ** a;

a =(int **)malloc( 5 * sizeof(int *));


我希望现在情况清楚。

Perhaps i know where you have got this wrong. You declared
int **a;
can you read the above declaration, what does it say?? It says "a is a
pointer to pointer to int". So a is large enough to hold one pointer to
integer. But in your code you assume it large enough to hold 5 pointers
to integer, ie. a is not an array of 5 pointers to int, but just a
(single)pointer to pointer to int. To make your program work we have to
say

int **a;
a = (int **)malloc(5 *sizeof(int *));

I hope things are clear now.


" pr **************@gmail.com" < PR ************** @ gmail.com>写在

新闻:11 ********************** @ i40g2000cwc.googlegr psps.com:
"pr**************@gmail.com" <pr**************@gmail.com> wrote in
news:11**********************@i40g2000cwc.googlegr oups.com:
也许我知道你哪弄错了。


谁弄错了什么?请参阅< URL:http://cfaj.freeshell.org/google/>

int ** a;
a =(int **)malloc(5 * sizeof(int *));


#include< stdlib.h>


int ** a = malloc(5 * sizeof(* a));


如果(a){


}


转换malloc的返回值可以隐藏错误到期没有

包括stdlib.h。


通过使用上面的表格,即使你是$ b $,malloc通话也是一样的b改变a的类型。

我希望现在情况很清楚。
Perhaps i know where you have got this wrong.
Who got what wrong? See <URL:http://cfaj.freeshell.org/google/>
int **a;
a = (int **)malloc(5 *sizeof(int *));
#include <stdlib.h>

int **a = malloc(5 * sizeof(*a));

if ( a ) {

}

Casting the return value of malloc can hide errors due to failure to
include stdlib.h.

By using the form above, the malloc call remains the same even if you
change the type of a.
I hope things are clear now.




不是真的。


思南


-

A. Sinan Unur< 1u ** @ llenroc.ude.invalid>

(反转每个组件并删除.invalid for email address)



Not really.

Sinan

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)


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

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