whale21
whale21

Reputation: 51

C when returning a long the program acts like I'm returning an int

So, in my function when I try to return a long, it acts like I'm returning an int. This mostly comes into play when I'm trying to return a long that has more than 10 digits, it will return an int limited to 10 digits.

For example my code is trying to find the greatest number within an array of longs so,

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

long maximum(long arr[])
{
    long i, max;

    for(i = 0; i < sizeof(arr); i++)
    {
        //This sets the initial number to the maximum
        if(i == 0)
        {
            max = arr[0]
        }

        else if(arr[i] > max)
        {
            max = arr[i];
        }
    }

    return max;
}

When I do a printf in the array right before the return it prints out the correct long with all its digits, but when I return it and try to use this function to return the long in another function then it will only return an int limited to 10 digits. I feel like there might be something obvious I'm missing but I don't know what it is.

Upvotes: 2

Views: 1224

Answers (4)

barak manos
barak manos

Reputation: 30126

For the sizeof operator to give the actual size of the array, you need to:

  1. Allocate the array statically
  2. Refer to the array within its scope of declaration

For example:

void func()
{
    int arr[10];
    int total_size = sizeof(arr);
    int num_of_elements = sizeof(arr)/sizeof(*arr);
    ...
}

An example of when the array is not statically allocated:

void func()
{
    int* arr = malloc(sizeof(int)*10);
    int total_size = sizeof(arr); // will give you the size of 'int*'
    ...
}

An example of when the array is not within its scope of declaration:

void func(int arr[]) // same as 'int* arr'
{
    int total_size = sizeof(arr); // will give you the size of 'int*'
    ...
}

Please note that for your specific purpose, you actually need the number of elements in the array and not its total size (as shown in the first example above).

In addition to all of that, sizeof(long) is not necessarily larger than sizeof(int) on every platform, so you might need to use long long instead.

Upvotes: 0

Sebastian Kaupper
Sebastian Kaupper

Reputation: 163

The clou is that a long has a size of 32bit (−2147483647,+2147483647) that means you aren't able to process larger numbers.

Try to use 'long long int' instead!

Upvotes: 0

gnasher729
gnasher729

Reputation: 52530

You are not posting the code that processes the return value. I assume that's because you're sure that's not where the bug is. So it's quite obvious that this is where the bug is.

Strategy for finding bugs: You start with the assumption that unknown evil forces keep your program from working and start fluffing around (that's your current stage). In that stage, you're not going to find the problem. The second stage is that you realise that you made a mistake and your job is to find where the mistake is. Emphasis is that you made the mistake. Your code is not correct. So look at it under the assumption that your code is wrong. Look at it under the assumption that code where you are 100% sure that it is correct is actually wrong.

If that strategy doesn't work, imagine that the code isn't written by you and therefore bug free, but written by your worst enemy who will finally get fired by your boss if you can find the bug in his code. That will help.

Upvotes: 1

malat
malat

Reputation: 12490

My guess is that you did not explicitely tell the compiler that the signature would return long from your other instanciation unit. Just state somewhere in your other code that the function really is long maximum(long arr[]). Using gcc, you can even use -Wimplicit-function-declaration

Upvotes: 2

Related Questions