打印“指针数组”的值。 [英] printing values of "arrays of pointers"

查看:84
本文介绍了打印“指针数组”的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目的:看评论。

我有什么:无限循环


/ *这个程序只会创建一个指向整数的指针数组

*并在使用malloc创建

*指针来填充数组时将填充一些值,然后将打印指向的值

* by那些指针

*

* /


#include< stdio.h>

# include< stdlib.h>


enum MAXSIZE {ARRSIZE = 100};

int main(无效)

{

int * arr_p [ARRSIZE];

int ** pp;

int * mp,* np;

int i;


int null_int = 0;


pp = arr_p;

np =& null_int;

for(i = 0; i< ARRSIZE - 1; ++ i)

{

mp = malloc(sizeof(int) );

mp =& i;

arr_p [i] = mp;

}

arr_p [ i] = np;


while(** pp)

{

printf(&q uot; ** pp =%d \ n",** pp);

}


返回0;

}


-
http:// lispmachine.wordpress.com/

我的电子邮件ID是@上面的地址

解决方案

arnuld说:


目的:看评论。

什么我GOT:无限循环


/ *这个程序只会创建一个指向整数的指针数组

*并在使用malloc创建

*指针来填充数组时填充一些值然后将按指针打印指向的值

*

*

* /


#include< stdio.h>

#include< stdlib.h>


enum MAXSIZE {ARRSIZE = 100};


int main(无效)

{

int * arr_p [ARRSIZE];

int ** pp;

int * mp,* np;

int i;


int null_int = 0;


pp = arr_p;

np =& null_int;


for(i = 0;我< ARRSIZE - 1; ++ i)

{

mp = malloc(sizeof(int));



你不要检查这是否未能分配所要求的

内存...


mp =& i;



....这并不重要,因为你把它扔掉了。


arr_p [i] = mp;

}



做得好。你刚刚用一堆指针填充了arr_p。那么为什么要用malloc调用来打扰
呢?你可以通过完全删除malloc调用来节省你自己(ARRSIZE-1)*

sizeof(int)字节的内存泄漏。


arr_p [i] = np;


而(** pp)



pp的值为& arr_p [0] ,所以** pp试图在

* arr_p [0]中找到int值。由于arr_p [0]指向i,我们正在寻找i的值。

该值是不确定的,因为对象我从未有过值

已分配它。因此,行为是不确定的。这个循环可以永远执行
(最可能的可能性)或者根本不执行(可能是第二个很可能是
)或者可能发生的奇怪事情。
< blockquote class =post_quotes>
{

printf(" ** pp =%d \ n",** pp);

}



你甚至没有/尝试/遍历数组,是吗?您可能需要

来仔细查看Visual Basic或RCX。


-

Richard Heathfield< http ://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


2008年5月2日星期五13:44:50 +0000,Richard Heathfield写道:


> for(i = 0; i< ARRSIZE - 1; ++ i)
{
mp = malloc(sizeof(int));


你不能检查这是否无法分配所请求的

内存.. 。



完成


> mp =& i;


...这并不重要,因为你把它扔掉了。



那么,我每次进入

循环时如何生成新变量?


做得好。你刚刚用一堆指针填充了arr_p。那么为什么要用malloc调用来打扰
呢?你可以通过完全删除malloc调用来节省自己(ARRSIZE-1)*

sizeof(int)字节的内存泄漏。



我可以,但我没有,因为我想了解如何指向指针的
数组指针并学习它们。


你甚至没有/尝试/遍历数组,是吗?



好​​的,这是一个小修复版本。我不能做任何关于变量的事情,因为我说,我不能找到一种方法来生成新变量

每次迭代通过循环。


您可能需要仔细查看Visual Basic



http://lispmachine.wordpress.com/200...urn-for-vbnet/


或RCX。



从未听说过它

-
http://lispmachine.wordpress.com/

我的电子邮件ID是@上面的地址




" arnuld" < No **** @ microsoft.comwrote in message

news:pa ************************** **@microsoft.com。 ..


目的:看评论。

什么我GOT:无限循环



我修改了你的代码,使其表现得更好:

#include< stdio.h>

#include< stdlib.h>


enum MAXSIZE {ARRSIZE = 100};

int main(无效)

{

int * arr_p [ARRSIZE];

int ** pp;

int * np;

int i;


int null_int = 0;


pp = arr_p;

np =& null_int;

for(i = 0; i< ARRSIZE - 1; ++ i)

{

arr_p [i] = malloc(sizeof(int));

* arr_p [i] = 1000 + i; / *将一些可识别的初始值初始化为

(和非0!)值* /

}

arr_p [i] = np; / * sentinel(* np包含0)* /


while(** pp)/ *使用pp * /

{

printf(" ** pp =%d \ n",** pp);

++ pp;

}


返回0;

}


- Bartc


PURPOSE: see the comments.
WHAT I GOT: infinite loop

/* This program will simply create an array of pointers to integers
* and will fill it with some values while using malloc to create
* pointers to fill the array and then will print the values pointed
* by those pointers
*
*/

#include <stdio.h>
#include <stdlib.h>

enum MAXSIZE { ARRSIZE = 100 };
int main( void )
{
int* arr_p[ARRSIZE];
int** pp;
int *mp, *np;
int i;

int null_int = 0;

pp = arr_p;
np = &null_int;
for( i = 0; i < ARRSIZE - 1; ++i )
{
mp = malloc( sizeof( int ) );
mp = &i;
arr_p[i] = mp;
}
arr_p[i] = np;

while( **pp )
{
printf("**pp = %d\n", **pp);
}

return 0;
}

--
http://lispmachine.wordpress.com/
my email ID is @ the above address

解决方案

arnuld said:

PURPOSE: see the comments.
WHAT I GOT: infinite loop

/* This program will simply create an array of pointers to integers
* and will fill it with some values while using malloc to create
* pointers to fill the array and then will print the values pointed
* by those pointers
*
*/

#include <stdio.h>
#include <stdlib.h>

enum MAXSIZE { ARRSIZE = 100 };
int main( void )
{
int* arr_p[ARRSIZE];
int** pp;
int *mp, *np;
int i;

int null_int = 0;

pp = arr_p;
np = &null_int;
for( i = 0; i < ARRSIZE - 1; ++i )
{
mp = malloc( sizeof( int ) );

You don''t check to see whether this fails to allocate the requested
memory...

mp = &i;

....which doesn''t really matter, since you throw it away here.

arr_p[i] = mp;
}

Well done. You just filled arr_p with a bunch of pointers to i. Why bother
with the malloc call, then? You could save yourself (ARRSIZE-1) *
sizeof(int) bytes of memory leak by removing the malloc call completely.

arr_p[i] = np;

while( **pp )

pp has the value &arr_p[0], so **pp attempts to find the int value at
*arr_p[0]. Since arr_p[0] points to i, we''re looking for the value of i.
That value is indeterminate, because the object i never had a value
assigned to it. Thus, the behaviour is undefined. This loop could execute
forever (the most likely possibility) or not at all (probably the second
most likely) or something completely weird could happen.

{
printf("**pp = %d\n", **pp);
}

You don''t even /try/ to iterate through the array, do you? You might want
to take a long hard look at Visual Basic or RCX.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Fri, 02 May 2008 13:44:50 +0000, Richard Heathfield wrote:

> for( i = 0; i < ARRSIZE - 1; ++i )
{
mp = malloc( sizeof( int ) );

You don''t check to see whether this fails to allocate the requested
memory...

done

> mp = &i;

...which doesn''t really matter, since you throw it away here.


well, then how can I generate a new variable every-time I enter into the
loop ?

Well done. You just filled arr_p with a bunch of pointers to i. Why bother
with the malloc call, then? You could save yourself (ARRSIZE-1) *
sizeof(int) bytes of memory leak by removing the malloc call completely.

I can but I did not because I want to understand the how pointers to
arrays to pointers behave and learn them.

You don''t even /try/ to iterate through the array, do you?

okay, here is a little fixed version. I can''t do anything about variable i
in for loop as I said, I can''t find a way to generate new variables at
every iteration through the loop.

You might want to take a long hard look at Visual Basic



http://lispmachine.wordpress.com/200...urn-for-vbnet/

or RCX.

never heard of it
--
http://lispmachine.wordpress.com/
my email ID is @ the above address



"arnuld" <No****@microsoft.comwrote in message
news:pa****************************@microsoft.com. ..

PURPOSE: see the comments.
WHAT I GOT: infinite loop

I''ve modified your code so that it behaves better:
#include <stdio.h>
#include <stdlib.h>

enum MAXSIZE { ARRSIZE = 100 };
int main( void )
{
int *arr_p[ARRSIZE];
int **pp;
int *np;
int i;

int null_int = 0;

pp = arr_p;
np = &null_int;
for( i = 0; i < ARRSIZE - 1; ++i )
{
arr_p[i] = malloc( sizeof( int ) );
*arr_p[i] = 1000+i; /* initial the ints to some recognisable
(and non-0!) value */
}
arr_p[i] = np; /* sentinel (*np contains 0) */

while( **pp) /* Step through array using pp */
{
printf("**pp = %d\n", **pp);
++pp;
}

return 0;
}

-- Bartc


这篇关于打印“指针数组”的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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