怎么输入char*arr[];的值
时间: 2023-05-19 19:01:54 浏览: 54
您可以使用以下代码来输入char*arr[]的值:
```
for(int i=0; i<sizeof(arr)/sizeof(arr[0]); i++){
scanf("%s", arr[i]);
}
```
这将允许您逐个输入char*arr[]数组中的每个元素的值。
相关问题
怎么输出输入char*arr[];的值
### 回答1:
您可以使用循环来输出char*arr[]中的值,例如:
for(int i=0; i<sizeof(arr)/sizeof(char*); i++){
printf("%s\n", arr[i]);
}
这将逐个输出arr[]中的每个字符串。
### 回答2:
要输出输入的`char* arr[]`的值,我们可以使用循环遍历数组,并逐个输出其元素。具体的代码如下:
```c++
#include <iostream>
using namespace std;
int main() {
// 定义一个字符串数组
char* arr[] = { "Hello", "World", "!" };
// 计算数组的长度
int length = sizeof(arr) / sizeof(arr[0]);
// 遍历数组并逐个输出元素
for (int i = 0; i < length; i++) {
cout << arr[i] << " ";
}
return 0;
}
```
输出结果为:
```shell
Hello World !
```
以上代码首先定义了一个字符串数组`arr`,然后通过计算数组长度的方法求得数组的长度。接着,通过循环遍历数组,并使用`cout`语句逐个输出数组的元素。最后返回0表示程序成功执行完毕。
希望以上回答能够解决你的问题。
### 回答3:
要输出输入的char* arr[]的值,可以使用循环遍历每个元素,并通过printf函数打印出来。
下面是一个简单的示例代码:
```c
#include <stdio.h>
int main() {
char* arr[] = {"Hello", "World", "12345"};
int size = sizeof(arr) / sizeof(arr[0]); // 获取arr数组的长度
for (int i = 0; i < size; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
```
在这个示例中,我们定义了一个char*类型的数组arr,并初始化了三个字符串元素。然后,通过计算数组的长度,使用for循环遍历数组,将每个元素都通过printf函数输出到屏幕上。每个元素都使用%s作为格式化字符,因为它是char*类型的字符串。
以上代码的输出结果如下:
Hello
World
12345
这样就可以输出输入的char* arr[]的值了。
void library::modify(library &L, char* arr, char* arr1, char* arr2, char* arr3, char* arr4, char* arr5, char* arr6, char* arr7) { char a[100], b[100]; strcpy(a, arr); library *p; int count = 0, i = 0, j = 0; while (*arr != '\0') { count++; arr++; } p = L.next; while (p) { strcpy(b, p->data.name); i = 0, j = 0; while (a[i] == b[j] && a[i] != '\0' && b[j] != '\0') { i++; j++; } if (i == count) { strcpy(p->data.name, arr1); strcpy(p->data.num, arr2); strcpy(p->data.tel, arr3); strcpy(p->data.cla, arr4); strcpy(p->data.name1, arr5); strcpy(p->data.time, arr6); strcpy(p->data.adress, arr7); cout << "修改成功" << endl; ofstream ofs; ofs.open("text.txt", ios::out); library *S; S = L.next; while (S) { ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " " << S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl; S = S->next; } ofs.close(); return; } p = p->next; } cout<<"查无此书,修改失败"<<endl; } 告诉我该段代码每一句的详细含义
这段代码定义了一个类 `library`,其中包含一个修改图书信息的成员函数 `modify`。以下是每一句的详细含义:
1. `void library::modify(library &L, char* arr, char* arr1, char* arr2, char* arr3, char* arr4, char* arr5, char* arr6, char* arr7) {`:定义了修改函数,函数名为 `modify`,参数包括一个 `library` 类型的引用 `L` 和 8 个 `char*` 类型的指针变量 `arr`、`arr1`、`arr2`、`arr3`、`arr4`、`arr5`、`arr6`、`arr7`。
2. `char a[100], b[100];`:定义两个字符数组 `a` 和 `b`,用于存储字符串。
3. `strcpy(a, arr);`:将 `arr` 指向的字符串复制到 `a` 中。
4. `library *p;`:定义一个 `library` 类型的指针变量 `p`。
5. `int count = 0, i = 0, j = 0;`:定义三个整型变量 `count`、`i` 和 `j`,分别表示字符串的长度和两个字符串相同字符的数量。
6. `while (*arr != '\0') { count++; arr++; }`:计算 `arr` 指向的字符串的长度并存储在 `count` 中。
7. `p = L.next;`:将 `p` 指向链表 `L` 的下一个节点。
8. `while (p) {`:循环遍历链表 `L` 的每个节点,直到 `p` 为空。
9. `strcpy(b, p->data.name);`:将 `p` 指向节点的 `name` 成员复制到 `b` 中。
10. `i = 0, j = 0;`:将 `i` 和 `j` 的值重置为 0。
11. `while (a[i] == b[j] && a[i] != '\0' && b[j] != '\0') { i++; j++; }`:比较字符数组 `a` 和 `b` 中的字符,如果相同则 `i` 和 `j` 分别加 1,直到其中一个字符串结束或者两个字符串不相同。
12. `if (i == count) {`:如果 `i` 等于 `count`(即 `a` 和 `b` 的长度相同且所有字符都相同),则执行以下代码块。
13. `strcpy(p->data.name, arr1);`:将 `arr1` 指向的字符串复制到 `p` 指向节点的 `name` 成员中。
14. `strcpy(p->data.num, arr2);`:将 `arr2` 指向的字符串复制到 `p` 指向节点的 `num` 成员中。
15. `strcpy(p->data.tel, arr3);`:将 `arr3` 指向的字符串复制到 `p` 指向节点的 `tel` 成员中。
16. `strcpy(p->data.cla, arr4);`:将 `arr4` 指向的字符串复制到 `p` 指向节点的 `cla` 成员中。
17. `strcpy(p->data.name1, arr5);`:将 `arr5` 指向的字符串复制到 `p` 指向节点的 `name1` 成员中。
18. `strcpy(p->data.time, arr6);`:将 `arr6` 指向的字符串复制到 `p` 指向节点的 `time` 成员中。
19. `strcpy(p->data.adress, arr7);`:将 `arr7` 指向的字符串复制到 `p` 指向节点的 `adress` 成员中。
20. `cout << "修改成功" << endl;`:输出修改成功的提示信息。
21. `ofstream ofs;`:定义一个输出文件流对象 `ofs`。
22. `ofs.open("text.txt", ios::out);`:打开名为 `text.txt` 的文件,以输出模式写入文件。
23. `library *S; S = L.next;`:将 `S` 指向链表 `L` 的下一个节点。
24. `while (S) { ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " " << S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl; S = S->next; }`:遍历链表 `L` 的每个节点,将每个节点的信息写入文件。
25. `ofs.close();`:关闭文件。
26. `return;`:结束函数。
27. `cout<<"查无此书,修改失败"<<endl;`:如果没有找到要修改的书籍,则输出修改失败的提示信息。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)