为什么2星传递指针时,将一个字符串函数 [英] Why 2 stars when passing pointer to a string to a function
问题描述
在花使这一code的工作时间长,能有人来我为什么需要2星级当我通过一个指向一个字符串作为参数传递给函数解释?
一个指针,根据定义,保持地址到某个变量将被放置的存储器。所以它是一个已经得到了它自己的地址和该地址下是地址到另一个变量的变量。好的。所以,如果我传递一个指向函数的指针我使用的符号,因为我必须通过指针地址的功能。精细。
但随后发生的事情。该函数接收其中的存储区位于该指针的信息。好的。这是我的理解。我不明白的是为什么我需要两个明星,当我定义函数及其参数。我传递一个指向一个char变量。为什么不作废wpisuj(字符* W)。为什么wpisuj(字符** W)。
内存分配是understandeable我 - 我保留这个内存malloc和malloc的返回地址的内存,所以我把这个地址作为变量W的值。
然后再次东西我不明白,如果* w为指针,并保持在内存中的新创建的地方,为什么我用*宽的地方有字符串的地址。岂不是*(* W)?由于* w是保留内存的地址,那么*(* W)是该存储器中的内容。
总结。我不明白的是:
1)为什么wpisuj(字符** W),而不是wpisuj(字符* W)
2)为什么的strcpy( W,bufor)代替的strcpy(的(*宽),bufor)
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义尺寸256无效wpisuj(字符** PW){
焦炭bufor [256];
scanf函数(%S,bufor);
INT升;
L = strlen的(bufor)+1;
* PW =(字符*)malloc的(L *的sizeof(字符));
的strcpy(* PW,bufor);
}
诠释主(){
字符*宽;
wpisuj(安培; w)的;
的printf(%S,W); 返回0;
}
如果我还可以问释放内存。我是正确的思维,这是恒星的corect量(如下面的code):
无效free_memory(字符** W){
免费(*宽);
}
但是,如果我在main()释放的内存我会:
INT的main(){
W =的malloc(sizeof的(缓冲)+的sizeof(字符));
/ *一些code之前* /
自由(重量);
}
有关这将是明确的考虑下面简单的程序
的#include<&stdio.h中GT;无效F(INT X)
{
X = X + 20;
}INT主要(无效)
{
INT X = 10; 的printf(调用F之前(X)X =%d个\\ N,X); F(X); 的printf(调用F后(X)X =%d个\\ N,X); 返回0;
}
的输出将是
调用F之前(x)x = 10
调用F后(x)x = 10
当你发现X不是在函数f chenged因为对象的副本功能的交易主点¯x定义。
然而,如果你将改写功能通过以下方式
的#include<&stdio.h中GT;无效F(INT * X)
{
* X = * X + 20;
}INT主要(无效)
{
INT X = 10; 的printf(调用F之前(X)X =%d个\\ N,X); F(&安培; X); 的printf(调用F后(X)X =%d个\\ N,X); 返回0;
}
然后在这种情况下,输出将是
调用F之前(x)x = 10
调用F后(x)x = 30
因为我们传递给函数的原始对象x本身改变了原来的对象和函数内部的地址。
同样是从您的帖子指针有效。
如果您将定义一个指针
的char * p;
在主,并将其作为参数传递的功能。
无效F(字符* P);
则该函数将处理原始对象的副本。副本的任何更改不会对原有指针式影响。因此,作为第一个例子,你应该到一个指针传递给这个指针就是函数应该像
声明 无效F(焦** P);
和你要调用它像
F(&安培; P);
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);
}
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 );
这篇关于为什么2星传递指针时,将一个字符串函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!