char指针? [英] char pointers?

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

问题描述

在试图理解这个问题时,我写了这个;


#include< stdio.h>

void f_output(char arg1 [6] ,int limit);

int main(){


f();


返回0;

}


void f(无效){

char matrix [2] [6] = {{''h'', '''','''',''',''o'',''\ 0''},

{''w'','' o'',''r'','l'',''d'',''\ 0''}};

f_output(矩阵,10);

}

void f_output(char * s,int limit){

int x,i;

for(i = 0; i< limit; i ++)

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


}


所需输出:Hello world

实际输出:


hello

ello
llo

lo

o

world

orld

rld

ld

我天真的看待这个wa的方式s假设声明


" void f_output(char arg1 [6],int limit)"


将告诉 ;该类型为6个字符数组的指针,因此
++将增加到矩阵[2]。我知道这是一个完全无用且无意义的功能,除了试图进一步理解我的

...所以请放心! :-)

解决方案

文章< 11 ***************** *****@b75g2000hsg.googlegroups .com> ;,

mdh< md ** @ comcast.netwrote:


>在试图理解这个问题时,我写了这个;


> void f_output(char arg1 [6],int limit);


> void f_output(char * s,int limit){
int x,i;
for(i = 0; i< limit; i ++)

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

}



您在函数声明

和函数定义之间存在分歧。在一个地方你使用了char arg1 [6]

而在另一个地方你使用了char * s - 数组与指针。


-

有些想法错了,只有一个非常聪明的人才能相信他们。 - George Orwell


mdh写道:


在试图理解这个问题时,我写了这个;


#include< stdio.h>

void f_output(char arg1 [6],int limit);

int main (){$>
f();



其中'是f()的原型?
< blockquote class =post_quotes>
返回0;

}


void f(无效){

char matrix [2] [6] = {{''h'',''e'',''l'','l'',''o'',''\ 0''},

{''w'',''o'',''r'','l'',''d'',''\ 0''}};

f_output(矩阵,10);

}


void f_output(char * s,int limit){



告诉你编译器应该给你的警告 - 这不是
匹配原型。


-

Ian Collins。

Ian Collins在04/11/07 17:37写道:


mdh写道:


>>在试图理解这个问题时,我写了这个;

#include< stdio.h>
void f_output(char arg1 [6 ],int limit);
[...]

void f_output(char * s,int limit){




领导你的编译器应该给你的警告 - 这不是
匹配原型。



据我所见,定义和声明

完全匹配。然而你和沃尔特罗伯森都说他们不同意......他们不同意......根据6.3.5.7p7


参数声明为数组/类型/"

应调整为合格指针/类型/

[...]


....你们中的一方或双方可以解释分歧吗?


到OP:comp.lang.c中的问题6.21和6.4

http://www.c-faq.com/

可以帮助你理解。


-
Er ********* @ sun.com


In trying to understand the issue, I wrote this;

#include <stdio.h>
void f_output(char arg1[6], int limit);
int main () {

f();

return 0;
}

void f(void) {
char matrix[2][6] ={ {''h'',''e'',''l'',''l'',''o'',''\0''},
{''w'',''o'',''r'',''l'',''d'',''\0''}};
f_output(matrix, 10);
}
void f_output(char *s, int limit) {
int x, i;
for (i=0; i < limit; i++)
printf("%s\n", s++);

}

Desired output: "Hello world"
Actual output:

hello
ello
llo
lo
o

world
orld
rld
ld
My naive way of looking at this was to assume that the declaration

"void f_output(char arg1[6], int limit)"

would "tell" the pointer that the type was an array of 6 chars, hence s
++ would increment to matrix[2]. I understand this is a totally
useless and pointless function, except in trying to further my
understanding...so please go easy!!! :-)

解决方案

In article <11**********************@b75g2000hsg.googlegroups .com>,
mdh <md**@comcast.netwrote:

>In trying to understand the issue, I wrote this;

>void f_output(char arg1[6], int limit);

>void f_output(char *s, int limit) {
int x, i;
for (i=0; i < limit; i++)
printf("%s\n", s++);

}

You have a disagreement between the function declaration
and the function definition. In one place you used char arg1[6]
and in the other place you used char *s -- array vs pointer.

--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell


mdh wrote:

In trying to understand the issue, I wrote this;

#include <stdio.h>
void f_output(char arg1[6], int limit);
int main () {

f();

Where''s the prototype for f()?

return 0;
}

void f(void) {
char matrix[2][6] ={ {''h'',''e'',''l'',''l'',''o'',''\0''},
{''w'',''o'',''r'',''l'',''d'',''\0''}};
f_output(matrix, 10);
}
void f_output(char *s, int limit) {

Head the warning your compiler should have given you here - this doesn''t
match the prototype.

--
Ian Collins.


Ian Collins wrote On 04/11/07 17:37,:

mdh wrote:

>>In trying to understand the issue, I wrote this;

#include <stdio.h>
void f_output(char arg1[6], int limit);
[...]

void f_output(char *s, int limit) {



Head the warning your compiler should have given you here - this doesn''t
match the prototype.

As far as I can see, the definition and declaration
match perfectly. Yet both you and Walter Roberson say
they disagree ... In light of 6.3.5.7p7

A declaration of a parameter as "array of /type/"
shall be adjusted to "qualified pointer to /type/"
[...]

.... could one or both of you explain the disagreement?

To the O.P.: Questions 6.21 and 6.4 in the comp.lang.c
Frequently Asked Questions (FAQ) at http://www.c-faq.com/
may help your understanding.

--
Er*********@sun.com


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

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