*str和str[]一样吗?
时间: 2023-11-23 07:05:45 浏览: 40
在函数参数中,`*str` 和 `str[]` 是等价的,它们都表示一个指向字符串的指针。例如:
```c
void printString(char *str) {
printf("%s\n", str);
}
void main() {
char str[] = "Hello, world!";
printString(str);
}
```
这个程序中,`printString` 函数接收一个指向字符串的指针,可以使用 `*str` 或 `str[]` 来表示。在 `main` 函数中,我们定义了一个字符串数组 `str`,然后把它传递给 `printString` 函数,由于 `str` 表示数组的首地址,所以可以直接传递给指针类型的参数。
需要注意的是,`*str` 和 `str[]` 在其他语境下可能有不同的含义。在定义变量时,`char *str` 表示一个指向字符的指针,而 `char str[]` 表示一个字符数组。在使用时需要根据具体情况理解。
相关问题
String str=i与 String str=new String(“i”)一样吗?
不完全一样。
在Java中,使用“=”运算符创建对象时,它将引用现有的对象。因此,当您执行以下操作时:
```
String str1 = "i";
String str2 = "i";
```
它们引用相同的字符串对象,因为字符串常量是在编译时创建的,并在内存中重用。
另一方面,当您使用new运算符创建字符串对象时,它会在堆中创建一个新的字符串对象。因此,当您执行以下操作时:
```
String str1 = new String("i");
String str2 = new String("i");
```
它们引用不同的字符串对象,即使它们包含相同的字符序列。
需要注意的是,如果您在创建字符串时使用new运算符,则会创建一个新的字符串对象,即使已经存在具有相同字符序列的字符串常量。因此,以下代码会创建两个不同的字符串对象:
```
String str1 = "i";
String str2 = new String("i");
```
因此,str1和str2引用不同的字符串对象。
函数声明:void DES(int *str,int key); 主函数中调用函数:DES(str,key); 已知主函数中str和key都是为大小为64的int数组 函数中str[0]是什么?
在函数中,`str[0]` 表示传递给函数的 int 数组中的第一个元素。由于在函数调用时,我们传递的是数组的地址(即指向数组第一个元素的指针),因此在函数内部可以使用 `str[0]` 来访问数组的第一个元素。实际上,`str[0]` 和 `*str` 是等价的,它们都表示指针所指向的内存中的第一个 int 类型的变量。
因此,如果你想在函数中访问传递给函数的 int 数组中的第一个元素,可以使用 `str[0]` 或者 `*str`,它们的效果是一样的。
相关推荐
![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)