C新手问题...... [英] C novice question...

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

问题描述

#include< stdio.h>


int main()

{

int i;

char * p =" sarang";

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

printf("%c", *(p + i));

printf(" \ n");

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

printf("%c",p [i]);

printf(p);

char * k = p;

k [1] =''你';

printf("%s",p);

}


i用gcc编译它,它显示错误信息,


i认为最后3行是问题..任何人都可以帮助我吗?

#include <stdio.h>

int main()
{
int i;
char *p="sarang";
for(i=0;i<4;i++)
printf("%c",*(p+i));
printf("\n");
for(i=0;i<4;i++)
printf("%c",p[i]);
printf(p);
char *k=p;
k[1]=''u'';
printf("%s",p);
}

i compiled it with gcc, and it shows error message,

i think the last 3 lines are problem.. Can anyone help me ?

推荐答案

下次,尝试包含错误消息,以便我们可以使用它来确定问题。


herrcho写道:
Next time, try to include the error message so we can use it to
determine the problem.

herrcho wrote:
#include< stdio.h>

int main()
{
int i;
char * p = QUOT;萨朗英寸;
char * k;

for(i = 0; i< 4; i ++)
printf("%c",*(p + i));
printf(" \ n");
for(i = 0; i< 4; i ++)
printf("%c",p [i]);
printf(p);
/ * char * k = p; * /
k = p;

k [1] =''u'';
printf( %s,p);
}
我用gcc编译它,它显示错误信息,
我认为最后3行是问题..任何人都可以帮助我吗?
#include <stdio.h>

int main()
{
int i;
char *p="sarang"; char *k;
for(i=0;i<4;i++)
printf("%c",*(p+i));
printf("\n");
for(i=0;i<4;i++)
printf("%c",p[i]);
printf(p);
/*char *k=p;*/ k=p;
k[1]=''u'';
printf("%s",p);
}

i compiled it with gcc, and it shows error message,

i think the last 3 lines are problem.. Can anyone help me ?




这将编译警告,但因为

k [1]而无法正常运行='''''行。

解决这个变化

char * p =" sarang";



char p [] =" sarang";


p []版本导致sarang的内存在堆栈上分配。

您的版本硬编码sarang在你的程序的.rodata部分,

这是不可写的。


图gcc给你的最后一个警告,它提供了信息

够了。


Mark



This will compile with warnings, but won''t run properly because of the
k[1]=''u'' line.
To solve this change
char *p="sarang";
in
char p[]="sarang";

The p[] version causes memory for "sarang" to be allocated on the stack.
Your version hard codes "sarang" in the .rodata section of your program,
which is not writable.

Figure the last warning gcc gives you out yourself, it is informative
enough.

Mark


herrcho< he ******* **@kornet.net>写道:
herrcho <he*********@kornet.net> wrote:
#include< stdio.h>
int main()
对于(i = 0; i< 4; i ++)
printf("%c",*(p + i));
printf(" \ n");
for(i = 0; i< 4; i ++)
printf("%c",p [i]);
printf(p);
char * k = p;
k [1] =''你' ;
printf("%s",p);
}


你承诺程序会返回一个int,但是你

不要保留它......最好添加一个返回语句。

i用gcc编译它,它显示错误信息,
i认为最后3行是问题..任何人都可以帮助我吗?
#include <stdio.h> int main()
{
int i;
char *p="sarang";
for(i=0;i<4;i++)
printf("%c",*(p+i));
printf("\n");
for(i=0;i<4;i++)
printf("%c",p[i]);
printf(p);
char *k=p;
k[1]=''u'';
printf("%s",p);
}
You promised that the program would return an int, but you
don''t keep it... Better add a return statement.
i compiled it with gcc, and it shows error message, i think the last 3 lines are problem.. Can anyone help me ?




由于这条线


char * p =" sarang";


p指的是什么叫做文字字符串,即一个字符串

驻留在内存中你不允许修改的地方(它是

实际上可以放在ROM中,或者编译器可以使用相同的

内存,如果你写的话


char * p1 =" sarang";

char * p2 =" arang";


p2只是p1 + 1)。现在你将k指向相同的

内存位置,然后尝试改变那里存储的内容。并且

这是问题,因为你不允许这样做。


解决这个问题的最简单方法是使pa真正的数组,

不只是一个指针:


char p [] =" sarang";


p现在一个内存属于你的程序的数组和

初始化为sarang。既然你拥有记忆,你也可以毫无问题地改变它。 p(没有任何添加)可以

仍然被视为指向数组的第一个元素的指针

在大多数情况下,所以程序的其余部分仍然可以工作

修改。

问候,Jens

-

_ _____ _____

| || _ _ || _ _ | Je *********** @ physik.fu-berlin.de

_ | | | | | |

| | _ | | | | | | http://www.physik.fu-berlin.de/~toerring

\ ___ / ens | _ | homs | _ | oerring



Due to the line

char *p="sarang";

p is pointing to what''s called a literal string, i.e. a string that
resides somewhere in memory which you aren''t allowed to modify (it
actually may get placed in ROM or the compiler could use the same
memory if e.g. you would write

char *p1="sarang";
char *p2="arang";

with p2 being just p1 + 1). Now you make k point to the same
memory location and then try to change what''s stored there. And
that''s the problem because you''re not allowed to do this.

The simplest way around this problem is to make p a real array,
not just a pointer:

char p[ ] = "sarang";

p is now an array with memory that belongs to your program and
initialized to "sarang". Since you own the memory you also can
change it without any problems. p (without any additions) can
still be treated like a pointer to the first element of the array
in most cases, so the rest of the program will still work without
modifications.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring


2003年9月15日星期一21:07:22 +0900(KST),herrcho < he ********* @ kornet.net>

写道:
On Mon, 15 Sep 2003 21:07:22 +0900 (KST), "herrcho" <he*********@kornet.net>
wrote:
#include< stdio.h>

int main()
{i /;
char * p =" sarang" ;;
for(i = 0; i< 4; i ++)
printf("%c",*(p + i));
printf(" \ n");
for(i = 0; i< 4; i ++)
printf("%c",p [i]);
printf(p);
char * k = p;


除非您运行的是C99编译器,否则上述声明是非法的。

声明不能与语句混合使用;它们必须在一个块中的语句

之前。

将k(char * k = p;)的声明移到紧跟在
p的声明(char * p =" sarang";)

k [1] =''u'';
printf("%s", p);
}
我用gcc编译它,它显示错误信息,

我认为最后3行是问题..任何人都可以帮助我?
#include <stdio.h>

int main()
{
int i;
char *p="sarang";
for(i=0;i<4;i++)
printf("%c",*(p+i));
printf("\n");
for(i=0;i<4;i++)
printf("%c",p[i]);
printf(p);
char *k=p;
Unless you are running a C99 compiler, the above statement is illegal.
Declarations cannot be mixed with statements; they must preceed the statements
in a block.

Move the declaration of k (char *k=p;) to the line immediately following the
declaration of p (char *p="sarang";)
k[1]=''u'';
printf("%s",p);
}

i compiled it with gcc, and it shows error message,

i think the last 3 lines are problem.. Can anyone help me ?




-

Lew Pitcher

IT顾问,企业技术解决方案

多伦多道明银行金融集团


(表达的意见是我自己的,而不是我的雇主')



--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers'')


这篇关于C新手问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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