Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » CGoto page 1, 2  Next

Pointer assigned by a function problem...

 
Jump to:  
 
jamaj
PostPosted: Wed Sep 03, 2008 12:53 am    Post subject: Pointer assigned by a function problem...
       
#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
 

 
Ian Collins
PostPosted: Wed Sep 03, 2008 12:53 am    Post subject: Re: Pointer assigned by a function problem...
       
jamaj wrote:
Quote:
#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.
 

 
Ian Collins
PostPosted: Wed Sep 03, 2008 12:53 am    Post subject: Re: Pointer assigned by a function problem...
       
jamaj wrote:

[please don't top-post]

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

Yes.


Quote:
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.
 

 
jamaj
PostPosted: Wed Sep 03, 2008 1:02 am    Post subject: Re: Pointer assigned by a function problem...
       
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.com> wrote:
Quote:
#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
 

 
Guest
PostPosted: Wed Sep 03, 2008 1:11 am    Post subject: Re: Pointer assigned by a function problem...
       
On Sep 3, 4:02 am, jamaj <jama...@gmail.com> wrote:
Quote:
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 is of type (double *). `p' expects (void *).
Change to:

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

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

Don't forget that it's possible for malloc to return NULL.

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

Same here; *ptr is (double *), `p' expects (void *).

Quote:
*ptr[0] = 12;

So here you have a problem, since you don't check if *p is indeed non-
null, and you just dereference it.
Change this to:

if(*ptr == NULL) return;
*ptr[0] = 12;

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

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

And here

Quote:
foo(&ptr);
printf("main ptr=%p\n",ptr);

and here.

Quote:
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

Please don't top post.
LINK
LINK
LINK
LINK
etc.
 

 
Guest
PostPosted: Wed Sep 03, 2008 1:19 am    Post subject: Re: Pointer assigned by a function problem...
       
On Sep 3, 4:11 am, vipps...@gmail.com wrote:
Quote:
On Sep 3, 4:02 am, jamaj <jama...@gmail.com> wrote:

<snip>

Ah yes, also there's a memory leak, since you never free *p.
So add a free(*p); after the last printf, and then add a return 0;
 

 
jamaj
PostPosted: Wed Sep 03, 2008 1:20 am    Post subject: Re: Pointer assigned by a function problem...
       
Quote:
Please don't top post.http://www.catb.org/jargon/html/T/top-post.htmlhttp://www.cs.tut.fi/~jkorpela/usenet/brox.htmlhttp://www.caliburn.nl/topposting.htmlhttp://www.html-faq.com/etiquette/?toppost
etc.


Ok. I won't do it anymore.

But the problem was that showed by Ian Collins. Many thanks.

Regards,

jamaj
 

 
jamaj
PostPosted: Wed Sep 03, 2008 1:26 am    Post subject: Re: Pointer assigned by a function problem...
       
On 2 set, 22:19, vipps...@gmail.com wrote:
Quote:
On Sep 3, 4:11 am, vipps...@gmail.com wrote:

On Sep 3, 4:02 am, jamaj <jama...@gmail.com> wrote:

snip

Ah yes, also there's a memory leak, since you never free *p.
So add a free(*p); after the last printf, and then add a return 0;

I think that you already know that these little snippets of code have
only an illustrative purpose, and they did the work - with or without
the memory leaks that you correctly showed. So you can rest, cause my
doubt was kindly removed by Ian Collins.

Thanks again.

jamaj
 

 
Guest
PostPosted: Wed Sep 03, 2008 1:31 am    Post subject: Re: Pointer assigned by a function problem...
       
On Sep 3, 4:26 am, jamaj <jama...@gmail.com> wrote:
Quote:
On 2 set, 22:19, vipps...@gmail.com wrote:

On Sep 3, 4:11 am, vipps...@gmail.com wrote:

On Sep 3, 4:02 am, jamaj <jama...@gmail.com> wrote:

snip
Ah yes, also there's a memory leak, since you never free *p.
So add a free(*p); after the last printf, and then add a return 0;

I think that you already know that these little snippets of code have
only an illustrative purpose, and they did the work

No they did not.

Quote:
- with or without
the memory leaks that you correctly showed. So you can rest, cause my
doubt was kindly removed by Ian Collins.
 

 
Guest
PostPosted: Wed Sep 03, 2008 1:32 am    Post subject: Re: Pointer assigned by a function problem...
       
On Sep 3, 4:20 am, jamaj <jama...@gmail.com> wrote:
Quote:
Please don't top post.

Ok. I won't do it anymore.

Thanks, now please leave the attributes untouched.

(original message was by me)
 

Page 1 of 2 .:. Goto page 1, 2  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

allianz katowice motorcycle patches teksty Schody drewniane lublin