0

I have a problem. I am getting an error:

 sizeof on array function parameter will return size of 'int *' 

even after I use sizeof(array)\sizeof(array[0]). Could you help me please?

bool search(int value, int values[], int n)
{   int a=sizeof(values)\sizeof(values[0]);
    for(int i=0; i<a; i++)

        if(values[i]==value)
        return true;
        else
        return false;
}
Kateryna
  • 15
  • 7
  • What's with the **bold** text? – Sourav Ghosh May 20 '16 at 20:06
  • 1
    Please show the code examples that demonstrate the question. – Weather Vane May 20 '16 at 20:06
  • Do not use an unnecessary formatting in the question text. But use a proper formatting for code and quotation. Also provide the code and the exact error message. Also make title clear and concise. – Eugene Sh. May 20 '16 at 20:06
  • Because the function does not know the `sizeof` the array it was passed. It only knows the `sizeof` the pointer it was converted to. – Weather Vane May 20 '16 at 20:08
  • So, sizeof(array)\sizeof(array[0]) is not a solution if I need to find out the length of the array? – Kateryna May 20 '16 at 20:20
  • 2
    `values` is not an array. It's a pointer. – Keith Thompson May 20 '16 at 20:22
  • You do know that `\\`` isn't a C operator, right? The divide operator is the same as it is in almost any computer language, i.e. `/` just as you'd expect. In any case, you have to explicitly pass in the size of the array. Otherwise it doesn't have the information. – Tom Karzes May 20 '16 at 20:23
  • Yes, I posted it it with "\" by mistake. Sorry. – Kateryna May 20 '16 at 20:25
  • @Kateryna please follow the links. The original array definition is not withing the scope of the function it was passed to. The function knows *nothing* about it except its type. The function gets a pointer: that's all. – Weather Vane May 20 '16 at 20:26
  • Thank you, now it becomes more clear! – Kateryna May 20 '16 at 20:28
  • Suggested reading: section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson May 20 '16 at 20:39
  • Since arrays decay immediately into pointers, an array is never actually passed to a function. You can pretend that a function receives an array as a parameter, and illustrate it by declaring the corresponding parameter as an array: void f(char a[]) { ... } Interpreted literally, this declaration would have no use, so the compiler turns around and pretends that you'd written a pointer declaration, since that's what the function will in fact receive: void f(char *a) { ... } – Kateryna May 20 '16 at 21:09
  • Keith Thompson, thank you so much, this is exactly what I was looking for! – Kateryna May 20 '16 at 21:27
  • "arrays decay immediately into pointers," depends on the code. Variable `char b[5]; sizeof b` --> `5`, not the size of a pointer. `b` is an array. `void foo(char d[5]) { ... sizeof d` --> the size of a pointer. `d` is a pointer. – chux - Reinstate Monica May 20 '16 at 21:35

0 Answers0