解释结果int main() { char str[][20]={"Hello","Beijing"},*p=str[0]; printf("%d",strlen(p+20)); }
时间: 2024-05-17 14:17:21 浏览: 14
这段代码定义了一个二维字符数组 `str`,其中有两个字符串分别是 `"Hello"` 和 `"Beijing"`。同时定义了一个字符指针 `p`,并且让它指向数组 `str` 中的第一个字符串 `"Hello"` 的首地址。然后通过 `strlen` 函数来计算 `p+20` 所指向的字符串的长度,并把结果输出。
具体的解释如下:
1. 定义了一个二维字符数组 `str`,它有 2 行、每行有 20 个字符的空间,即 `str[0]` 和 `str[1]`。同时初始化了第一行为 `"Hello"`,第二行为 `"Beijing"`。
2. 定义了一个字符指针 `p`,并且让它指向数组 `str` 中的第一个字符串 `"Hello"` 的首地址,即 `str[0]`。
3. `strlen(p+20)` 表示让指针 `p` 指向的地址加上 20,也就是让指针 `p` 指向数组 `str` 中的第二个字符串 `"Beijing"` 的首地址,然后计算这个字符串的长度。但是这个字符串的长度是未知的,因为它没有以 `'\0'` 结尾,也就是说,它并不是一个有效的 C 语言字符串。因此,执行这段代码会导致未定义的行为。
4. 最后,代码输出 `strlen(p+20)` 的结果,但是这个结果是未定义的,可能是一个随机值,也可能是一个错误的值。
因此,这段代码的输出结果是不确定的,可能是任意值,也可能是一个错误的值。
相关问题
int main() char a; char*str=&a; strcpy(str,"hello");printf(str); return 0;
这段代码是有问题的。首先,在 main 函数的参数列表中,应该写成 `int main(void)` 或 `int main(int argc, char *argv[])`。然后,将一个字符指针 `str` 指向了一个 `char` 类型的变量 `a`,并将字符串常量 "hello" 复制到了 `str` 所指向的内存中,这个操作是非法的,因为 `str` 指向的内存只有一个字节,而 "hello" 有 6 个字节,因此会导致数组越界和内存访问错误。正确的做法是定义一个足够长的字符数组,例如 `char str[10];`,然后再将字符串复制到这个数组中。改正后的代码如下:
```
#include <stdio.h>
#include <string.h>
int main(void) {
char str[10];
strcpy(str, "hello");
printf("%s\n", str);
return 0;
}
```
输出结果为:
```
hello
```
#include <stdio.h> #include <string.h> #include <stdlib.h> char* reverse(char* s) { int i = strlen(s); int j = i; char* ptr = (char*)malloc(strlen(s) * sizeof(char)); memset(ptr, 0, strlen(s) * sizeof(char)); i--; for (; i >= 0; i--) { ptr[i] = *s; s++; } ptr[j] = '\0'; s = ptr; return s; } void* obfuscate(void* ptr) { int* p = (int*)ptr; *p = (*p ^ 0xdeadbeef) + 0x1337; return ptr; } int main() { char* str = (char*)malloc(200 * sizeof(char)); printf("Enter a string: "); scanf("%s", str); str = (char*)obfuscate((void*)reverse(obfuscate((void*)str)))); printf("Reversed string: %s\n", str); free(str); return 0; } 整理这个代码格式,使其美观容易阅读
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* reverse(char* s) {
int i = strlen(s);
int j = i;
char* ptr = (char*) malloc(strlen(s) * sizeof(char));
memset(ptr, 0, strlen(s) * sizeof(char));
i--;
for (; i >= 0; i--) {
ptr[i] = *s;
s++;
}
ptr[j] = '\0';
s = ptr;
return s;
}
void* obfuscate(void* ptr) {
int* p = (int*) ptr;
*p = (*p ^ 0xdeadbeef) + 0x1337;
return ptr;
}
int main() {
char* str = (char*) malloc(200 * sizeof(char));
printf("Enter a string: ");
scanf("%s", str);
str = (char*) obfuscate((void*) reverse(obfuscate((void*) str)));
printf("Reversed string: %s\n", str);
free(str);
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)