在函数内更改 char 数组的地址
changing adress of char array inside a function
我在玩指针和数组,我发现很难理解为什么这段代码不起作用。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void change(char **test)
{
char *test1 = malloc(sizeof(char)*17);
strcpy(test1, "HEAP");
*test = test1;
}
int main()
{
char str[15]= "hello World"; //Array of char
change(&str); // Passing the address of my array of char
printf("%s", str);
}
目标是将str
的地址改为函数内部分配的内存数组change
但换行:
char str[15]= "hello World";
至此(只读对吗?):
char *str= "hello World";
此外,我在编译时收到警告,我也不明白:
main.c:17:12: warning: passing argument 1 of ‘change’ from incompatible pointer type [-Wincompatible-pointer-types]
17 | change(&str); // Passing the address of my array of char
| ^~~~
| |
| char (*)[15]
main.c:5:20: note: expected ‘char **’ but argument is of type ‘char (*)[15]’
5 | void change(char **test)
谢谢!
char str[15]
不是指针,因此您无法更改其地址。您需要 str
才能成为 char*
。
示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HEAP "HEAP"
void change(char **test)
{
char *test1 = malloc(sizeof HEAP); // sizeof(char) is defined to be 1
strcpy(test1, HEAP);
*test = test1;
}
int main()
{
char orig[] = "hello World";
char *str = orig; // initialize it to point at the first char in orig
printf("before: %s\n", str);
change(&str);
printf("after : %s\n", str);
free(str); // free what you've allocated
}
The goal is to change the address of str
to the allocated memory array inside the function change...
您不能更改变量的地址。如果一个变量是一个 non-const 指针,你可以让它指向一些其他的内存地址,前提是它应该是与指针类型兼容的类型。
在继续之前,数组的几点说明:
- 数组不是指针。他们都是不同的。
- 在 C 中,数组名称是不可修改的左值。
您收到的警告消息非常清楚地传达了信息 - 您传递给 change()
函数的参数类型与其参数类型不兼容。 &str
参数的类型(传递给 change()
)是 char (*)[15]
,test
参数的类型是 char **
.
让我们更改 change()
函数的 test
参数的类型,并使其与您传递给 change()
函数的 &str
的类型兼容,并检查什么发生了!
在您的程序中进行以下更改
void change(char (*test)[15])
^^^^^^^^^^^
编译程序时进行此更改,将出现以下错误:
# gcc -Wall -Wextra prg.c
prg.c:10:11: error: array type 'char [15]' is not assignable
*test = test1;
~~~~~ ^
1 error generated.
发生错误是因为 test
是指向 str
数组的指针,当您取消引用它时(即 *test
),您会得到类型为 char [15]
的数组并且,这意味着,该语句最终将尝试将 test1
分配给类型为 char [15]
的数组(在此上下文中,为数组 str
)。检查上面的第二点——数组名是不可修改的左值。因此,出现此错误。
因此,数组是不可赋值的,但是,您可以使用传递给 change()
函数的 str
地址来更改 change()
函数中数组的内容。你可以这样做:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_CHARS 15
void change(char (*test)[NUM_CHARS]) {
strcpy (*test, "HEAP");
}
int main (void) {
char str[NUM_CHARS]= "hello World"; //Array of char
printf ("Before calling change(), str : %s\n", str);
change (&str); // Passing the address of my array of char
printf ("After calling change(), str : %s\n", str);
return 0;
}
输出:
# ./a.out
Before calling change(), str : hello World
After calling change(), str : HEAP
请注意,您可以通过将 str
传递给 change()
函数来实现相同的效果,而不是传递 str
的地址(当然,您必须在change()
也有功能)。
如何使指针指向函数内的其他一些内存已显示在其他 post(@Ted Lyngmo)中。
遵循良好的编程习惯:
- 始终检查
malloc()
return。
- 确保在完成动态分配内存后释放它。
我在玩指针和数组,我发现很难理解为什么这段代码不起作用。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void change(char **test)
{
char *test1 = malloc(sizeof(char)*17);
strcpy(test1, "HEAP");
*test = test1;
}
int main()
{
char str[15]= "hello World"; //Array of char
change(&str); // Passing the address of my array of char
printf("%s", str);
}
目标是将str
的地址改为函数内部分配的内存数组change
但换行:
char str[15]= "hello World";
至此(只读对吗?):
char *str= "hello World";
此外,我在编译时收到警告,我也不明白:
main.c:17:12: warning: passing argument 1 of ‘change’ from incompatible pointer type [-Wincompatible-pointer-types]
17 | change(&str); // Passing the address of my array of char
| ^~~~
| |
| char (*)[15]
main.c:5:20: note: expected ‘char **’ but argument is of type ‘char (*)[15]’
5 | void change(char **test)
谢谢!
char str[15]
不是指针,因此您无法更改其地址。您需要 str
才能成为 char*
。
示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HEAP "HEAP"
void change(char **test)
{
char *test1 = malloc(sizeof HEAP); // sizeof(char) is defined to be 1
strcpy(test1, HEAP);
*test = test1;
}
int main()
{
char orig[] = "hello World";
char *str = orig; // initialize it to point at the first char in orig
printf("before: %s\n", str);
change(&str);
printf("after : %s\n", str);
free(str); // free what you've allocated
}
The goal is to change the address of
str
to the allocated memory array inside the function change...
您不能更改变量的地址。如果一个变量是一个 non-const 指针,你可以让它指向一些其他的内存地址,前提是它应该是与指针类型兼容的类型。
在继续之前,数组的几点说明:
- 数组不是指针。他们都是不同的。
- 在 C 中,数组名称是不可修改的左值。
您收到的警告消息非常清楚地传达了信息 - 您传递给 change()
函数的参数类型与其参数类型不兼容。 &str
参数的类型(传递给 change()
)是 char (*)[15]
,test
参数的类型是 char **
.
让我们更改 change()
函数的 test
参数的类型,并使其与您传递给 change()
函数的 &str
的类型兼容,并检查什么发生了!
在您的程序中进行以下更改
void change(char (*test)[15])
^^^^^^^^^^^
编译程序时进行此更改,将出现以下错误:
# gcc -Wall -Wextra prg.c
prg.c:10:11: error: array type 'char [15]' is not assignable
*test = test1;
~~~~~ ^
1 error generated.
发生错误是因为 test
是指向 str
数组的指针,当您取消引用它时(即 *test
),您会得到类型为 char [15]
的数组并且,这意味着,该语句最终将尝试将 test1
分配给类型为 char [15]
的数组(在此上下文中,为数组 str
)。检查上面的第二点——数组名是不可修改的左值。因此,出现此错误。
因此,数组是不可赋值的,但是,您可以使用传递给 change()
函数的 str
地址来更改 change()
函数中数组的内容。你可以这样做:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_CHARS 15
void change(char (*test)[NUM_CHARS]) {
strcpy (*test, "HEAP");
}
int main (void) {
char str[NUM_CHARS]= "hello World"; //Array of char
printf ("Before calling change(), str : %s\n", str);
change (&str); // Passing the address of my array of char
printf ("After calling change(), str : %s\n", str);
return 0;
}
输出:
# ./a.out
Before calling change(), str : hello World
After calling change(), str : HEAP
请注意,您可以通过将 str
传递给 change()
函数来实现相同的效果,而不是传递 str
的地址(当然,您必须在change()
也有功能)。
如何使指针指向函数内的其他一些内存已显示在其他 post(@Ted Lyngmo)中。
遵循良好的编程习惯:
- 始终检查
malloc()
return。 - 确保在完成动态分配内存后释放它。