走过一系列的char指针 [英] walking through an array of char pointers

查看:40
本文介绍了走过一系列的char指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法让它工作:


#include< stdio.h>

#include< stdlib.h> ;

#include< string.h>


int main()

{

char * names [3];

char ** np;


names [0] =" jack";

names [1] =" jill";

names [2] =" zack";


while(** np!=''\\ \\ 0''){

printf("%s\ n",* np);

np ++;

}


返回0;

}


打印3个名字后,它会打印垃圾然后有时会出现故障。

我知道我可以使用for循环轻松地完成它,但那不是我想要的东西

for。我的印象实际上是名字:


[] - > " jack \0"

[] - > jill \0

[] - > " zack \0"

[] - > \ 0

如果是这样的话,我不能执行上述循环吗?


这样有效:


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int main()

{

char * names [3];

char ** np;


names [0] =" jack";

names [1] =" jill";

names [ 2] =" zack";


while(** np!=''\''')

printf(" \% s \ n",*(* np)++);

np ++;

while(** np!=''\ 0'')

printf(" \%s\ n",*(* np)++);

np ++;

while(** np!=''\''')

printf(" \%s\\\
",*(* np)++);


返回0;

{


但是,我需要3个循环来完成我想要做的一个。

我不想使用我知道有n $ $
str的事实数组中的数据。我想使用这样一个事实:每个字符串都是

null终止然后有一个最终的null终止符。


任何建议都会受到赞赏。


- gaga

I can''t seem to get this to work:

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

int main()
{
char *names[3];
char **np;

names[0] = "jack";
names[1] = "jill";
names[2] = "zack";

while (**np != ''\0'') {
printf("%s\n",*np);
np++;
}

return 0;
}

after printing the 3 names, it prints garbage then sometimes seg faults.
I know i can do it easily using a for loop, but that''s not what I am looking
for. I was under the impression names is actually:

[] --> "jack\0"
[] --> "jill\0"
[] --> "zack\0"
[] --> \0
If, that is so, shouldn''t I be able to perform the above loop?

This works:

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

int main()
{
char *names[3];
char **np;

names[0] = "jack";
names[1] = "jill";
names[2] = "zack";

while (**np != ''\0'')
printf("\%s\n",*(*np)++);
np++;
while (**np != ''\0'')
printf("\%s\n",*(*np)++);
np++;
while (**np != ''\0'')
printf("\%s\n",*(*np)++);

return 0;
{

But, I need 3 loops for what I''d like to do in one.
I do not want to use the fact that I know there are n
strings in the array. I want to use the fact that each string is
null terminated then there is a final null terminator.

any suggestions would be appreciated.

- gaga

推荐答案

gaga< zi ******* @ aol.com>写道:
gaga <zi*******@aol.com> wrote:
我似乎无法让这个工作:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
int main()
{char / names [3];
char ** np;
names [0] =" jack";
names [1] =" jill";
names [2] =" zack";

虽然(** np!=''\'''){


np此时尚未初始化。我想你的意思是在循环开始之前设置

设置

np =名字;


。但即便如此,在打印zack之后,没有理由将循环停止。因为之后np

会增加到__后第三个数组元素,

这可能是一些随机位模式,当解释为

as一个char指针,并不指向存储''\0'''charac-

ter的东西。所以你需要你的

''名称''数组的第四个元素,它指向一个空字符串。

printf("%s \ n", * np);
np ++;
}

返回0;
}


你需要这样的东西代替让它工作:


int main(无效)

{

char * names [] = {" jack" ,jill,zack," };

char ** np =姓名;


while(** np!=''\''')

printf("%s\ n",* np ++);

返回EXIT_SUCCESS;

}

这个有效:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
int main()
{char / names [3];
char ** np;
names [0] =" jack";
names [1] =" jill";
names [2] =" zack";

while(** np!=''\ 0'')
printf(" \%s\\\
",*(* np)++);
np ++;
while(** np!=''\ 0'')
printf(" \%s \ n",*(* np)++);
np ++;
while(** np!=''\''')
printf(" \%s\\\
",*(* np)++);
返回0;
{
I can''t seem to get this to work: #include <stdio.h>
#include <stdlib.h>
#include <string.h> int main()
{
char *names[3];
char **np; names[0] = "jack";
names[1] = "jill";
names[2] = "zack";

while (**np != ''\0'') {
np hasn''t been in initialized at this point. I guess you meant to
set
np = names;

before the start of the loop. But even then there''s no reason why
the loop should stop after printing "zack" because afterwards np
would get incremented to point _after_ the third array element,
which probably is some random bit pattern which, when interpreted
as a char pointer, doesn''t point to something where a ''\0'' charac-
ter would be stored. So you would need a fourth element of your
''names'' array that would point to an empty string.
printf("%s\n",*np);
np++;
}

return 0;
}
You would need something like this instead to get it working:

int main( void )
{
char *names[ ] = { "jack", "jill", "zack", "" };
char **np = names;

while ( **np != ''\0'' )
printf( "%s\n", *np++ );
return EXIT_SUCCESS;
}
This works: #include <stdio.h>
#include <stdlib.h>
#include <string.h> int main()
{
char *names[3];
char **np; names[0] = "jack";
names[1] = "jill";
names[2] = "zack";

while (**np != ''\0'')
printf("\%s\n",*(*np)++);
np++;
while (**np != ''\0'')
printf("\%s\n",*(*np)++);
np++;
while (**np != ''\0'')
printf("\%s\n",*(*np)++);

return 0;
{




如果这个版本适合您,那只是偶然的 - 在我的机器上它

立即崩溃。什么是'\\'%s"和*(* np)++应该这样做吗?


问候,Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de

\ __________________________ http://www.toerring.de


gaga写道:

我似乎无法让这个工作:

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

int main()
{
char *名称[3];
4 / *需要一个最终指针* / char ** np;

np =& names; / * initialize * / names [0] =" jack";
names [1] =" jill";
names [2] =" zack" ;;
names [3] ="" ;; / *结束标记* /
while(** np!=''\'''){
printf("%s \ n",* np);
np ++ ;






打印3个名字后,它会打印垃圾然后有时会发生故障。我知道我可以使用for循环轻松完成,但那不是我想要的。我的印象名称是

I can''t seem to get this to work:

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

int main()
{
char *names[3]; 4 /* need a final pointer */ char **np;
np = &names; /* initialize */ names[0] = "jack";
names[1] = "jill";
names[2] = "zack"; names[3] = ""; /* end marker */
while (**np != ''\0'') {
printf("%s\n",*np);
np++;
}

return 0;
}

after printing the 3 names, it prints garbage then sometimes seg
faults. I know i can do it easily using a for loop, but that''s
not what I am looking for. I was under the impression names is




尝试上面注释的更改/添加。更简单的是:


char * names [] = {" jack"," jill"," zack",""};

char * * np;


for(np =& names; ** np; np ++)printf("%s \ n,* np);

-

fix(vb。):1。纸张覆盖,模糊,隐藏在公众视野之外; 2.

以一种产生意想不到的后果的方式来解决,比原来的问题更糟糕。用法:Windows ME

修复了Windows 98 SE的许多缺点。 - Hutchison



Try the commented changes/additions above. Simpler is:

char *names[] = {"jack", "jill", "zack", ""};
char* *np;

for (np = &names; **np; np++) printf("%s\n, *np);
--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison


CBFalconer写道:
CBFalconer wrote:
gaga写道:
gaga wrote:
我可以'似乎让这个工作:

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

int main()
{char * names [3];
I can''t seem to get this to work:

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

int main()
{
char *names[3];



4 / *需要一个最终指针* /



4 /* need a final pointer */

char ** np;
char **np;



np =& names; / * initialize * /



np = &names; /* initialize */

names [0] =" jack";
names [1] =" jill";
names [2] = " zack";
names[0] = "jack";
names[1] = "jill";
names[2] = "zack";



names [3] ="" ;; / *结束标记* /



names[3] = ""; /* end marker */

while(** np!=''\ 0''){
printf("%s \ n,* np);
np ++;
}

返回0;


打印3个名字后,打印垃圾然后有时候seg
错误。我知道我可以使用for循环轻松完成,但那不是我想要的。我的印象名称是
while (**np != ''\0'') {
printf("%s\n",*np);
np++;
}

return 0;
}

after printing the 3 names, it prints garbage then sometimes seg
faults. I know i can do it easily using a for loop, but that''s
not what I am looking for. I was under the impression names is



尝试上面注释的更改/添加。更简单的是:

char * names [] = {" jack"," jill"," zack",""};
char * * np;

for(np =& names; ** np; np ++)printf("%s \ n,* np);


Try the commented changes/additions above. Simpler is:

char *names[] = {"jack", "jill", "zack", ""};
char* *np;

for (np = &names; **np; np++) printf("%s\n, *np);




char * names [] = {" jack"," jill"," zack",NULL};

char ** np = names;

while( * np)printf("%s\ n",* np ++);


请注意,names是指向char的指针数组。 ''names''衰减到指向数组第一个元素的
指针。指向char的指针。所以

赋值是''np = names;'',而不是''np =& names;''。


-

Joe Wright mailto:jo ******** @ comcast.net

所有内容都应尽可能简单,但并不简单。

---阿尔伯特爱因斯坦---



char *names[] = {"jack", "jill", "zack", NULL };
char **np = names;
while (*np) printf("%s\n", *np++);

Note that names is an array of pointers to char. ''names'' decays to a
pointer to the array''s first element. A pointer to char. So the
assignment is ''np = names;'', not ''np = &names;''.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---


这篇关于走过一系列的char指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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