By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

pass by pointer

P: n/a
#include<stdio.h>
#include<alloc.h>

void func(int *p)
{
p=malloc(2);
*p=10;
}

void main(void)
{
int *ptr=NULL;
func(ptr);
printf("%d",*ptr);
}

Output : 0

why out is comming 0, it should be 10?
can any body explain me?

Feb 22 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
ravi wrote:
#include<stdio.h>
#include<alloc.h>

void func(int *p)
{
Here p has function scope.
p=malloc(2);
*p=10;
}
void func(int **p)
{
*p=malloc(2);
**p=10;
}

void main(void)
int main(void);
{
int *ptr=NULL;
func(ptr);
func(&ptr);

--
Ian Collins.
Feb 22 '07 #2

P: n/a
ravi wrote:
#include<stdio.h>
#include<alloc.h>
alloc.h is non-standard. Include stdlib.h in it's place to get
malloc's prototype.
void func(int *p)
{
p=malloc(2);
*p=10;
}

void main(void)
Use int main(void).
{
int *ptr=NULL;
func(ptr);
printf("%d",*ptr);
}

Output : 0

why out is comming 0, it should be 10?
can any body explain me?
Yes. func gets a new copy of ptr, which is locally called as p. You're
assigning the address of your allocated memory to this local variable,
*not* ptr in main. When func ends p is destroyed and you've lost track
of the malloc'ed buffer. Meanwhile ptr in main is still set to NULL.
You're deferencing it within printf, which is undefined behaviour.

To solve this problem pass the *address* of ptr to func. You can do
that like:
func(&ptr);
Now func will act on ptr itself, not it's local copy.

Feb 22 '07 #3

P: n/a
ravi said:
void main(void)
In C, main returns int.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 22 '07 #4

P: n/a
Why it works in this case?

#include<stdio.h>
#include<alloc.h>

void func(int *p)
{
*p=10;
}

void main(void)
{
int *ptr=malloc(2);
func(ptr);
printf("%d",*ptr);
}

Feb 22 '07 #5

P: n/a
On Feb 22, 12:10 am, "ravi" <dceravigu...@gmail.comwrote:
#include<stdio.h>
#include<alloc.h>

void func(int *p)
{
p=malloc(2);
*p=10;

}

void main(void)
{
int *ptr=NULL;
func(ptr);
printf("%d",*ptr);

}

Output : 0

why out is comming 0, it should be 10?
can any body explain me?
#include<stdio.h>
#include<stdlib.h>

void func(int **p)
{
*p = malloc(sizeof(**p));
**p = 10;
}

int main(void)
{
int *ptr = NULL;
func(&ptr);
printf("%d",*ptr);
return 0;
}
Feb 22 '07 #6

P: n/a
On 22 Feb, 12:05, "ravi" <dceravigu...@gmail.comwrote:
Why it works in this case?
For ease of explanation I'll reorder your code slightly...
#include<stdio.h>
#include<alloc.h>
void func(int *p); /* prototype for func */
>
void main(void)
{
int *ptr=malloc(2);
You malloc space for 2 bytes and store the address of that space in
the int pointer "ptr".
(note - this may not be a good plan. "int *ptr = malloc(sizeof *ptr);"
would allow for ints
being other than 2 bytes long).
func(ptr);
You now call "func" passing "ptr" _by value_ - func receives a fresh
int * variable, containing the same address as ptr in "main".
printf("%d",*ptr);

}
void func(int *p)
{
*p=10;
"func" stores the value 10 (decimal) in the storage pointed to by "p".
>
}
Feb 22 '07 #7

P: n/a
On Feb 22, 4:20 am, "Nishu" <naresh.at...@gmail.comwrote:
#include<stdio.h>
#include<stdlib.h>

void func(int **p)
{
*p = malloc(sizeof(**p));
**p = 10;

}

int main(void)
{
int *ptr = NULL;
func(&ptr);
printf("%d",*ptr);
return 0;

}
I forgot to free the allocation and check the allocation. Is it
necessary to free it even for a single variable?

Thanks,
Nishu

Feb 22 '07 #8

P: n/a
Nishu wrote:
On Feb 22, 4:20 am, "Nishu" <naresh.at...@gmail.comwrote:
#include<stdio.h>
#include<stdlib.h>

void func(int **p)
{
*p = malloc(sizeof(**p));
**p = 10;

}

int main(void)
{
int *ptr = NULL;
func(&ptr);
printf("%d",*ptr);
return 0;

}

I forgot to free the allocation and check the allocation. Is it
necessary to free it even for a single variable?
It's more a good habit than a practical necessity, since the whole
program is a toy one. Only if you painstakingly do it, even for
seemingly trivial cases, will you still remember to do it, when it's
really important.

Feb 22 '07 #9

P: n/a
ravi wrote:
Why it works in this case?

#include<stdio.h>
#include<alloc.h>
Drop alloc.h. If your compiler insists on it, throw away the compiler.
void func(int *p)
{
*p=10;
}

void main(void)
You're apparently not interested in learning and improving. As I told
you before, and as you read in almost every thread in this newsgroup:
In C, main *returns an int*. Change the line to:

int main(void)
{
int *ptr=malloc(2);
func(ptr);
printf("%d",*ptr);
}
Since you call malloc on the same pointer you use with printf, and
since func only changes what p *points to* and doesn't change *p
itself*, things work. For more detailed explanations consult your
textbook.

Feb 22 '07 #10

P: n/a
On Thu, 2007-02-22 at 04:32 -0800, Nishu wrote:
I forgot to free the allocation and check the allocation. Is it
necessary to free it even for a single variable?
Yes, for two reasons:
1. It's a very good habit to get into.
2. If your code ever becomes a library routine, a few bytes of lost
memory could loop into millions.

--
Andrew Poelstra <http://www.wpsoftware.net>
For email, use 'apoelstra' at the above site.
"You're only smart on the outside." -anon.

Feb 22 '07 #11

P: n/a
On 22 Feb 2007 04:05:24 -0800, in comp.lang.c , "ravi"
<dc**********@gmail.comwrote:
>Why it works in this case?
This version doesn't change the value of p inside func() before
writing 10 into where p points to. So when func() returns, p is
pointing to the place you stored 10.

The other version changed a copy of p to point somewhere else, write a
number into that location, then lost the location when func()
returned. p still pointed to the original place, which contained
nothing.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 22 '07 #12

P: n/a
[slightly edited]
>ravi wrote:
>void func(int *p) {
In article <54*************@mid.individual.net>
Ian Collins <ia******@hotmail.comwrote:
>Here p has function scope.
Well, technically, no: it has "block scope", with the block being
the entire function. This is different from "function scope",
though only because according to the C standard, only goto-labels
have "function scope". (Practically speaking, there is no real
difference.)

[remaining correct, if incomplete, corrections to ravi's code snipped]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 24 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.