char指针数组 [英] array of char pointers

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

问题描述

我认为我终于明白了。唉。

给出:

char * s [] = {" Jan"," Feb"," Mar"," April"};


是否可以将char * p指向s?


* p = s ...不会这样做,正如我所料。

* p = s [0] ...我相信它会指向s中的第一个元素。但是

这不是我想要的。我想要的是一个指针,当

递增时,它将产生Jan,Feb,Mar,April和4月。等等


我相信我想要的是指针相当于:


printf("%s",s [i]) ,(其中i = 0-> 3)

谢谢。

And I thought I understood it, finally. Alas.
given:
char *s[]={"Jan","Feb","Mar","April"};

is it possible to have char *p point at s?

*p = s...does not do it, as I expect.
*p=s[0]...I believe sets it to point to the first element in s. But
this is not what I want. What I want is a pointer which, when
incremented will produce "Jan","Feb","Mar","April" etc.

I believe what I want is a pointer equivalent of:

printf("%s", s[i]), (where i = 0->3)
Thanks.

推荐答案

mdh写道:
mdh wrote:

最后,我想我明白了。唉。


给出:

char * s [] = {" Jan"," Feb"," Mar"" April" };


是否可以将char * p指向s?
And I thought I understood it, finally. Alas.
given:
char *s[]={"Jan","Feb","Mar","April"};

is it possible to have char *p point at s?



概念上可能更容易想到s作为char **。


所以给定char * p;


p = s [0];

p现在指向字符串文字Jan。

It might be conceptually easier to think of s as a char**.

so given char* p;

p = s[0];

p now points to the string literal "Jan".


* p = s ...没有按照我的预期这样做。 />
* p = s [0] ...我相信它设置为指向s中的第一个元素。但是

这不是我想要的。我想要的是一个指针,当

递增时,它将产生Jan,Feb,Mar,April和4月。等等
*p = s...does not do it, as I expect.
*p=s[0]...I believe sets it to point to the first element in s. But
this is not what I want. What I want is a pointer which, when
incremented will produce "Jan","Feb","Mar","April" etc.



然后你希望p成为另一个char **。

Then you want p to be another char**.


我相信我想要的是一个指针相当于:


printf("%s",s [i]),(其中i = 0-> 3)
I believe what I want is a pointer equivalent of:

printf("%s", s[i]), (where i = 0->3)



char ** p = s;


for(unsigned n = 0; n< 4; ++ n)

{

printf("%s",* p);

++ p;

}

-

Ian Collins。

char** p = s;

for( unsigned n = 0; n < 4; ++n )
{
printf("%s", *p );
++p;
}

--
Ian Collins.


4月17日下午6:59,Ian Collins< ian-n。 .. @ hotmail.comwrote:
On Apr 17, 6:59 pm, Ian Collins <ian-n...@hotmail.comwrote:

mdh写道:
mdh wrote:


给出:

char * s [] = {" Jan"," Feb"," Mar"," April"};
given:
char *s[]={"Jan","Feb","Mar","April"};


>

从概念上讲,将s视为char **可能更容易。

那么你希望p成为另一个char **。
>
It might be conceptually easier to think of s as a char**.
Then you want p to be another char**.


char ** p = s;


for(unsigned n = 0; n< 4 ; ++ n)

{

printf("%s",* p);

++ p;
char** p = s;

for( unsigned n = 0; n < 4; ++n )
{
printf("%s", *p );
++p;



Ian ...得到JanJanJan


如果我用* p ++取代* p得到了预期。

这有意义吗?


Ian...got "JanJanJan"

if I replaced *p with *p++ got as expected.
Does this make sense?



mdh< m ... @ comcast.netwrote:
mdh <m...@comcast.netwrote:

我认为我终于明白了。唉。


给出:

char * s [] = {" Jan"," Feb"," Mar"," April" };


是否可以将char * p指向s?
And I thought I understood it, finally. Alas.

given:
char *s[]={"Jan","Feb","Mar","April"};

is it possible to have char *p point at s?



No.

No.


* p = s ...没有按照我的预期这样做。

* p = s [0] ...我相信它会指向s中的第一个元素。但是

这不是我想要的。我想要的是一个指针,当

递增时,它将产生Jan,Feb,Mar,April和4月。等等
*p = s...does not do it, as I expect.
*p=s[0]...I believe sets it to point to the first element in s. But
this is not what I want. What I want is a pointer which, when
incremented will produce "Jan","Feb","Mar","April" etc.



要指向一个元素,你需要一个指向元素类型的指针。


指向一个元素对于一个元素数组,你需要一个

指向元素类型的指针。


所以,要指向一个X数组的元素,你需要一个指针

到X.


因此,要指向一个char *数组的元素,你需要

a指针到char *。换句话说,你需要一个char **。

To point to an element, you need a pointer to the element type.

To point to an element of an array of elements, you need a
pointer to the element type.

So, to point to an element of an array of X, you need a pointer
to X.

Thus, to point to an element of an array of char *, you need
a pointer to char *. In other words, you need a char **.


我相信我想要的是指针相当于:


printf("%s",s [i]),(其中i = 0-> 3)
I believe what I want is a pointer equivalent of:

printf("%s", s[i]), (where i = 0->3)



#include< stdio.h>


#define countof(X)((size_t)(sizeof(X)/ sizeof *(X)))


int main(void)

{

const char * s [] = {" Jan",Feb,Mar,April};

const char ** p;

size_t i;


puts(" Loop 1:");

for(i = 0; i< countof(s); i ++)

puts(s [i]);


puts(" \ nLoop 2:");

for(p = s,i = 0; i< countof(s); i ++)

puts(p [i ]);


put(" \\\
Loop 3:");

for(p = s; p<& s [countof] (s)]; p ++)

put(* p);


返回0;

}

#include <stdio.h>

#define countof(X) ( (size_t) ( sizeof(X)/sizeof*(X) ) )

int main(void)
{
const char *s[]={"Jan","Feb","Mar","April"};
const char **p;
size_t i;

puts("Loop 1:");
for (i = 0; i < countof(s); i++)
puts(s[i]);

puts("\nLoop 2:");
for (p = s, i = 0; i < countof(s); i++)
puts(p[i]);

puts("\nLoop 3:");
for (p = s; p < &s[countof(s)]; p++)
puts(*p);

return 0;
}


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

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