Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > passing by reference

Reply
Thread Tools

passing by reference

 
 
squid
Guest
Posts: n/a
 
      07-27-2009
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) * ;
p = buff;
return buff;
}
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      07-27-2009
On Jul 27, 11:27*am, squid <jvstew...@gmail.com> wrote:

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.

> {
> char *a, *b;
>
> b = getbuff(a*);

Won't compile. Bad syntax.
>
> return;
>
> }
>
> char * getbuff(char * p)
> {
> * * * * char * buff;
> * * * * buff = (char *) malloc(sizeof(char) * ;
> * * * * p = buff;
> * * return buff;
>
> }





 
Reply With Quote
 
 
 
 
squid
Guest
Posts: n/a
 
      07-27-2009
On Jul 27, 2:34*pm, red floyd <redfl...@gmail.com> wrote:
> On Jul 27, 11:27*am, squid <jvstew...@gmail.com> wrote:
>
> 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.
>
> > {
> > char *a, *b;

>
> > b = getbuff(a*);

>
> Won't compile. *Bad syntax.
>
>
>
> > return;

>
> > }

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

>
> > }

>
>


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

The program compiles in Visual Studio C++ Express Edition.
 
Reply With Quote
 
squid
Guest
Posts: n/a
 
      07-27-2009
On Jul 27, 2:43*pm, squid <jvstew...@gmail.com> wrote:
> On Jul 27, 2:34*pm, red floyd <redfl...@gmail.com> wrote:
>
>
>
> > On Jul 27, 11:27*am, squid <jvstew...@gmail.com> wrote:

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

>
> > > {
> > > char *a, *b;

>
> > > b = getbuff(a*);

>
> > Won't compile. *Bad syntax.

>
> > > return;

>
> > > }

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

>
> > > }

>
> 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);
 
Reply With Quote
 
squid
Guest
Posts: n/a
 
      07-27-2009
On Jul 27, 2:49*pm, squid <jvstew...@gmail.com> wrote:
> On Jul 27, 2:43*pm, squid <jvstew...@gmail.com> wrote:
>
>
>
> > On Jul 27, 2:34*pm, red floyd <redfl...@gmail.com> wrote:

>
> > > On Jul 27, 11:27*am, squid <jvstew...@gmail.com> wrote:

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

>
> > > > {
> > > > char *a, *b;

>
> > > > b = getbuff(a*);

>
> > > Won't compile. *Bad syntax.

>
> > > > return;

>
> > > > }

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

>
> > > > }

>
> > 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);


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) * ;
p = buff;
return buff;
}
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      07-27-2009
squid wrote:

> 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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      07-27-2009
On Jul 27, 12:41*pm, "Default User" <defaultuse...@yahoo.com> wrote:
> squid wrote:
> > 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


OP deleted his post, and reposted in c.l.c.
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      07-27-2009
squid wrote:
> On Jul 27, 2:49 pm, squid <jvstew...@gmail.com> wrote:
>> On Jul 27, 2:43 pm, squid <jvstew...@gmail.com> wrote:
>>
>>
>>
>>> On Jul 27, 2:34 pm, red floyd <redfl...@gmail.com> wrote:

>>
>>>> On Jul 27, 11:27 am, squid <jvstew...@gmail.com> wrote:

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

>>
>>>>> {
>>>>> char *a, *b;

>>
>>>>> b = getbuff(a*);

>>
>>>> Won't compile. Bad syntax.

>>
>>>>> return;

>>
>>>>> }

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

>>
>>>>> }

>>
>>> 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);

>
> 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) * ;
> 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


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      07-27-2009
red floyd wrote:

> On Jul 27, 12:41*pm, "Default User" <defaultuse...@yahoo.com> wrote:
> > squid wrote:
> > > 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.


> 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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      07-28-2009
On Jul 27, 2:11*pm, "Default User" <defaultuse...@yahoo.com> wrote:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a reference as a reference pamelamishra@gmail.com Perl Misc 2 08-16-2007 09:48 PM
asp.net 2005 question re: "reference to a non-shared member requires an object reference" ce ASP .Net 1 06-23-2005 09:15 PM
web reference interfering with reference to component Dude ASP .Net 0 11-09-2004 11:53 AM
How to tell if a reference is project or file reference from within the IDE? Darren ASP .Net 0 10-11-2004 12:51 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57