为什么2星星将指针传递给函数的字符串(Why 2 stars when passing pointer to a string to a function)

编程入门 行业动态 更新时间:2024-10-24 12:31:14
为什么2星星将指针传递给函数的字符串(Why 2 stars when passing pointer to a string to a function)

经过很长时间的代码工作,有人可以向我解释为什么当我将一个指向字符串的指针作为参数传递给函数时,为什么我需要2颗星? 根据定义,指针将地址保存到某个变量将被放置的内存中。 所以它是一个拥有自己地址的变量,在这个地址下是另一个变量的地址。 好的。 所以如果我传递一个指向函数的指针,我使用&符号,因为我必须将指针地址传递给函数。 精细。 但接下来会发生什么。 该函数接收内存中位于该指针的信息。 好的。 这是我的理解。 我不明白的是为什么当我定义函数和它的论点时我需要两颗星。 我传递一个指向char变量的指针。 为什么不能void wpisuj(char * w)。 为什么wpisuj(char ** w)。 内存分配对我来说是不可估量的 - 我用malloc保留了内存,malloc返回了这个内存的地址,所以我把这个地址作为变量w的值。 然后又是我不明白的东西,如果* w是指针并将新创建的地址保存在内存中,为什么我使用* w来放置一个字符串。 它不应该是*(* w)吗? 由于* w是保留内存的地址,因此*(* w)是该内存的内容。

加起来。 我不明白的是:1)为什么wpisuj(char ** w)而不是wpisuj(char * w)2)为什么strcpy( w,bufor)而不是strcpy( (* w),bufor)

#include<stdlib.h> #include<stdio.h> #include<string.h> # define SIZE 256 void wpisuj(char** pw){ char bufor[256]; scanf("%s", bufor); int l; l=strlen(bufor)+1; *pw=(char*)malloc(l*sizeof(char)); strcpy(*pw, bufor); } int main(){ char* w; wpisuj(&w); printf("%s", w); return 0; }

如果我也可能会问有关释放记忆。 我是否认为这是恒星的核心数量(如下面的代码所示):

void free_memory(char **w){ free(*w); }

但是,如果我在main()中释放内存,我会有:

int main(){ w=malloc(sizeof(buffer)+sizeof(char)); /* some code before */ free(w); }

After a long time spent on making this code work, can someone explain to me why I need 2 stars when I pass a pointer to a string as an argument to the function? A pointer, by definition, keeps the address to a memory where a certain variable will be placed. So it is a variable that has got its own address and under this address is address to another variable. Okay. So if I pass a pointer to a function I use ampersand because I must pass the pointer address to the function. Fine. But then what happens. The function receives the information where in the memory is located this pointer. Okay. This is what I understand. What I do not understand is why I need two stars when I define the function and its argument. I am passing a pointer to a char variable. Why not void wpisuj(char * w). Why wpisuj(char** w). Memory allocation is understandeable to me - I reserved memory with malloc and malloc returns address of this memory, so I place this address as the value of the variable w. And then again something I do not understand, if *w is the pointer and keeps the address of the newly created place in the memory, why I use *w to place there a string. Should it not be *(*w)? Since *w is the address of the reserved memory, then *(*w) is the contents of this memory.

Summing up. What I do not understand is: 1) why wpisuj (char **w) instead of wpisuj (char *w) 2) why strcpy(w, bufor) instead of strcpy((*w), bufor)

#include<stdlib.h> #include<stdio.h> #include<string.h> # define SIZE 256 void wpisuj(char** pw){ char bufor[256]; scanf("%s", bufor); int l; l=strlen(bufor)+1; *pw=(char*)malloc(l*sizeof(char)); strcpy(*pw, bufor); } int main(){ char* w; wpisuj(&w); printf("%s", w); return 0; }

And if I may also ask about freeing the memory. Am I correct in thinking that this is the corect amount of stars (as in the code below):

void free_memory(char **w){ free(*w); }

but, if I freed memory in main() I would have:

int main(){ w=malloc(sizeof(buffer)+sizeof(char)); /* some code before */ free(w); }

最满意答案

很明显,考虑以下简单的程序

#include <stdio.h> void f( int x ) { x = x + 20; } int main(void) { int x = 10; printf( "Before the call f( x ) x = %d\n", x ); f( x ); printf( "After the call f( x ) x = %d\n", x ); return 0; }

输出将是

Before the call f( x ) x = 10 After the call f( x ) x = 10

正如你所看到的,x在函数f中不是chenged,因为该函数处理在main中定义的对象x的副本。

但是,如果您将按以下方式重写该函数

#include <stdio.h> void f( int *x ) { *x = *x + 20; } int main(void) { int x = 10; printf( "Before the call f( x ) x = %d\n", x ); f( &x ); printf( "After the call f( x ) x = %d\n", x ); return 0; }

那么在这种情况下,输出将是

Before the call f( x ) x = 10 After the call f( x ) x = 30

因为我们向函数传递了原始对象x的地址,并在函数内部改变了原始对象本身。

您的帖子中的指针同样有效。

如果你将定义一个指针

char *p;

并将其作为参数传递给函数

void f( char *p );

那么该函数将处理原始对象的副本。 副本的任何更改都不会影响原始指针。 因此,在第一个示例中,您应该将指针传递给该指针,该函数应该声明为

void f( char **p );

你必须这样称呼它

f( &p );

For it would be clear consider the following simple program

#include <stdio.h> void f( int x ) { x = x + 20; } int main(void) { int x = 10; printf( "Before the call f( x ) x = %d\n", x ); f( x ); printf( "After the call f( x ) x = %d\n", x ); return 0; }

The output will be

Before the call f( x ) x = 10 After the call f( x ) x = 10

As you see x was not chenged in function f because the function deals with a copy of object x defined in main.

However if you will rewrite the function the following way

#include <stdio.h> void f( int *x ) { *x = *x + 20; } int main(void) { int x = 10; printf( "Before the call f( x ) x = %d\n", x ); f( &x ); printf( "After the call f( x ) x = %d\n", x ); return 0; }

then in this case the output will be

Before the call f( x ) x = 10 After the call f( x ) x = 30

because we passed to the function the address of the original object x and inside the function the original object itself was changed.

The same is valid with the pointer from your post.

If you will define a pointer

char *p;

in main and pass it as an argument to function

void f( char *p );

then the function will deal with a copy of the original object. Any changes of the copy do not influence on the original pointer. So as in the first example you should to pass a pointer to this pointer that is the function should be declared like

void f( char **p );

and you have to call it like

f( &p );

更多推荐

本文发布于:2023-07-25 22:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   指针   函数   星星   stars

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!