0

I have an array like this

uint8_t mArray[8] = {0xAA,0xBB,0xCC,0xDD,0x00,0x11,0x22};

I send to this array to two different function like this: In main.c

  func1(2,(uint8_t*)&mArray);

In function.c

void func1(uint8_t pos, uint8_t* mArray)
{
....
// When I print the mArray here I get correct value
func2(pos,&mArray);
}

When I print to mArray in func1 I get aa bb cc dd 00 11 22 00

    void func2(uint8_t pos, uint8_t* mArray)
    {
      uint16_t i;
      for(i = 0 ; i < 8; i++)
      {
      printf("%02x ",mArray[i]);
      }
.... 
    }

When I print mArray here I get meanless number e8 ff 02 20 84 26 00 20

Where is the problem, why the array does not passing second time ?

gogogo
  • 529
  • 1
  • 3
  • 11
  • 3
    In `func1`: `func2(pos,&mArray);` --> `func2(pos,mArray);` as it is already a pointer. – Paul Ogilvie Jul 02 '21 at 07:49
  • 3
    Are warnings of your compiler on? It might have given a warning message. – Paul Ogilvie Jul 02 '21 at 07:49
  • 2
    `func2(pos,&mArray);` is passing the location of the argument on the stack, not its value. – Weather Vane Jul 02 '21 at 07:54
  • But when I print the sizeof(mArray) in func2 I get 4 value although length is 7 why this happening ? – gogogo Jul 02 '21 at 08:11
  • 1
    That is the size of the pointer. You can't find the length of what it points to. Please see [c, finding length of array inside a function](https://stackoverflow.com/questions/17590226/c-finding-length-of-array-inside-a-function) – Weather Vane Jul 02 '21 at 08:13
  • 3
    Remove all of your address-of operators `&` and type casts `(uint8_t *)`. None of them are necessary, and they're breaking your code. – Tom Karzes Jul 02 '21 at 08:22
  • I couldnt find array length with (sizeof((mArray)/(sizeof(mArray[0]))) return 4 again. – gogogo Jul 02 '21 at 08:27
  • but when I print array as your advise I printed correctly – gogogo Jul 02 '21 at 08:29
  • *"I couldnt find array length with"* - As we said, you cannot do that inside the function you passed the array to – klutt Jul 02 '21 at 08:35
  • @gogogo You can find out the length of an array only where the array is defined, probably in the `main` function. When you pass this array to a function, the function will get only a pointer to the first array element. If you need the length in a called function you have to calculate the length in the calling function and pass the length as an additional argument, e.g. `func1(2, mArray, sizeof(mArray)/sizeof(mArray[0]))` and `void func1(uint8_t pos, uint8_t* mArray, size_t arrayLength)` – Bodo Jul 02 '21 at 08:38
  • You should also change `uint16_t` to `size_t` – klutt Jul 02 '21 at 08:46
  • Not really a dupe but worth reading: https://stackoverflow.com/a/3960723/4386427 – Support Ukraine Jul 02 '21 at 08:56
  • 2
    "Where is the problem" The problem is that you took a chance at the C syntax and didn't read the compiler warnings. And then you have some lax compiler which doesn't give an error for C language violations. Make a habit of compiling with `-Werror` until you've learnt C. – Lundin Jul 02 '21 at 08:58
  • Hmm... you have a cast here: `(uint8_t*)&mArray` What made you put the cast there? A compiler warning? If so why did you ignore the warning from doing: `func2(pos,&mArray);`? Anyway... Don't use casts to get rid of warnings. Actually you shouldn't use casts at all (expect for a few special cases). So whenever you think you need a cast, think it over again. Your first reaction to any cast should be: This is most likely wrong. – Support Ukraine Jul 02 '21 at 09:12
  • FYI: the bare name of an array degrades to the address of the first byte of the array – user3629249 Jul 04 '21 at 01:58

1 Answers1

2

Providing an array where a pointer is expected (as is the case in main) results in a pointer to the first element of the array. So we just need func1(2, mArray); in main.

In func1, you want to pass the pointer you received to func2, so you want func2(pos, mArray);. You don't want &mArray any more than you want &pos. You simply want to pass the value of the parameter variable (the address of the first element of main's mArray), not the address of the parameter variable.

Fixed:

void func2(uint8_t pos, uint8_t* mArray)
{
   uint16_t i;
   for(i = 0 ; i < 8; i++)
   {
      printf("%02x ", mArray[i]);
   }
}

void func1(uint8_t pos, uint8_t* mArray)
{
   func2(pos, mArray);
}

int main()
   uint8_t mArray[8] = {0xAA,0xBB,0xCC,0xDD,0x00,0x11,0x22};
   func1(2, mArray);
}
ikegami
  • 367,544
  • 15
  • 269
  • 518