Home > OS >  Bunch of warnings but my program works fine
Bunch of warnings but my program works fine

Time:09-05

warning: assignment makes pointer from integer without a cast [-Wint-conversion]
warning: return makes integer from pointer without a cast [-Wint-conversion]
    #include<stdio.h>  

    long int jumlah(long int a,long int b, long int * sum){
        sum = (a b);
        return (sum);
    }

    int main(){
        long int a,b,sum;
        scanf("%d %d",&a,&b);
        sum = jumlah(a,b,&sum);
        printf("%d\n",sum);
        return 0;
    }

My program works fine but everything inside the function gets a warning and I want to know why

CodePudding user response:

In the jumlah function you have to dereference the sum pointer to access the variable it points to. The format specifier for long ints is "%ld".

#include <stdio.h>

long int jumlah(long int a, long int b, long int *sum)
{
    *sum = a b;
    return *sum;
}

int main(void)
{
    long int a, b, sum;
    scanf("%ld %ld", &a, &b);
    sum = jumlah(a, b,&sum);
    printf("%ld\n", sum);
    return 0;
}

CodePudding user response:

long int jumlah(long int a,long int b, long int * sum){
    sum = (a b);
    return (sum);
}

sum is a pointer to long int. You can only assign values of type pointer to long int to it. (a b) does return a value of type long int, in order to assign the result to sum, you first have to dereference it, like this: *sum = (a b);

The function return type is long int, but again sum is a pointer type, it makes a huge difference if you return either sum or *sum, one will be an address, the other the value stored at that address.

Either change the return type of that function to long int* and return sum (only if you want to return the address instead the value - but it does not seem like that) or return the value by dereferencing *sum from the function.

CodePudding user response:

As I wrote in the comment under you question, you have mistakenly used the pointer sum as a long int storage type.

This is needlessly complicated. Let's simplify it. (Good code is sharp.)

long int jumlah( long int a, long int b ) {
    return a   b;
}

int main(){
    long int a, b;

    // make use of return values from system (input) functions
    if( scanf( "%d %d", &a, &b ) == 2 )
        printf( "%d\n", jumlah( a, b ) );
    else
        printf( "Input error\n" );

    return 0;
}

CodePudding user response:

Your program may work as expected, but you’re stuffing a long integer into a pointer to long (sum), not AT the pointer to long. Wrong type, but it just happens to fit. Basically, you’re using the sum parameter as a locally defined variable, similar to this:

long int jumlah(long int a,long int b){
long int sum;

sum = a   b;
return (sum);

}

In the main(), since you store the return from jumlah() back into sum, passing &sum doesn’t do much instead of providing that ersatz local variable. If you wanted to store the result at sum (in addition to returning it), jumlah() should be:

long int jumlah(long int a,long int b, long int * sum){
*sum = a   b;
return (*sum);

}

Always treat compiler warnings as errors and fix ‘em.

  •  Tags:  
  • c
  • Related