-1

I have made this code for a homework problem but I get warning and I don't understand why. And when I open the program it doesn't work.

This is the code:

#include <stdio.h>
#include <math.h>

int binarytodec(int array[]) {

  int i, result=0, n = sizeof(array)/sizeof(array[0]);

  for( i=0; i<n; i++ ) {

    if(array[i]==1) {
      result +=pow(2,i);
    } else if(array[i]==0) {
      result +=0;
    }
    else break;
  }
    return result;
}

int main() {

  int b[] = {1,1,1,0,0,1,0,0,12};

  binarytodec(b);

  return 0;
}

The warning is:

warning: sizeof on array function parameter will return size of 'int *' instead of 'int []' [-Wsizeof-array-argument] int i, result=0, n = sizeof(array)/sizeof(array[0]);

What is the problem?

Thank you

zeeks
  • 775
  • 2
  • 12
  • 30

1 Answers1

1

When you pass an array to a function in C, that array argument decays to a pointer and so the call to sizeof within the function you pass it to returns the size of the pointer itself, not the size in bytes of the array you pass.

bazz
  • 276
  • 1
  • 3
  • 10