2

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.

Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27
whale21
  • 51
  • 6
  • 6
    arrays decay to pointers so the function is actually `long maximum(long *arr)` making `sizeof(arr)` the size of a pointer (most likely 4 or 8 depending or the architecture). – bolov Jan 22 '15 at 06:59
  • can you paste the line with `printf()`? – Michał Šrajer Jan 22 '15 at 07:02
  • 2
    How do you declare a variable, which is assigned a value returned by the `maximum()` function? – CiaPan Jan 22 '15 at 07:09
  • What are the sizes of `int` and `long` on your system? – Keith Thompson Jan 22 '15 at 07:09
  • What is the definition of the other function you are using which takes input from this function `maximum()`? – Santosh A Jan 22 '15 at 07:11
  • The sizes of `int`s on my system are 10 digits, I'm not sure what the size of a `long` is but I think it has enough to suit my needs – whale21 Jan 22 '15 at 07:12
  • `sizeof(int)` was what was being asked. Not that it matters. Your `sizeof(arr)` is simply incorrect. That is the size of a pointer; not the array sequence length. You need to pass the length of the sequence in addition to the base pointer of the array's first element. The latter you have (`arr[]`), the former you need (likely a `size_t len` passed by the caller), thereby making your function `long maximum(long arr[], size_t len)` – WhozCraig Jan 22 '15 at 07:14
  • The `printf()` line is `printf("The maximum is %Ld. \n", max);` When I do the exact same outside of the function using the long value I obtained from the `maximum()` function, it acts like an int though. – whale21 Jan 22 '15 at 07:16
  • Depending on your platform, `int` and `long` may in-fact be identical. The standard doesn't mandate `long` must hold a larger magnitude; only that it cannot hold a *smaller* one. `printf("%zu %zu\n", sizeof(int), sizeof(long));` will confirm if that is the case. Regardless, your function is not using the correct length of its sequence. See prior comment. – WhozCraig Jan 22 '15 at 07:18
  • I did `printf("%zu %zu\n", sizeof(int), sizeof(long));` which gave me 4 and 8 , and I changed the function like you said WhozCraig. I see your logic and why I was wrong to start with `sizeof()` in the first place, however the function is still giving me an int with only 10 digits. – whale21 Jan 22 '15 at 07:26
  • And your validating that with what *exact* output statement and what *exact* receiving variable? This sort of info needs to be in your question, btw. – WhozCraig Jan 22 '15 at 07:35
  • 1
    The error is most likely in how you use the function's return value. For example, you might store it intermediately in a narrower `int` type. Care to show us the code that calls `maximum` and how the return value is used? – M Oehm Jan 22 '15 at 07:37
  • Try to use `long long` instead of `long`. Also, fix the problem with `sizeof(arr)`. – Marian Jan 22 '15 at 07:37
  • See example with 64bit longs [**here**](http://coliru.stacked-crooked.com/a/9a46eeed8f56d75a) – WhozCraig Jan 22 '15 at 07:40
  • possible duplicate of [Why does a C-Array have a wrong sizeof() value when it's passed to a function?](http://stackoverflow.com/questions/2950332/why-does-a-c-array-have-a-wrong-sizeof-value-when-its-passed-to-a-function) – phuclv Jan 22 '15 at 09:32
  • in binary people typically don't care how many decimal digits it can store because even a 32-bit int simply can't store 4,444,444,444 although it does have 10 digits. When talking about size, it's the size in bytes or bits and in C sizeof returns the size in bytes – phuclv Jan 22 '15 at 09:40
  • to print `long` and `long long`, use `%ld` and `%lld` respectively, not `%Ld` – phuclv Jan 22 '15 at 09:47

4 Answers4

2

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

malat
  • 12,152
  • 13
  • 89
  • 158
1

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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

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.

barak manos
  • 29,648
  • 10
  • 62
  • 114
0

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!