程序重复,我怀疑指针麻烦。 [英] Program repeats itself, pointer trouble I suspect.

查看:64
本文介绍了程序重复,我怀疑指针麻烦。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我用一系列指针编写了程序,我怀疑它们是在Do ... While循环中指向对方的
。 br />
有些东西搞乱了增量变量字。一个程序

我正在谈论的内容。


#include< stdio.h>

#include < string.h>


int main(void)

{

char string [50] = {" Have美好的一天人们};

char * line_ptr;

char * list [20] = {''\ 0''}; //初始化指针数组

NULL。

int word = 0;


line_ptr = strtok(string," ;");


do

{

list [word] = line_ptr;

word ++;

line_ptr = strtok(NULL,"");

} while(line_ptr!= NULL);


返回0;

}

这有点奇怪,因为程序在我运行时会重复。我

省略了输出部分,因为我知道它工作正常。在

调试器中,它可以正常工作。有内存泄漏吗?使用后,是否需要将

指针分配给NULL?在程序终止之前,我应该从指针数组中释放内存吗?完成它们后,最好的

方式处理指针是什么?


感谢您的帮助

Neil

解决方案

2月15日下午1:50,Neil < neilwrit ... @ hotmail.comwrote:


#include< stdio.h>

#include< string.h> ;


int main(无效)

{

char string [50] = {"祝你有个美好的一天 };

char * line_ptr;

char * list [20] = {''\ 0''};



{0}做同样的事情并且更有意义。


int word = 0 ;


line_ptr = strtok(string,"");


do

{

list [word] = line_ptr;

word ++;

line_ptr = strtok(NULL,"");

} while(line_ptr!= NULL);


返回0;}


这有点奇怪,导致程序在重复时重复我跑了



这个程序没问题。 重复本身是什么意思?

这个程序不产生输出。


在调试器中它工作正常。有内存泄漏吗?





>使用它们后是否需要将指针指定为NULL?





我应该在
之前从指针数组中释放内存吗?
程序终止了吗?





完成这些指针后处理指针的最佳方法是什么?



不采取任何特别行动。


我遗漏了输出部分,因为我知道它工作正常。



显然不是......


2007年2月14日16:50:11 - 0800,Neil < ne ********* @ hotmail.comwrote:


>大家好!

我写了一个程序一个指针数组,我怀疑它们在Do ... While循环中相互指向。
有些东西与增量变量字搞混了。一个程序
我正在谈论的内容。

#include< stdio.h>
#include< string.h>

int main(void)
{char string [50] = {"祝你有个美好的一天'};
char * line_ptr;
char * list [20 ] = {''\ 0''}; //将指针数组初始化为
NULL。



这就是为什么你不应该在usenet中使用//样式注释。


如果你想分配20个中的每一个数组中的指针为NULL值,

使用NULL。虽然0和''\ 0''都可以工作,但它们在视觉上是误导的。
误导。有人可能会想到指针指向含有''\0''的字符的


> int word = 0;

line_ptr = strtok(字符串,"");



{

list [word] = line_ptr;

word ++;

line_ptr = strtok(NULL,"");

} while(line_ptr!= NULL);

返回0;
}
这有点奇怪,因为程序在我运行时会重复。我把输出部分省略了,因为我知道它工作正常。在



定义重复。


档案中充满了错误在该部分的消息
海报省略了
,因为它的工作原理。发布一个可编辑的示例

,演示相关行为并让我们帮您找到问题。


>调试器它工作正常。有内存泄漏吗?你需要分配



没有动态分配你就不会有内存泄漏。


>指针使用它们后为NULL?我应该从
释放内存



除非你测试指针为NULL,否则你永远不需要将它重置为

NULL。 />


>程序终止前的指针数组?什么是最好的



任何释放你没有分配的内存的尝试都会调用

未定义的行为。


>完成它们之后处理指针的方式?



当你完成任务时你处理任何其他类型的对象的方式相同

。在大多数情况下,只需在任何

后续代码中使用它就足够了。

删除电子邮件的del


< blockquote> 2月14日下午4:50,Neil < neilwrit ... @ hotmail.comwrote:


line_ptr = strtok(string,"");



除非你确定你总是会有一个令牌,你应该

在这里检查line_ptr为NULL,也好。


这是一个紧凑的形式,如果你不介意在

表达式中的作业:

if((p = strtok(string,""))!= NULL){

do {

printf(" Token:% s \ n,p);

} while((p = strtok(NULL,""))!= NULL);

}


do

{

list [word] = line_ptr;

word ++;

line_ptr = strtok(NULL,"");

} while(line_ptr!= NULL);



这是我运行时的输出。


''有''

''a''

''很好''

''天''

''伙计''


对我来说很好看。什么是你的产出?


-Beej


Hello all!
I wrote program with a array of pointers, and I suspect they are
pointing at each other in the Do ...While loop.
Something is messed up with the increment variable word. A program
clip of what I''m talking about.

#include <stdio.h>
#include <string.h>

int main(void)
{
char string[50] = {"Have a nice day folks"};
char *line_ptr;
char *list[20] = { ''\0'' }; //Initialize the array of pointers to
NULL.
int word = 0;

line_ptr = strtok(string, " ");

do
{
list[word] = line_ptr;
word++;
line_ptr = strtok(NULL," ");
} while (line_ptr != NULL);

return 0;
}
It''s kinda weird, cause the program repeats itself when I run it. I
left out the output section, cause I know it works fine. In the
debugger it works OK. Is there a memory leak? Do you need to assign
pointers to NULL after you use them? Should I free up the memory from
the array of pointers before the program terminates? What is the best
way handle pointers after your done with them?

Thanks for all your help
Neil

解决方案

On Feb 15, 1:50 pm, "Neil" <neilwrit...@hotmail.comwrote:

#include <stdio.h>
#include <string.h>

int main(void)
{
char string[50] = {"Have a nice day folks"};
char *line_ptr;
char *list[20] = { ''\0'' };

{ 0 } does the same thing and makes a bit more sense.

int word = 0;

line_ptr = strtok(string, " ");

do
{
list[word] = line_ptr;
word++;
line_ptr = strtok(NULL," ");
} while (line_ptr != NULL);

return 0;}

It''s kinda weird, cause the program repeats itself when I run it.

This program is fine. What do you mean by ''repeats itself'' ?
This program generates no output.

In the debugger it works OK. Is there a memory leak?

No

>Do you need to assign pointers to NULL after you use them?

No

Should I free up the memory from the array of pointers before the
program terminates?

No

What is the best way handle pointers after your done with them?

Take no special action.

I left out the output section, cause I know it works fine.

Apparently not...


On 14 Feb 2007 16:50:11 -0800, "Neil" <ne*********@hotmail.comwrote:

>Hello all!
I wrote program with a array of pointers, and I suspect they are
pointing at each other in the Do ...While loop.
Something is messed up with the increment variable word. A program
clip of what I''m talking about.

#include <stdio.h>
#include <string.h>

int main(void)
{
char string[50] = {"Have a nice day folks"};
char *line_ptr;
char *list[20] = { ''\0'' }; //Initialize the array of pointers to
NULL.

This is why you should not use // style comments in usenet.

If you want assign each of the 20 pointer in the array the NULL value,
use NULL. While 0 and ''\0'' will both work, they are visually
misleading. Someone might be tempted to think that the pointers point
to a char containing ''\0''.

>int word = 0;

line_ptr = strtok(string, " ");

do
{
list[word] = line_ptr;
word++;
line_ptr = strtok(NULL," ");
} while (line_ptr != NULL);

return 0;
}
It''s kinda weird, cause the program repeats itself when I run it. I
left out the output section, cause I know it works fine. In the

Define repeat.

The archives are full of messages where the error was in the section
omitted by the poster because "it works." Post a compilable example
that demonstrates the behavior in question and let us help you find
the problem.

>debugger it works OK. Is there a memory leak? Do you need to assign

You cannot have a memory leak without dynamic allocation.

>pointers to NULL after you use them? Should I free up the memory from

Unless you test a pointer for NULL, you never need to reset it to
NULL.

>the array of pointers before the program terminates? What is the best

Any attempt to free memory that you did not allocate will invoke
undefined behavior.

>way handle pointers after your done with them?

The same way you handle an object of any other type when you are done
with it. In most cases, it is sufficient to simply not use it in any
subsequent code.
Remove del for email


On Feb 14, 4:50 pm, "Neil" <neilwrit...@hotmail.comwrote:

line_ptr = strtok(string, " ");

Unless you''re sure you''re always going to have a token, you should
probably check line_ptr for NULL here, too.

Here''s a compact form of that, if you don''t mind assignments in your
expressions:

if ((p = strtok(string, " ")) != NULL) {
do {
printf("Token: %s\n", p);
} while ((p = strtok(NULL, " ")) != NULL);
}

do
{
list[word] = line_ptr;
word++;
line_ptr = strtok(NULL," ");
} while (line_ptr != NULL);

Here is my output when I ran it.

''Have''
''a''
''nice''
''day''
''folks''

Looks fine to me. What''s your output?

-Beej


这篇关于程序重复,我怀疑指针麻烦。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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