Store int* array

Discussion in 'C Programming' started by MN, Aug 27, 2008.

  1. MN

    MN Guest

    How to store the function's result into variable in this case?
    My code is like this::

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

    extern int* var1;

    int* function(int in1, int in2);

    int* var1 =NULL;
    int* view_var1 =NULL;

    int main ()
    {
    int i = 0;
    //extern int* view_var1;
    int in1 = 5;
    int in2 = 10;

    /* if (view_var1 != NULL) */
    /* free(view_var1); */

    /* view_var1 = (int*)malloc (sizeof(int*)* in2); */

    /* view_var1 = function(in1, in2); */

    function(in1, in2);

    for (i = 0; i < in2; i++)
    {
    if (i == 0)
    //printf("- %d\n", &view_var1);
    printf("- %d\n",var1);

    else
    //printf("%d %d\n", i-1, &view_var1);
    printf("%d %d\n", i-1, var1);
    }
    return 0;

    }

    int* function(int in1, int in2)
    {
    int i = 0;
    if (var1 != NULL)
    free(var1);
    var1 = (int*)malloc (sizeof(int*)* in2);


    for (i = 0; i < in2; i++)
    {
    if (i == 0)
    var1 = 0;
    else
    {
    if (i == 1)
    var1= 1;
    else
    var1 = var1[i-1]*2+in1;
    }
    }
    return (var1[in2]);
    }


    The right result must be :
    - 0
    0 1
    1 7
    2 19
    3 43
    4 91
    5 187
    6 379
    7 763
    8 1531

    But if I activate lines that use view_var1, I get a result (which is
    wrong) like this:
    - 0
    0 4
    1 8
    2 12
    3 16
    4 20
    5 24
    6 28
    7 32
    8 36

    Also how to do in order to avoid these warning?
    test.c:26: warning: return makes pointer from integer without a cast
    test.c:56: warning: format ‘%d’ expects type ‘int’, but argument 2 has
    type ‘int *’
    MN, Aug 27, 2008
    #1
    1. Advertising

  2. On Wed, 27 Aug 2008 00:07:35 -0700, MN wrote:
    [snip]
    >
    > for (i = 0; i < in2; i++)
    > {
    > if (i == 0)


    {

    > //printf("- %d\n", &view_var1);
    > printf("- %d\n",var1);


    }
    >
    > else


    {

    > //printf("%d %d\n", i-1, &view_var1);
    > printf("%d %d\n", i-1, var1);


    }

    > }
    > return 0;
    >
    > }
    >
    > int* function(int in1, int in2)
    > {
    > int i = 0;
    > if (var1 != NULL)
    > free(var1);
    > var1 = (int*)malloc (sizeof(int*)* in2);
    >
    >
    > for (i = 0; i < in2; i++)
    > {
    > if (i == 0)
    > var1 = 0;
    > else
    > {
    > if (i == 1)
    > var1= 1;
    > else
    > var1 = var1[i-1]*2+in1;
    > }
    > }
    > return (var1[in2]);
    > }
    >


    The return type is int* but here you return int. The correct
    way would be to write:

    return var1+in2;

    But then again, that just returns a pointer pointing beyond the
    allocated array. I'm guessing you actually want the beginning
    of the array:

    return var1;

    --
    OU
    Obnoxious User, Aug 27, 2008
    #2
    1. Advertising

  3. MN

    MN Guest

    Thanks for your help.
    MN, Aug 27, 2008
    #3
  4. MN <> wrote:
    > How to store the function's result into variable in this case?
    > My code is like this::


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


    > extern int* var1;


    Why do you declare it as extern if you define it just
    two lines later?

    > int* function(int in1, int in2);


    > int* var1 =NULL;
    > int* view_var1 =NULL;


    > int main ()
    > {
    > int i = 0;
    > //extern int* view_var1;
    > int in1 = 5;
    > int in2 = 10;


    > /* if (view_var1 != NULL) */
    > /* free(view_var1); */


    > /* view_var1 = (int*)malloc (sizeof(int*)* in2); */


    'view_var' seems to be supposed to be a pointer to int, at
    least that's how you defined it. Then this line is wrong,
    since you allocate room for 'in2' pointers to int, and
    not for 'in2' ints. You also shouldn't cast the return
    value of malloc() - the conversion from void pointer to
    the left hand side type happens all by itself, but the
    cast will keep the compiler from warning you when you
    forget to include <stdlib.h>. So make that line

    view_var = malloc( in2 * sizeof( int ) );

    or, perhaps better

    view_var = malloc( in2 * sizeof *view_var );

    You may have got away with your faulty version since on
    a number of systems an int and a pointer to int take
    the same amount of room, but that's nothing you should
    rely on.

    > /* view_var1 = function(in1, in2); */


    > function(in1, in2);


    > for (i = 0; i < in2; i++)
    > {
    > if (i == 0)
    > //printf("- %d\n", &view_var1);


    '&view_var1' is a pointer to the element with index 'i'
    of the 'view_var' array and not an integer. Simply drop
    the '&'.

    > printf("- %d\n",var1);


    > else
    > //printf("%d %d\n", i-1, &view_var1);


    Same her.

    > printf("%d %d\n", i-1, var1);
    > }
    > return 0;


    > }


    > int* function(int in1, int in2)
    > {
    > int i = 0;
    > if (var1 != NULL)
    > free(var1);
    > var1 = (int*)malloc (sizeof(int*)* in2);


    Here's the same problem as above, you need

    var1 = malloc( in2 * sizeof *var1 );

    > for (i = 0; i < in2; i++)
    > {
    > if (i == 0)
    > var1 = 0;
    > else
    > {
    > if (i == 1)
    > var1= 1;
    > else
    > var1 = var1[ i - 1 ] * 2 + in1;
    > }
    > }
    > return (var1[in2]);


    This will return an int, not, as the function is defined to,
    a pointer to int. I guess you wanted to do

    return var1;

    > }

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Aug 27, 2008
    #4
    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. S300
    Replies:
    4
    Views:
    21,963
    Roedy Green
    Aug 19, 2003
  2. Schnoffos
    Replies:
    2
    Views:
    1,194
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,608
    Old Wolf
    Jan 20, 2004
  4. arun
    Replies:
    8
    Views:
    429
    Dave Thompson
    Jul 31, 2006
  5. Alain Spineux
    Replies:
    6
    Views:
    618
    Eric Sosman
    May 17, 2011
Loading...

Share This Page