char *p = ‘a’
时间: 2023-12-22 19:06:27 浏览: 24
This statement is incorrect as it is using single quotes to assign a character to a pointer variable. The correct syntax for assigning a character to a pointer variable is:
```
char *p = "a";
```
Note the use of double quotes to denote a string literal. This assigns a pointer variable `p` to point to the memory location where the character `'a'` is stored.
相关问题
void fun(char *a) { // 去掉前导的 * while (*a == '*') { a++; } // 去掉尾部的 * char *end = a + strlen(a) - 1; while (*end == '*' && end > a) { end--; } *(end + 1) = '\0'; // 去掉中间的 * char *p = a; while (*p != '\0') { if (*p == '*') { if (*(p + 1) == '*' || *(p - 1) == '*') { // 保留连续的 * p++; } else { // 删除单个 * char *q = p; while (*q != '\0') { *q = *(q + 1); q++; } } } else { p++; } }}帮我运行这个程序
好的,我可以帮你运行这个程序。下面是我写的测试代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
void fun(char *a) {
// 去掉前导的 *
while (*a == '*') {
a++;
}
// 去掉尾部的 *
char *end = a + strlen(a) - 1;
while (*end == '*' && end > a) {
end--;
}
*(end + 1) = '\0';
// 去掉中间的 *
char *p = a;
while (*p != '\0') {
if (*p == '*') {
if (*(p + 1) == '*' || *(p - 1) == '*') {
// 保留连续的 *
p++;
} else {
// 删除单个 *
char *q = p;
while (*q != '\0') {
*q = *(q + 1);
q++;
}
}
} else {
p++;
}
}
}
int main() {
char a[100];
cout << "请输入带*的字符串(不超过99个字符):" << endl;
cin >> a;
fun(a);
cout << "处理后的字符串为:" << endl;
cout << a << endl;
return 0;
}
```
你可以把上面的代码复制到你的编译器中运行,输入带*的字符串,程序会自动调用函数 fun 处理字符串,并输出处理后的结果。注意,输入的字符串长度不能超过 99 个字符。
5.在32位机器上执行 char a[] = “hello”; char *p = a;,sizeof(a) = 多少字节,sizeof(p) = 多少字节。
在32位机器上执行 char a[] = "hello"; char *p = a; 后,sizeof(a) 的值为 6,sizeof(p) 的值为 4。
原因如下:
1. sizeof(a) 表示数组 a 的大小,包括数组中的所有元素。在这个例子中,a 是一个字符数组,它有 6 个元素(包括字符串末尾的空字符 '\0'),因此 sizeof(a) 的值为 6。
2. sizeof(p) 表示指针 p 的大小,也就是指针所占用的空间大小。在 32 位机器上,指针的大小为 4 个字节。因此,sizeof(p) 的值为 4。
需要注意的是,虽然 a 和 p 都指向字符串 "hello" 的首地址,但它们的类型不同。a 是一个字符数组,而 p 是一个字符指针。因此,a 和 p 所占用的空间大小是不同的。