-2

I am passing an array to a function whose size is determined by the user itself.

  #include<stdio.h>
void mapper(int a[], int size);
int main ()
{
    int n;
    printf("Please provide the number of inputs\n");
    scanf("%d",&n);
    int array[n];
    for(int i=0; i<n;i++)
    {
        scanf("%d",&array[i]);
    }
    mapper(array,n);
    return 0;

}
void mapper (int a[], int size)
{
  for (int i=0; i<size;i++)
  {
    for(int j=0; j<sizeof(a[i]);j++)
    {
        printf("#");
    }
    printf("\n");
  }
}

When I run this program I get an error called the comparison of signed integer to unsigned integer. Why is it so? Is it because I have passed a[] as an argument and am later using a[i]?

How do you pass an array as an argument to a function whose size is to be determined?

  • 2
    The [`sizeof` operator](https://en.cppreference.com/w/c/language/sizeof) returns `size_t`. `size_t` is a flavor of `unsigned` which has the size of pointers on your platform (e.g. on 64 bit size of `size_t` is 64 bit as well). – Scheff's Cat Jan 19 '19 at 10:34
  • 1
    Usually, comparing signed with unsigned yields a **warning**. Do you really get an error? What compiler settings have you used? – Scheff's Cat Jan 19 '19 at 10:36
  • @Sheff most companies force to treat warnings as errors as programmers tend to ignore them. – 0___________ Jan 19 '19 at 10:45
  • `a` is a one dimensional array. What do you intend `sizeof(a[i])` to return? The size of type `int` on your architecture (that's what it does in real life)? – Stefan Becker Jan 19 '19 at 10:59
  • Potential duplicate of [A warning - comparison between signed and unsigned integer expressions](https://stackoverflow.com/questions/3660901/a-warning-comparison-between-signed-and-unsigned-integer-expressions). – hnefatl Jan 19 '19 at 11:19
  • 1
    @Scheff: The C standard does not require that `size_t` be the size of pointers. Indeed, pointers of different types may have different sizes within a C implementation, in which case it is impossible for `size_t` to be the same size as all pointers. – Eric Postpischil Jan 19 '19 at 12:12
  • Why those downvotes? – alk Jan 19 '19 at 12:19
  • @alk because there is no mcve? (i didn't downvote) – Swordfish Jan 19 '19 at 12:41
  • @EricPostpischil You are always that accurate... Indeed, I found this in [std::size_t](https://en.cppreference.com/w/cpp/types/size_t) (C++) _On many platforms (an exception is systems with segmented addressing) std::size_t can safely store the value of any non-member pointer, in which case it is synonymous with std::uintptr_t._ but not such note for [size_t](https://en.cppreference.com/w/c/types/size_t) (C). I must admit I use C++ in daily work and follow C to keep in touch (and become aware to the peculiar differences). ;-) – Scheff's Cat Jan 19 '19 at 12:48
  • @P__J__ That's why I've asked for the compiler settings... – Scheff's Cat Jan 19 '19 at 12:51
  • @hnefatl The _Potential duplicate_ is for C++. Considering that both communities are often annoyed when C and C++ are not strictly separated as two languages, there better should be recommended one with [tag:c]... May be, this one: [SO: sizeof() operator in if-statement](https://stackoverflow.com/q/17293749/7478597) – Scheff's Cat Jan 19 '19 at 12:56
  • @Scheff: [Citing the C11 Standard](http://port70.net/~nsz/c/c11/n1570.html#7.19p2): "*size_t which is the unsigned integer type of the result of the sizeof operator*" for completeness. ;) – alk Jan 19 '19 at 12:59
  • @alk I believe I got it... ;-) – Scheff's Cat Jan 19 '19 at 13:01
  • 1
    @alk: My impression is a number of people vote down any question that they consider makes an obvious error or overlooks an obvious solution or course of action, where “obvious” is determined by the voter’s knowledge and not the asker’s. It is not a good behavior. It is a strange ability of humans to be knowledgeable about a technical matter and simultaneously oblivious to the work that got them there, unaware of their own previous ignorance, and unforgiving of others striding on the same path they previously traversed. – Eric Postpischil Jan 19 '19 at 13:11

1 Answers1

1

When I run this program I get an error called the comparison of signed integer to unsigned integer. Why is it so?

You get that error because in

for(int j=0; j<sizeof(a[i]);j++)

sizeof yields a value of type size_t which is an unsigned type and you compare it to an int.

Swordfish
  • 12,971
  • 3
  • 21
  • 43