-1

I'm new in C and I'm trying to make an algorithm with bubble sort. It's not so hard but I don't understand why my sizeof() of a table change between my main function and another function.

Indeed, I have a table with 5 elements, so my sizeof(), because for me int is equal to four bytes, is equal to 20. However, when I put my table into a function, with a pointer, my sizeof gets 8. In my opinion, it's the same table so I don't understand why.

Here my code :

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

void bubble_sort(int* table) {
    printf("Size of table in bubble sort function : %d\n", sizeof(table));
    int permutation = 1;
    int number = 0;
    while(permutation != 0) {
        permutation = 0;
        for (int i = 0; i < sizeof(table)/sizeof(int); i++) {
            if (table[i] > table[i+1]) {
                    number = table[i];
                    table[i] = table[i+1];
                    table[i+1] = number;
                    permutation++;
            }
        }
    }
}

int main() {
    int table[5] = {3, 20, 12, 4, 21};
    printf("Size of table in main function : %d\n", sizeof(table));
    bubble_sort(table);
}

And I got this :

Size of table in main function : 20
Size of table in bubble sort function : 8
  • 1
    One `table` is an array, the other `table` is a pointer. Totally different. You have to pass the size as a separate parameter to the function. – BoP Jan 20 '23 at 06:38
  • In main, you are taking the size of the entire array. Within bubble_sort, you are taking the size of a pointer. You need to pass an integer for the size to the bubble sort function. – robthebloke Jan 20 '23 at 06:38
  • PS: If/when you fix this `sizeof` problem, the code will be stepping out of bounds in the loop (aka "Undefined Behaviour")... Think carefully about what indices `i+1` will produce... – Fe2O3 Jan 20 '23 at 06:45
  • @Fe2O3 Yeah I changed my code for my post but at the beginning I added -1 to "sizeof(table)/sizeof(int)" so no worries about that but thanks for your help :) – NoCodeGamer Jan 20 '23 at 06:52
  • For future reference, shrinking code to a MRE may be difficult in cases where the OP doesn't know what causes a problem... In this case, you've posted far more code than was necessary to demonstrate what perplexed you, but say that you 'tweaked' one line resulting in my previous comment... Please take care when posting. It helps no-one to diagnose problems that do not exist in your actual code. – Fe2O3 Jan 20 '23 at 06:57

1 Answers1

3

An array decays to a pointer in function parameters.

sizeof (table) in the function returns the size of the pointer, not the size of the array it points to. You can't determine the size of an array from a decayed pointer value of the array.

There might be a trick or two, but I do not know of any standard way to do so. You could instead:

  • Pass in the size as a separate parameter.
  • Pass a pointer to a struct containing both the array and it's size.
Harith
  • 4,663
  • 1
  • 5
  • 20