Mohamed Azzam
Mohamed Azzam

Reputation: 69

using the same pointer for two functions in c

I am using the same pointer for two functions,

#include <stdio.h>

/* function to generate and return random numbers */
int * getRandom( ) {

    static int  r[10];
    int i;
    for ( i = 0; i < 10; ++i) {
        r[i]=i*2*i*9183784;
        printf( "r1[%d] = %d\n", i, r[i]);
    }
    return r;
}

int * geti( *p) {
    static int  r[10];
    int I;

        r[i] = p[i]*i;
    printf( "r2[%d] = %d\n", i, r[i]);
    }

    return r;
}

int main () {
    int *p;
    int i;

    p = getRandom();
    for ( i = 0; i < 10; i++ ) {
        printf( "*(p + %d) 1: %d\n", i, *(p + i));
    }

    p = geti(p);

    for ( i = 0; i < 10; i++ ) {
        printf( "*(p + %d) 2: %d\n", i, *(p + i));
    }

    return 0;
}

It becomes the following output:

r1[0] = 0
r1[1] = 18367568
r1[2] = 73470272
r1[3] = 165308112
r1[4] = 293881088
r1[5] = 459189200
r1[6] = 661232448
r1[7] = 900010832
r1[8] = 1175524352
r1[9] = 1487773008
*(p + 0) 1: 0
*(p + 1) 1: 18367568
*(p + 2) 1: 73470272
*(p + 3) 1: 165308112
*(p + 4) 1: 293881088
*(p + 5) 1: 459189200
*(p + 6) 1: 661232448
*(p + 7) 1: 900010832
*(p + 8) 1: 1175524352
*(p + 9) 1: 1487773008
r2[0] = 0
r2[1] = 18367568
r2[2] = 146940544
r2[3] = 495924336
r2[4] = 1175524352
r2[5] = -1999021296
r2[6] = -327572608
r2[7] = 2005108528
r2[8] = 814260224
r2[9] = 505055184
*(p + 0) 2: 0
*(p + 1) 2: 18367568
*(p + 2) 2: 146940544
*(p + 3) 2: 495924336
*(p + 4) 2: 1175524352
*(p + 5) 2: -1999021296
*(p + 6) 2: -327572608
*(p + 7) 2: 2005108528
*(p + 8) 2: 814260224
*(p + 9) 2: 505055184

these can not be the numbers mathematically for instance in *(p + 5) 2. Where is the problem? Thank you in advance.

Upvotes: 0

Views: 154

Answers (2)

sg7
sg7

Reputation: 6298

The primary problem is the integer overflow. The calculated results do not fit into the int value. The 64 bit long long would allow proper calculations.

This is the modified program with test results (gcc 7.2.0 on 64 bit Mac):

#include <stdio.h>

/* function to generate and return random numbers */
long long * getRandom(long long r[])
{
    //static int r[10];
    int i;

    for ( i = 0; i < 10; ++i) {
        r[i]=i*2*i*9183784;
        printf( "r1[%d] = %lld\n", i, r[i]);
    }
    return r;
}

long long * geti(long long *p) { // declare the type
    //static int  r[10];
    int i; // i

    for ( i = 0; i < 10; ++i) {
       p[i] = p[i]*i;
       printf( "r2[%d] = %lld\n", i, p[i]);
    }

    return p;
}

int main () {
    long long *p;
    int i;
    long long  r[10];

    p = getRandom(r);
    for ( i = 0; i < 10; i++ ) {
        printf( "*(p + %d) 1: %lld\n", i, *(p + i));
    }

    p = geti(p);

    for ( i = 0; i < 10; i++ ) {
        printf( "*(p + %d) 2: %lld\n", i, *(p + i));
    }

    return 0;
}

Output:

r1[0] = 0
r1[1] = 18367568
r1[2] = 73470272
r1[3] = 165308112
r1[4] = 293881088
r1[5] = 459189200
r1[6] = 661232448
r1[7] = 900010832
r1[8] = 1175524352
r1[9] = 1487773008
*(p + 0) 1: 0
*(p + 1) 1: 18367568
*(p + 2) 1: 73470272
*(p + 3) 1: 165308112
*(p + 4) 1: 293881088
*(p + 5) 1: 459189200
*(p + 6) 1: 661232448
*(p + 7) 1: 900010832
*(p + 8) 1: 1175524352
*(p + 9) 1: 1487773008
r2[0] = 0
r2[1] = 18367568
r2[2] = 146940544
r2[3] = 495924336
r2[4] = 1175524352
r2[5] = 2295946000
r2[6] = 3967394688
r2[7] = 6300075824
r2[8] = 9404194816
r2[9] = 13389957072
*(p + 0) 2: 0
*(p + 1) 2: 18367568
*(p + 2) 2: 146940544
*(p + 3) 2: 495924336
*(p + 4) 2: 1175524352
*(p + 5) 2: 2295946000
*(p + 6) 2: 3967394688
*(p + 7) 2: 6300075824
*(p + 8) 2: 9404194816
*(p + 9) 2: 13389957072  

Upvotes: 0

Tom Karzes
Tom Karzes

Reputation: 24052

You're experiencing integer overflow. The values you're trying to calculate don't fit into 32-bit signed integers. For instance, r2[5] is meant to be 459189200 * 5, which is 2295946000. But the largest positive value that can be represented in a signed 32-bit integer is 2**31 - 1, which is 2147483647. So it comes out as 2295946000 - 2**32 which is -1999021296.

You could represent the desired value in a 32-bit unsigned integer, but that would only get you one value further.

If your compiler supports a 64-bit long long data type, that would solve your problem. Beyond that, you could either use floating point (but you would eventually lose precision in the low-order bits), or switch to an extended-precision integer representation.

Upvotes: 1

Related Questions