string a="12345"; char b[100000]="231"; b=&a[0];
时间: 2024-05-16 12:18:13 浏览: 9
这段代码有问题,不能将一个字符串的指针直接赋值给一个字符数组的指针。如果你想把字符串 a 的内容复制到字符数组 b 中,可以使用 strcpy 函数。
正确的代码应该是这样的:
```
#include <stdio.h>
#include <string.h>
int main() {
char a[] = "12345";
char b[100000] = "231";
strcpy(b, a);
printf("%s\n", b);
return 0;
}
```
这样就可以把字符串 a 中的内容复制到字符数组 b 中,并输出 b 的内容。
相关问题
string==string(a,b)
string==string(a,b)中的"=="是比较运算符,用于比较两个字符串是否相等。而string(a,b)是C++中的string构造函数,用于将字符数组或其他字符串类型转换为string对象。
例如,假设有两个字符数组a和b,可以使用string构造函数将它们转换为string对象,并使用"=="比较它们是否相等,如下所示:
```cpp
char a[] = "Hello";
char b[] = "World";
string str1(a);
string str2(b);
if (str1 == str2) {
cout << "str1 and str2 are equal." << endl;
} else {
cout << "str1 and str2 are not equal." << endl;
}
```
在上述代码中,str1和str2分别是由字符数组a和b构造而成的string对象。然后使用"=="比较它们是否相等。如果相等,输出"str1 and str2 are equal.",否则输出"str1 and str2 are not equal."。
string A转const char* B
可以通过以下代码将string类型的A转换为const char*类型的B:
```
const char* B = A.c_str();
```
其中,c_str()函数可以取得string对象中字符串的指针,该指针指向以空字符结尾的字符数组。因此,将c_str()函数的返回值赋给const char*类型的变量B即可完成转换。
相关推荐
![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)