在函数中使用 char **,我们可以为传递给她的 char * 分配内存
Using char ** in function, that we could malloc memory for char * that is passed to her
阅读:
Scope of malloc used in a function
Memory allocated with malloc does not persist outside function scope?
和其他与此问题相关的问题
和万维网上的其他内容,所以请继续阅读...
不久之后,我想,我从来没有 alloc
喜欢这个 char *
的记忆,但我认为它可能有用:
void _alloc(char *line)
*line = malloc(sizeof(char) * BUFSIZE);
忽略 _alloc
和 main
中的错误处理程序。主要的是 SEGFAULT
这样的部分:
main()
{
char *text, *start;
_alloc(text);
start = text;
// add contents in text briefly
*text++ = 'd'; *text = '[=11=]'; // SEGFAULT
fprintf(stdout, "%s", start);
}
我有一张 _alloc 函数的图片是这样工作的:
main()
{
char *text;
/// function _alloc:
char *tmp = text;
*tmp = malloc ( sizeof(char) * BUFSIZE);
/// end
}
还有...
当我尝试这个时,它给了我 警告:赋值从指针生成整数,没有强制转换和 SEGF。
我的照片是:
tmp = text; // tmp point at address of line
*tmp = malloc(...); // tmp change to what point line...
而且我看到我需要 _alloc(char **) ,但不知道它是如何与 ** 一起工作的。我试着用 array[][] 来操作,我知道。但是无法获取图片,为什么它需要是char **?
这是部分答案:
Here, line
is a local variable within _alloc
. Pointers are passed by
value in C, so a receives a copy of the pointer in main when you do
_alloc(text);
< answer user nos (modified with my name of variables) on Scope of malloc used in a function
P.S。我知道这样写函数更简单 char *_alloc();
应该是
void _alloc(char **line){
*line = malloc(sizeof(char) * BUFSIZE);
}
这样使用:
/* ... */
char *buf;
_alloc(&buf);
buf
是一个指针。您使用 &buf
获得指向它的指针,然后将其传递给 _alloc
,它取消引用指向指针的指针,获取 buf
的值,并将其更改为指向新的分配的内存。
C 是严格按值传递的,因此指针与变量没有区别对待。即,要理解这一点,您可以考虑类比以下代码:
void changeValue(char *chr){
*chr = 'a';
}
(像这样使用:)
/* ... */
char buf;
changeValue(&buf);
让我们举一个简单的例子,用一个整数。
假设我们有函数:
void foo(int n)
{
n = 3;
}
int main(void)
{
int number = 5;
foo(number);
}
我想你会同意number
在foo(number)
之后没有被修改。这是因为 n
只是 number
的一个副本,一个在 foo
退出后被销毁的局部变量。
这就是我们使用指针的原因。但是指针是变量,两者是完全一样的东西。看看这个例子:
void bar(int *n)
{
*n = 3;
int *temp = malloc(sizeof(int));
*temp = 6;
n = temp;
}
int main(void)
{
int number = 5;
int *number_p = &number;
bar(number_p);
}
在这里,number = 3
在 bar(number_p)
之后,因为我们将整数的地址传递给 bar
。但是,地址本身是原始指针的副本。
因此,指令 n = temp;
不会对 number_p
做任何事情,因为 n 只是一个局部变量。
这就是为什么我们需要使用指向指针的指针。
通过下面的例子,可以修改foo
函数内部的原始指针:
void foo(int **n)
{
**n = 3;
int *temp = malloc(sizeof(int));
*temp = 6;
*n = temp;
}
int main(void)
{
int number = 5;
int *number_p = &number;
foo(&number_p); //We pass the adress of the pointer number_p
}
在foo(&number_p)
之后,number
是3,而number_p
是指向temp
的指针,因为我们可以修改[=20=中的地址本身].
在你的例子中,你需要修改_alloc
函数中的指针,所以签名应该是
void _alloc(char **line)
并且应该使用
修改指针
*line = malloc(...);
另外,_alloc
应该这样称呼
char* s;
_alloc(&s);
对不起,我的英语不好,为了表达清楚,我已经尽力了。
阅读:
Scope of malloc used in a function
Memory allocated with malloc does not persist outside function scope?
和其他与此问题相关的问题 和万维网上的其他内容,所以请继续阅读...
不久之后,我想,我从来没有 alloc
喜欢这个 char *
的记忆,但我认为它可能有用:
void _alloc(char *line)
*line = malloc(sizeof(char) * BUFSIZE);
忽略 _alloc
和 main
中的错误处理程序。主要的是 SEGFAULT
这样的部分:
main()
{
char *text, *start;
_alloc(text);
start = text;
// add contents in text briefly
*text++ = 'd'; *text = '[=11=]'; // SEGFAULT
fprintf(stdout, "%s", start);
}
我有一张 _alloc 函数的图片是这样工作的:
main()
{
char *text;
/// function _alloc:
char *tmp = text;
*tmp = malloc ( sizeof(char) * BUFSIZE);
/// end
}
还有... 当我尝试这个时,它给了我 警告:赋值从指针生成整数,没有强制转换和 SEGF。 我的照片是:
tmp = text; // tmp point at address of line
*tmp = malloc(...); // tmp change to what point line...
而且我看到我需要 _alloc(char **) ,但不知道它是如何与 ** 一起工作的。我试着用 array[][] 来操作,我知道。但是无法获取图片,为什么它需要是char **? 这是部分答案:
Here,
line
is a local variable within_alloc
. Pointers are passed by value in C, so a receives a copy of the pointer in main when you do_alloc(text);
< answer user nos (modified with my name of variables) on Scope of malloc used in a function
P.S。我知道这样写函数更简单 char *_alloc();
应该是
void _alloc(char **line){
*line = malloc(sizeof(char) * BUFSIZE);
}
这样使用:
/* ... */
char *buf;
_alloc(&buf);
buf
是一个指针。您使用 &buf
获得指向它的指针,然后将其传递给 _alloc
,它取消引用指向指针的指针,获取 buf
的值,并将其更改为指向新的分配的内存。
C 是严格按值传递的,因此指针与变量没有区别对待。即,要理解这一点,您可以考虑类比以下代码:
void changeValue(char *chr){
*chr = 'a';
}
(像这样使用:)
/* ... */
char buf;
changeValue(&buf);
让我们举一个简单的例子,用一个整数。
假设我们有函数:
void foo(int n)
{
n = 3;
}
int main(void)
{
int number = 5;
foo(number);
}
我想你会同意number
在foo(number)
之后没有被修改。这是因为 n
只是 number
的一个副本,一个在 foo
退出后被销毁的局部变量。
这就是我们使用指针的原因。但是指针是变量,两者是完全一样的东西。看看这个例子:
void bar(int *n)
{
*n = 3;
int *temp = malloc(sizeof(int));
*temp = 6;
n = temp;
}
int main(void)
{
int number = 5;
int *number_p = &number;
bar(number_p);
}
在这里,number = 3
在 bar(number_p)
之后,因为我们将整数的地址传递给 bar
。但是,地址本身是原始指针的副本。
因此,指令 n = temp;
不会对 number_p
做任何事情,因为 n 只是一个局部变量。
这就是为什么我们需要使用指向指针的指针。
通过下面的例子,可以修改foo
函数内部的原始指针:
void foo(int **n)
{
**n = 3;
int *temp = malloc(sizeof(int));
*temp = 6;
*n = temp;
}
int main(void)
{
int number = 5;
int *number_p = &number;
foo(&number_p); //We pass the adress of the pointer number_p
}
在foo(&number_p)
之后,number
是3,而number_p
是指向temp
的指针,因为我们可以修改[=20=中的地址本身].
在你的例子中,你需要修改_alloc
函数中的指针,所以签名应该是
void _alloc(char **line)
并且应该使用
修改指针*line = malloc(...);
另外,_alloc
应该这样称呼
char* s;
_alloc(&s);
对不起,我的英语不好,为了表达清楚,我已经尽力了。