#include <stdio.h>
#define N 10
char str2[N]={"Hello"};
int main(){
printf("sizeof(str2): %d bytes\n", sizeof(str2));
printf("sizeof(&str2): %d bytes\n", sizeof(&str2));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
sizeof(str2): 10 bytes
sizeof(&str2): 4 bytes
Run Code Online (Sandbox Code Playgroud)
我知道str2单独是数组中第一个元素的地址str2.并且当str2它的参数何时sizeof返回整个数组str2的大小.
另外,&str2也是arr中第一个元素的地址,str2但是来自不同的类型(char (*)[N]==指向数组的指针).但是&str2当它是一个论证时,它是如何表现的sizeof?
#include<stdio.h>
void main()
{
char *p="nyks";
p[2]='n';
printf("%s",p);
}
Run Code Online (Sandbox Code Playgroud)
这会导致SEGMENTATION FAULT崩溃.有人可以解释原因吗?
我试着strcpy自己做.它应该工作,我甚至复制和粘贴(几乎确切的代码)来自这里的某些人 strcpy.两者都给我一个"分段错误".
char* strcpy(char * destination, const char * source)
{
while( (*destination++ = *source++) != '\0' )
;
return destination;
}
Run Code Online (Sandbox Code Playgroud)
这段代码出了什么问题?
char* a = "hello";
cout << strcpy(a, "Haha") << endl;
Run Code Online (Sandbox Code Playgroud)