passing by reference

S

squid

I am trying to pass a pointer to a function and in the function
allocate some memory for it using malloc and then using it in the
calling function. If I return the pointer in the function return
value and assign it to a pointer variable when I call the function it
works. But when I try to use the pointer I sent as a parameter it
says the pointer variable is undefined and I am unable to access the
allocated memory.

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

char * getbuff(char *);

void main(void)
{
char *a, *b;

b = getbuff(a*);

return;
}


char * getbuff(char * p)
{
char * buff;
buff = (char *) malloc(sizeof(char) * 8);
p = buff;
return buff;
}
 
R

red floyd

Oh. My. Goodness. There is so much wrong here, I don't know where to
start.

I guess first, you should post in comp.lang.c, as you are coding in
the strict C subset.
I am trying to pass a pointer to a function and in the function
allocate some memory for it using malloc

Do not use malloc in a C++ program. Use new.
and then using it in the
calling function.  If I return the pointer in the function return
value and assign it to a pointer variable when I call the function it
works.
 But when I try to use the pointer I sent as a parameter it
says the pointer variable is undefined and I am unable to access the
allocated memory.

What book are you using that doesn't discuss the fact that C++ uses
pass-by-value?
#include <stdio.h>
#include <stdlib.h>

char * getbuff(char *);

void main(void)
In C++, main returns int. Period.
 
S

squid

Oh. My. Goodness.  There is so much wrong here, I don't know where to
start.

I guess first, you should post in comp.lang.c, as you are coding in
the strict C subset.


Do not use malloc in a C++ program.  Use new.


What book are you using that doesn't discuss the fact that C++ uses
pass-by-value?






In C++, main returns int.  Period.



Won't compile.  Bad syntax.

I have to use C for this program. I am using "The Complete Reference"
Osborne

The program compiles in Visual Studio C++ Express Edition.
 
S

squid

I have to use C for this program.  I am using "The Complete Reference"
Osborne

The program compiles in Visual Studio C++ Express Edition.

Except for b = getbuff(a*);
that was a typo when I made my post it should be
b = getbuff(a);
 
S

squid

Except for b = getbuff(a*);
that was a typo when I made my post it should be
b = getbuff(a);

Also I had to initialize the variables so the program is as follows
and comiples and runs fine on MS ++ Express Edition.


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


char * getbuff(char *);


void main(void)
{
char *a, *b;
a = 0, b = 0;
b = getbuff(a);

return;
}


char * getbuff(char * p)
{
char * buff;
buff = (char *) malloc(sizeof(char) * 8);
p = buff;
return buff;
}
 
D

Default User

squid said:
I have to use C for this program. I am using "The Complete Reference"
Osborne

Then you are in the wrong newsgroup. You want comp.lang.c

By the way, your thread title is incorrect. You are not passing the
pointer by reference (of any sort). C has no reference mechanism, you
have to use a pointer to pointer. C++ does, but you aren't using it.





Brian
 
R

red floyd

Then you are in the wrong newsgroup. You want comp.lang.c

By the way, your thread title is incorrect. You are not passing the
pointer by reference (of any sort). C has no reference mechanism, you
have to use a pointer to pointer. C++ does, but you aren't using it.

Brian

OP deleted his post, and reposted in c.l.c.
 
B

Bo Persson

squid said:
Also I had to initialize the variables so the program is as follows
and comiples and runs fine on MS ++ Express Edition.


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


char * getbuff(char *);


void main(void)
{
char *a, *b;
a = 0, b = 0;
b = getbuff(a);

return;
}


char * getbuff(char * p)
{
char * buff;
buff = (char *) malloc(sizeof(char) * 8);
p = buff;

This assigns buff to p, not to a. To have a pointer to a, you would
need getbuff(char** p) which works, but is a lot of trouble.

return buff;
}


Why don't you just try

char* getbuff(void);

int main()
{

char* b = getbuff();

}


Bo Persson
 
D

Default User

OP deleted his post, and reposted in c.l.c.

I saw the later post to clc. "Deleting" posts is something at best
works partially. Many servers ignore all cancel requests, so you tend
to end up with a situation where some people see it and some don't. Not
to mention that it doesn't matter once others have seen it and replied.
It's not like the OP can remove the thread.

In general, it's better to post a reply that indicates the shift to a
new group.




Brian
 
R

red floyd

I saw the later post to clc. "Deleting" posts is something at best
works partially. Many servers ignore all cancel requests, so you tend
to end up with a situation where some people see it and some don't. Not
to mention that it doesn't matter once others have seen it and replied.
It's not like the OP can remove the thread.

OP asked me to delete my post on the grounds that it had his private
(as opposed to public posting) email. Hence I removed mine as a
favor. Good luck with that, though, John.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top