Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: passing by reference

1 view
Skip to first unread message
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Default User

unread,
Jul 27, 2009, 3:41:23 PM7/27/09
to
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

red floyd

unread,
Jul 27, 2009, 4:34:45 PM7/27/09
to

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

Bo Persson

unread,
Jul 27, 2009, 4:35:56 PM7/27/09
to
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) * 8);
>>>>> 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) * 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


Default User

unread,
Jul 27, 2009, 5:11:37 PM7/27/09
to
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

red floyd

unread,
Jul 27, 2009, 8:41:19 PM7/27/09
to
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.

Jack Klein

unread,
Jul 27, 2009, 10:31:47 PM7/27/09
to
On Mon, 27 Jul 2009 11:34:13 -0700 (PDT), red floyd
<redf...@gmail.com> wrote in comp.lang.c++:

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

That's an extremely ignorant attitude to take, just plain wrong.

Where in the C standard is there any definition of what any code does
when compiled as C++? Even one example?

If the OP's program can be corrected to be proper C++, who are you to
take it upon yourself to redirect him to comp.lang.c?

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

What sources are you using that don't mention the fact that C++
actually has true pass by reference, although C does not?

--
Jack Klein http://JK-Technology.Com
FAQs for
news:comp.lang.c http://c-faq.com/
news:comp.lang.c++ http://www.parashift.com/c++-faq-lite/
news:alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

0 new messages