`
carolaif
  • 浏览: 70311 次
  • 性别: Icon_minigender_2
  • 来自: 大连
最近访客 更多访客>>
社区版块
存档分类
最新评论

C程序 关于字符串的问题

阅读更多
#include<stdio.h>

void strcpy(char *s,char *t);

int main(){

		char *s = new char[100];
		s = "hello";
		//char s[100] = "hello";

		char *d = "world";
		strcpy(s,d);
		printf("%s\n",s);
		return 0;
}


void strcpy(char *s,char *t)
{
	while(*s++ = *t++)
		;
}

 

//A
char *s = new char[100];
s = "hello";

 

//B
char s[100] = "hello";

 

A 与 B的区别:

A中 s= new char[100];是将s指向这新申请的100个字符空间的首地址,而s="hello",已经修改了s的值,将s指向了字符串常量"hello"的首地址。
B 中s是一个长度为100的字符数组的首地址。

 

因此用A种这种方法时,运行是会提示修改的不能written的值。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics