473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

pass by pointer

#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
12 7226
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
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
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
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
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
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
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
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
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
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
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
[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 (40°39.22'N, 111°50.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: DamonChong | last post by:
Hi, I am still struggling to master C++ and is trying to understand how to achieve passing arguments using pointers. I got some questions that I like to post to the experts here, hope you can...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
4
by: S. | last post by:
Hi all, I have the requirement that I must pass-by-reference to my function addStudent() and getAge() functions where my getAge() function is within the addStudent() function. I am able to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.