由功能问题分配的指针...... [英] Pointer assigned by a function problem...

查看:70
本文介绍了由功能问题分配的指针......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdlib.h>

#include< stdio.h>


void foo(double * ptr)

{

printf(" foo ptr =%p \ n",ptr);

ptr = malloc(sizeof(* ptr));

printf(" foo ptr =%p \ n",ptr);

ptr [0] = 12;

printf(" ; foo value =%f\ n",* ptr);

}

int main(int argc,char ** argv)

{

double * ptr = NULL;

printf(" main ptr =%p \ n",ptr);

foo (ptr);

printf(" main ptr =%p \ n",ptr);

}

返回:


main ptr =(nil)

foo ptr =(nil)

foo ptr = 0x804a008

foo值= 12.000000

main ptr =(nil)


为什么主函数中的指针没有更新?


提前Thnaks。


jamaj

解决方案

jamaj写道:


#include< stdlib.h>

#include< stdio.h>


void foo(double * ptr)

{

printf(" foo ptr =%p \ n",ptr);

ptr = malloc(sizeof(* ptr));

printf(" foo ptr =%p \ n",ptr);

ptr [0] = 12;

printf(" foo value) =%f \ n",* ptr);

}


int main(int argc,char ** argv)

{

double * ptr = NULL;

printf(" main ptr =%p \ n",ptr);

foo(ptr);

printf(" main ptr =%p \ n",ptr);

}


返回:


main ptr =(nil)

foo ptr =(nil)

foo ptr = 0x804a008
foo value = 12.000000

main ptr =(nil)


为什么主函数中的指针没有更新?



因为您要更新foo中的本地副本。 ptr in foo是

副本中的主要内容。


你必须写一下


void foo( double ** ptr)

{

* ptr = malloc(sizeof(* ptr));

}

-

Ian Collins。


我已经改变了这个版本,并且它有效。但这是唯一的

和正确的方式吗?


void foo(double ** ptr)

{

printf(" foo ptr =%p \ n",* ptr);

* ptr = malloc(sizeof(** ptr));

printf(" foo ptr =%p \ n",* ptr);

* ptr [0] = 12;

printf(" foo value = %f \ n",** ptr);

}

int main(int argc,char ** argv)

{

double * ptr = NULL;

printf(" main ptr =%p \ n",ptr);

foo(& ptr);

printf(" main ptr =%p \ n",ptr);

printf(" main value =%f\ n", * ptr);

}


现在的结果如下:


main ptr =(nil)

foo ptr =(nil)

foo ptr = 0x804a008

foo value = 12.000000

main ptr = 0x804a008

主要值= 12.000000

2集,21:53,jamaj< jama ... @ gmail.comwrote:


#include< stdlib.h>

#include< stdio.h>


void foo(double * ptr)

{

* * * * printf(" foo ptr =%p \ n",ptr);

* * * * ptr = malloc(sizeof(* ptr));

* * * * printf(" foo ptr =%p \ n",ptr);

* * * * ptr [0] = 12;

* * * * printf(" foo value =%f\ n,* ptr);


}


int main( int argc,char ** argv)

{

* * * * double * ptr = NULL;

* * * * printf(" ; main ptr =%p \ n",ptr);

* * * * foo(ptr);

* * * * printf(" main ptr = %p \ n",ptr);


}


退货:


main ptr =(nil)

foo ptr =(nil)

foo ptr = 0x804a008

foo value = 12.000000

main ptr =(nil)


为什么主函数中的指针没有更新?


提前提醒。


jamaj


jamaj wr ote:


[请不要顶一下]


我已修改此版本,并且它有效。但这是唯一的

和正确的方式吗?



是的。


void foo(double ** ptr)

{

printf(" foo ptr =%p \ n",* ptr);

* ptr = malloc(sizeof(** ptr));

printf(" foo ptr =%p \ n",* ptr);

* ptr [0] = 12;

printf(" foo value =%f \ n",** ptr);

}


-

Ian Collins。


#include <stdlib.h>
#include <stdio.h>

void foo(double *ptr)
{
printf("foo ptr=%p\n",ptr);
ptr = malloc(sizeof(*ptr));
printf("foo ptr=%p\n",ptr);
ptr[0] = 12;
printf("foo value=%f\n",*ptr);
}
int main(int argc, char **argv)
{
double *ptr=NULL;
printf("main ptr=%p\n",ptr);
foo(ptr);
printf("main ptr=%p\n",ptr);
}
Returns:

main ptr=(nil)
foo ptr=(nil)
foo ptr=0x804a008
foo value=12.000000
main ptr=(nil)

Why the pointer in the main function is not updated?

Thnaks in advance.

jamaj

解决方案

jamaj wrote:

#include <stdlib.h>
#include <stdio.h>

void foo(double *ptr)
{
printf("foo ptr=%p\n",ptr);
ptr = malloc(sizeof(*ptr));
printf("foo ptr=%p\n",ptr);
ptr[0] = 12;
printf("foo value=%f\n",*ptr);
}
int main(int argc, char **argv)
{
double *ptr=NULL;
printf("main ptr=%p\n",ptr);
foo(ptr);
printf("main ptr=%p\n",ptr);
}
Returns:

main ptr=(nil)
foo ptr=(nil)
foo ptr=0x804a008
foo value=12.000000
main ptr=(nil)

Why the pointer in the main function is not updated?

Because you are updating the local copy in foo. ptr in foo is copy of
the one in main.

You have to write

void foo(double **ptr)
{
*ptr = malloc(sizeof(*ptr));
}

--
Ian Collins.


I have altered for this version, and it worked. But is this the unique
and right way?

void foo(double **ptr)
{
printf("foo ptr=%p\n",*ptr);
*ptr = malloc(sizeof(**ptr));
printf("foo ptr=%p\n",*ptr);
*ptr[0] = 12;
printf("foo value=%f\n",**ptr);
}
int main(int argc, char **argv)
{
double *ptr=NULL;
printf("main ptr=%p\n",ptr);
foo(&ptr);
printf("main ptr=%p\n",ptr);
printf("main value=%f\n",*ptr);
}

Now the results are:

main ptr=(nil)
foo ptr=(nil)
foo ptr=0x804a008
foo value=12.000000
main ptr=0x804a008
main value=12.000000
On 2 set, 21:53, jamaj <jama...@gmail.comwrote:

#include <stdlib.h>
#include <stdio.h>

void foo(double *ptr)
{
* * * * printf("foo ptr=%p\n",ptr);
* * * * ptr = malloc(sizeof(*ptr));
* * * * printf("foo ptr=%p\n",ptr);
* * * * ptr[0] = 12;
* * * * printf("foo value=%f\n",*ptr);

}

int main(int argc, char **argv)
{
* * * * double *ptr=NULL;
* * * * printf("main ptr=%p\n",ptr);
* * * * foo(ptr);
* * * * printf("main ptr=%p\n",ptr);

}

Returns:

main ptr=(nil)
foo ptr=(nil)
foo ptr=0x804a008
foo value=12.000000
main ptr=(nil)

Why the pointer in the main function is not updated?

Thnaks in advance.

jamaj


jamaj wrote:

[please don''t top-post]

I have altered for this version, and it worked. But is this the unique
and right way?

Yes.

void foo(double **ptr)
{
printf("foo ptr=%p\n",*ptr);
*ptr = malloc(sizeof(**ptr));
printf("foo ptr=%p\n",*ptr);
*ptr[0] = 12;
printf("foo value=%f\n",**ptr);
}

--
Ian Collins.


这篇关于由功能问题分配的指针......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆