passing by reference

Discussion in 'C++' started by squid, Jul 27, 2009.

  1. squid

    squid Guest

    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;
    }
     
    squid, Jul 27, 2009
    #1
    1. Advertising

  2. squid

    red floyd Guest

    On Jul 27, 11:27 am, squid <> 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;
    >
    > }
     
    red floyd, Jul 27, 2009
    #2
    1. Advertising

  3. squid

    squid Guest

    On Jul 27, 2:34 pm, red floyd <> wrote:
    > On Jul 27, 11:27 am, squid <> 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.
     
    squid, Jul 27, 2009
    #3
  4. squid

    squid Guest

    On Jul 27, 2:43 pm, squid <> wrote:
    > On Jul 27, 2:34 pm, red floyd <> wrote:
    >
    >
    >
    > > On Jul 27, 11:27 am, squid <> 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);
     
    squid, Jul 27, 2009
    #4
  5. squid

    squid Guest

    On Jul 27, 2:49 pm, squid <> wrote:
    > On Jul 27, 2:43 pm, squid <> wrote:
    >
    >
    >
    > > On Jul 27, 2:34 pm, red floyd <> wrote:

    >
    > > > On Jul 27, 11:27 am, squid <> 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;
    return buff;
    }
     
    squid, Jul 27, 2009
    #5
  6. squid

    Default User Guest

    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
     
    Default User, Jul 27, 2009
    #6
  7. squid

    red floyd Guest

    On Jul 27, 12:41 pm, "Default User" <> 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.
     
    red floyd, Jul 27, 2009
    #7
  8. squid

    Bo Persson Guest

    squid wrote:
    > On Jul 27, 2:49 pm, squid <> wrote:
    >> On Jul 27, 2:43 pm, squid <> wrote:
    >>
    >>
    >>
    >>> On Jul 27, 2:34 pm, red floyd <> wrote:

    >>
    >>>> On Jul 27, 11:27 am, squid <> 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
     
    Bo Persson, Jul 27, 2009
    #8
  9. squid

    Default User Guest

    red floyd wrote:

    > On Jul 27, 12:41 pm, "Default User" <> 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
     
    Default User, Jul 27, 2009
    #9
  10. squid

    red floyd Guest

    On Jul 27, 2:11 pm, "Default User" <> 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.
     
    red floyd, Jul 28, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    556
    E. Robert Tisdale
    Jul 16, 2003
  2. Victor Bazarov
    Replies:
    25
    Views:
    902
    E. Robert Tisdale
    Mar 23, 2005
  3. pereges
    Replies:
    7
    Views:
    441
    pereges
    Jun 1, 2008
  4. Ramon F Herrera
    Replies:
    8
    Views:
    486
    Pascal J. Bourguignon
    Sep 13, 2009
  5. Replies:
    2
    Views:
    150
Loading...

Share This Page