0

I'm facing an issue on a PIC from Microchip but my question is more general. This will help me check if it is an issue from my code or from the debugger of MPLAB X which is sometimes buggy.

So here is my question, let's say I have this function:

int test( int *array )
{
   int a = array[0];
   return a;
}

Now, if I want to pass an array it's working, there is no issue. But let's say I want to use a single variable instead of the array like this:

int main()
{
   while(1)
   {
      int test_variable_1 = 8;
      int test_variable_2 = test( &test_variable_1 );
   }
   return 0;
}

I did not use an array but for me this should work as expected since the test_variable_1 is like an array of size of one int.

EDIT QUESTION: So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function? Also, is it a good/bad practice?

Thanks in advance and don't hesitate to tell me if I'm unclear.

Have a good day!

Adrien

Adrien G.
  • 411
  • 3
  • 15
  • 1
    `Can you explain this to me please?` <- I am not sure what you wish to have explained. Please edit your question to make the question more explicit. – Morten Jensen Nov 25 '20 at 08:39
  • 1
    In what way did it not 'work'? What is the `while(1)` for? The function `test()` has no idea whether the pointer argument passed points to one variable or to an array of them. So the code is correct. `int a = array[0];` is the same as `int a = *array;`. – Weather Vane Nov 25 '20 at 08:41
  • 1
    About the edit: is it good practice? What you show seems pointless so it is hard to say if it's a good idea to call a function with a pointer instead of doing `test_variable_2 = test_variable_1;` – Weather Vane Nov 25 '20 at 08:48
  • while(1) does nothing it was just to reproduce a basic main function. Yes, the ````test()``` function seems ok to me. But it is how I'm using it: Can I give it a single int instead of an array? (I update my question by the way) – Adrien G. Nov 25 '20 at 08:49
  • 1
    No, you have to pass a pointer (you did). As I wrote, the function does not know what is being pointed to. – Weather Vane Nov 25 '20 at 08:49
  • 1
    No, the function is seeing a pointer, and does not know or care whether it a one value or an array being pointed to. – Weather Vane Nov 25 '20 at 08:52
  • Ok thanks so everything was OK. By the way, the debugger now gets the right value after waiting several minutes it is working well (classic Microchip I guess). Thanks everyone! – Adrien G. Nov 25 '20 at 08:52
  • Every MCU I worked with, I had to make my own emulator, because the offerings from the manufacturer were terrible (and unreasonably slow). – Weather Vane Nov 25 '20 at 08:54
  • I agree, and it is very unstable. Sometimes you need to restart everything (computer + software), change the debugger and pray before it is working properly – Adrien G. Nov 25 '20 at 08:57

1 Answers1

2

So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function?

Yes. In fact C doesn't even know that you passed an array. For details, see Do pointers support "array style indexing"?

Also, is it a good/bad practice?

It's fine. Though ideally the function should take the number of items as one of the parameters passed.

Lundin
  • 195,001
  • 40
  • 254
  • 396