returning function pointer

Discussion in 'C Programming' started by Pradyut Bhattacharya, Jun 26, 2007.

  1. Hi,
    in this program i cannot retrive the values from the pointer j....
    The code :-

    --------------------------------------------------------------------------------------------------------------

    #include <stdio.h>

    void func(int [], int);
    int*func2();

    void main()
    {
    int a[] = {3,2,4};

    int*j[8];
    int i;

    func(a, 2);

    *j = func2();
    printf("\nTraversing second list\n");

    printf("\n%d", *j);

    }

    void func(int a[], int size)
    {
    int i=0;
    for (i=0; i<=size; i++)
    printf("%d\n", a);
    }

    int*func2()
    {

    int b[] = {112,3,4,5,6,7,9,4,10};

    return *b;
    }

    -------------------------------------------------------------------------------------------------------------

    Any help

    Thanks
    Pradyut
    http://pradyut.tk
    India
    Pradyut Bhattacharya, Jun 26, 2007
    #1
    1. Advertising

  2. Pradyut Bhattacharya

    Default User Guest

    Pradyut Bhattacharya wrote:

    > Hi,
    > in this program i cannot retrive the values from the pointer j....
    > The code :-
    >
    > ----------------------------------------------------------------------
    > ----------------------------------------
    >
    > #include <stdio.h>
    >
    > void func(int [], int);
    > int*func2();
    >
    > void main()


    main() returns int ALWAYS.

    > {
    > int a[] = {3,2,4};
    >
    > int*j[8];
    > int i;
    >
    > func(a, 2);


    The number of elements in your array is three. Why pass two?

    >
    > *j = func2();
    > printf("\nTraversing second list\n");
    >
    > printf("\n%d", *j);


    You're obviously not traversing, but accessing the first element.

    >
    > }
    >
    > void func(int a[], int size)
    > {
    > int i=0;
    > for (i=0; i<=size; i++)


    Change that to i < size

    > printf("%d\n", a);
    > }
    >
    > int*func2()
    > {
    >
    > int b[] = {112,3,4,5,6,7,9,4,10};
    >
    > return *b;


    Yikes. You pass back an int from a function that's supposed to be
    returning an int *. Presumbably you meant to type:

    return b;

    However, that's not right either. b is a local array, you can NOT use
    it outside of its scope, which happens to be the function func2().

    You'll either have to dynamically allocate an array and populate it, or
    make b static.


    > Any help


    Turn up the warning level on your compiler.




    Brian
    Default User, Jun 26, 2007
    #2
    1. Advertising

  3. Pradyut Bhattacharya

    pete Guest

    Default User wrote:
    >
    > Pradyut Bhattacharya wrote:
    >
    > > Hi,
    > > in this program i cannot retrive the values from the pointer j....
    > > The code :-
    > >
    > > ----------------------------------------------------------------------
    > > ----------------------------------------
    > >
    > > #include <stdio.h>
    > >
    > > void func(int [], int);
    > > int*func2();
    > >
    > > void main()

    >
    > main() returns int ALWAYS.
    >
    > > {
    > > int a[] = {3,2,4};
    > >
    > > int*j[8];
    > > int i;
    > >
    > > func(a, 2);

    >
    > The number of elements in your array is three. Why pass two?
    >
    > >
    > > *j = func2();
    > > printf("\nTraversing second list\n");
    > >
    > > printf("\n%d", *j);

    >
    > You're obviously not traversing, but accessing the first element.
    >
    > >
    > > }
    > >
    > > void func(int a[], int size)
    > > {
    > > int i=0;
    > > for (i=0; i<=size; i++)

    >
    > Change that to i < size
    >
    > > printf("%d\n", a);
    > > }
    > >
    > > int*func2()
    > > {
    > >
    > > int b[] = {112,3,4,5,6,7,9,4,10};
    > >
    > > return *b;

    >
    > Yikes. You pass back an int from a function that's supposed to be
    > returning an int *. Presumbably you meant to type:
    >
    > return b;
    >
    > However, that's not right either. b is a local array, you can NOT use
    > it outside of its scope, which happens to be the function func2().
    >
    > You'll either have to dynamically allocate an array
    > and populate it, or make b static.
    >
    > > Any help

    >
    > Turn up the warning level on your compiler.



    /* BEGIN new.c */

    #include <stdio.h>

    #define NINE 9

    void func(int a[], int size);
    int *func2(void);

    int main(void)
    {
    int a[] = {3,2,4};
    int *(*j)(void) = func2;

    func(a, sizeof a / sizeof *a);
    printf("\nTraversing second list\n");
    func(j(), NINE);
    return 0;
    }

    void func(int a[], int size)
    {
    int i;

    for (i = 0; size > i; i++) {
    printf("%d\n", a);
    }
    }

    int *func2(void)
    {
    static int b[NINE] = {112,3,4,5,6,7,9,4,10};

    return b;
    }

    /* END new.c */


    --
    pete
    pete, Jun 26, 2007
    #3
  4. Pradyut Bhattacharya

    Default User Guest

    Default User wrote:

    > Pradyut Bhattacharya wrote:
    >
    > > Hi,
    > > in this program i cannot retrive the values from the pointer
    > > j.... The code :-
    > >
    > > --------------------------------------------------------------------
    > > -- ----------------------------------------
    > >
    > > #include <stdio.h>
    > >
    > > void func(int [], int);
    > > int*func2();
    > >
    > > void main()

    >
    > main() returns int ALWAYS.
    >
    > > {
    > > int a[] = {3,2,4};
    > >
    > > int*j[8];
    > > int i;
    > >
    > > func(a, 2);

    >
    > The number of elements in your array is three. Why pass two?
    >
    > >
    > > *j = func2();

    >
    > > printf("\nTraversing second list\n");
    > >
    > > printf("\n%d", *j);



    Oops, I just noticed that you are printing an int pointer as if it were
    an int. Yet another mistake. j is an array of int pointers. Possibly
    you wanted it to be just an int pointer, which would make the printf ok.




    Brian
    Default User, Jun 26, 2007
    #4
  5. Pradyut Bhattacharya

    Default User Guest

    pete wrote:

    > void func(int a[], int size);
    > int *func2(void);
    >
    > int main(void)
    > {
    > int a[] = {3,2,4};
    > int *(*j)(void) = func2;
    >
    > func(a, sizeof a / sizeof *a);
    > printf("\nTraversing second list\n");
    > func(j(), NINE);
    > return 0;
    > }


    Hmmm, points for working part of the subject into the solution, but the
    OP said RETURNING function pointer.




    Brian
    Default User, Jun 26, 2007
    #5
  6. Pradyut Bhattacharya

    pete Guest

    Default User wrote:
    >
    > pete wrote:
    >
    > > void func(int a[], int size);
    > > int *func2(void);
    > >
    > > int main(void)
    > > {
    > > int a[] = {3,2,4};
    > > int *(*j)(void) = func2;
    > >
    > > func(a, sizeof a / sizeof *a);
    > > printf("\nTraversing second list\n");
    > > func(j(), NINE);
    > > return 0;
    > > }

    >
    > Hmmm, points for working part of the subject into the solution,
    > but the OP said RETURNING function pointer.


    I think I did better than that.

    OP said "cannot retrive the values from the pointer j"
    I made j into a function pointer.
    I used the return value from a function call made through j
    to access nine values.
    And that's how I got OP's original line of code:
    printf("\nTraversing second list\n");
    to say what it means.

    --
    pete
    pete, Jun 26, 2007
    #6
  7. On Wed, 27 Jun 2007 02:17:16 +0530, in comp.lang.c , "Pradyut
    Bhattacharya" <> wrote:

    >int*func2();


    funct returns a pointer to an int...

    > int*j[8];


    j is an array of pointers to ints. I don't think you wanted that...

    > *j = func2();


    you set the first pointer in j to whatever func2 returns.

    > int b[] = {112,3,4,5,6,7,9,4,10};


    b is a local array which is discarded when func2 returns.

    > return *b;


    *b is the value of the first element ie 112,

    But your function is expecting a /pointer/ to an int. You should
    hopefully get a warning here. If not, turn up warning levels. You have
    set j[0] to point to the 112th byte of your computer's memory, which
    is probably protected ROM space or something...


    I presume you want return an array from a function. You can't do that
    (easily). Pass the array as an argument.
    --
    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
    Mark McIntyre, Jun 26, 2007
    #7
  8. Pradyut Bhattacharya

    Default User Guest

    pete wrote:

    > Default User wrote:
    > >
    > > pete wrote:
    > >
    > > > void func(int a[], int size);
    > > > int *func2(void);
    > > >
    > > > int main(void)
    > > > {
    > > > int a[] = {3,2,4};
    > > > int *(*j)(void) = func2;
    > > >
    > > > func(a, sizeof a / sizeof *a);
    > > > printf("\nTraversing second list\n");
    > > > func(j(), NINE);
    > > > return 0;
    > > > }

    > >
    > > Hmmm, points for working part of the subject into the solution,
    > > but the OP said RETURNING function pointer.

    >
    > I think I did better than that.
    >
    > OP said "cannot retrive the values from the pointer j"
    > I made j into a function pointer.
    > I used the return value from a function call made through j
    > to access nine values.
    > And that's how I got OP's original line of code:
    > printf("\nTraversing second list\n");
    > to say what it means.


    Ok, that's not bad.



    Brian
    Default User, Jun 26, 2007
    #8
  9. Pradyut Bhattacharya wrote:
    > Hi,
    > in this program i cannot retrive the values from the pointer j....


    We won't bother discussing absurdities of
    > void main()

    If you don't know better by now, since you have done as any civilized
    person would and followed the newsgroup and checked the FAQ before
    posting, you never will.

    The answer to your stated problem is simple. There is no "the pointer j".
    You have declared j with
    > int*j[8];

    So j is an array[8] of pointers to int.
    Martin Ambuhl, Jun 26, 2007
    #9
  10. Pradyut Bhattacharya

    !truth Guest

    On 6 27 , 4 47 , "Pradyut Bhattacharya" <> wrote:
    > Hi,
    > in this program i cannot retrive the values from the pointer j....
    > The code :-
    >
    > ---------------------------------------------------------------------------­-----------------------------------
    >
    > #include <stdio.h>
    >
    > void func(int [], int);
    > int*func2();
    >
    > void main()
    > {
    > int a[] = {3,2,4};
    >
    > int*j[8];
    > int i;
    >
    > func(a, 2);
    >
    > *j = func2();
    > printf("\nTraversing second list\n");
    >
    > printf("\n%d", *j);
    >
    > }
    >
    > void func(int a[], int size)
    > {
    > int i=0;
    > for (i=0; i<=size; i++)
    > printf("%d\n", a);
    >
    > }
    >
    > int*func2()
    > {
    >
    > int b[] = {112,3,4,5,6,7,9,4,10};
    >
    > return *b;
    >
    > }
    >
    > ---------------------------------------------------------------------------­----------------------------------
    >
    > Any help
    >
    > Thanks
    > Pradyuthttp://pradyut.tk
    > India


    It not wise to return a pointer which points to the static.
    But if you still want to do so, try to remove the line:
    printf("\nTraversing second list\n");
    !truth, Jun 27, 2007
    #10
  11. On Jun 26, 9:36 pm, !truth <> wrote:
    > On 6 27 , 4 47 , "Pradyut Bhattacharya" > > in this program i cannot retrive the values from the pointer j....
    > > The code :-

    >
    > > ---------------------------------------------------------------------------­­-----------------------------------

    >
    > > #include <stdio.h>

    >
    > > void func(int [], int);
    > > int*func2();

    >
    > > void main()
    > > {
    > > int a[] = {3,2,4};

    >
    > > int*j[8];
    > > int i;

    >
    > > func(a, 2);

    >
    > > *j = func2();
    > > printf("\nTraversing second list\n");

    >
    > > printf("\n%d", *j);

    >
    > > }

    >
    > > void func(int a[], int size)
    > > {
    > > int i=0;
    > > for (i=0; i<=size; i++)
    > > printf("%d\n", a);

    >
    > > }

    >
    > > int*func2()
    > > {

    >
    > > int b[] = {112,3,4,5,6,7,9,4,10};

    >
    > > return *b;

    >
    > > }

    >
    > > ---------------------------------------------------------------------------­­----------------------------------

    >
    > > Any help

    >
    > It not wise to return a pointer which points to the static.


    Whyever not? What does this have to do with the question anyway?

    > But if you still want to do so, try to remove the line:
    > printf("\nTraversing second list\n");


    Why? How is that relevant to anything?
    J. J. Farrell, Jun 28, 2007
    #11
    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. glen stark
    Replies:
    2
    Views:
    688
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,026
    Fraser Ross
    Aug 14, 2004
  3. Protoman
    Replies:
    14
    Views:
    557
    Protoman
    Dec 11, 2005
  4. Mark Piffer
    Replies:
    9
    Views:
    895
    luserXtrog
    May 15, 2009
  5. pembed2012
    Replies:
    1
    Views:
    410
    James Kuyper
    Feb 27, 2012
Loading...

Share This Page