C语言中char*和char[]用法区别分析&微软编译器的坑
来源:互联网 发布:java做自动化测试 编辑:程序博客网 时间:2024/06/06 08:48
一般来说,很多人会觉得这两个定义效果一样,其实差别很大。以下是个人的一些看法,有不正确的地方望指正。
本质上来说,char *s定义了一个char型的指针,它只知道所指向的内存单元,并不知道这个内存单元有多大,所以:
当char *s = "hello";后,不能使用s[0]='a';语句进行赋值。这是将提示内存不能为"written"。
当用char s[]="hello";后,完全可以使用s[0]='a';进行赋值,这是常规的数组操作。
若定义:
也可以使用p[0] = 'a';因为这是p ==s,都是指向数组的指针。
下面看另外一种定义:
这句话其实相当于:
定义的也是一个指向数组的指针,便可进行数组的下标操作
例子
结果是:
$ > ./main
size of buf1: 4
size of buf2: 15
------------------------------------------------------------------------------------------------------------------------------------------------------------------
在编写这道题目时,用了VS2010:
使用C/C++编写函数,实现字符串反转,要求不使用任何系统函数,且时间复杂度最小。
当运行下列代码时,报出了错误:0xC0000005: Access violation writing location 0x00125744。
char* reverseString(char* s) { if(s == NULL){return "";}int length = 0;for(length;;length++){if(s[length] == '\0'){break;}}for(int i = 0;i<length/2;i++){char temp;temp = s[i];s[i] = s[length-1-i];s[length-1-i] =temp;}return s;}上网查了查,发现了上面关于char*和char[]的区别。但是这段代码在leetcode上却通过了。
后来把函数改写成了:
char* reverseString(char* s) { if(s == NULL){return "";}int length = 0;<span style="white-space:pre"></span>for(length;;length++){if(s[length] == '\0'){break;}}<span style="white-space:pre"></span>char *newString = (char*)malloc(length*sizeof(char));for(int i = 0;i<length;i++){newString[i] = s[length-1-i];}newString[length] = '\0';return newString;}
但似乎用了系统函数malloc。
而直接定义一个char数组,char newString[length]又会报错:expected constant expression。这是因为:
The sizes of array variables in C must be known at compile time. If you know it only at run time you will have to malloc
some memory yourself instead.
好纠结啊,大家有什么方法吗?
When you write char myArray[6]="Hello";
you are allocating 6 char
s on the stack (including a null-terminator).
Yes you can change individual elements; e.g. myArray[4] = '\0' will transform your string to "Hell" (as far as the C library string functions are concerned), but you can't redefine the array itself as that would ruin the stack.
Note that [const] char* myArray = "Hello";
is an entirely different beast: that is read-only memory and any changes to that string is undefined behaviour.
Array is a non modifiable lvalue. So you cannot modify it.If you wish to modify the contents of the array, use strcpy
.
- C语言中char*和char[]用法区别分析&微软编译器的坑
- C语言中char*和char[]用法区别分析
- C语言中char * 和 char []的区别
- C语言中char * 和 char []的区别
- C语言中char * 和 char []的区别
- C语言中char * 和 char []的区别
- C语言中char * 和 char []的区别
- c语言中 char* 和 unsigned char* 的区别浅析
- c 中char * 和 char []的区别
- JAVA中的char与C语言中char的区别
- C语言中unsigned char* 与char*的区别?
- C语言char s[] 和 char *s的区别
- c中有关char *p 和char a[6] ,char a[] char a[0] 的区别
- 深入分析C++中char * 和char []的区别
- 深入分析C++中char * 和char []的区别
- 深入分析C++中char * 和char []的区别 [转]
- [转]深入分析C++中char * 和char []的区别
- 深入分析C++中char * 和char []的区别
- 递归和非递归快排
- Xcode快捷键
- Java语法----Java中equals和==的区别(转载)
- linux中vi命令
- Ubuntu Make新版上线:支持安装Swift编程语言
- C语言中char*和char[]用法区别分析&微软编译器的坑
- 通过不同形式获取Bitmap,以及创建Bitmap
- 【数据结构与算法】二叉树递归与非递归遍历
- Scala集合操作
- android自定义popupWindow
- java客户端动态调用webservice服务
- 怎么理解state thread库?
- 机器学习算法笔记
- IP头、TCP头、UDP头详解以及定义