I'm working on my I²C read function to pass an array pointer to a function and copy the data from the buffer.
Now I'm just testing the methodology of passing the array in code blocks to see the results.
My questions:
- Is the array passing to the function from main without correct?
- Also is my way of developing the function and passing the array as a pointer a good code strategy?
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
uint8_t reaArr (uint8_t len, uint8_t *data);
int main(void)
{
uint8_t receive[9];
uint8_t k;
receive[9] = reaArr(9, receive);
for (k=0; k<9; k++)
{
printf("%d\n", receive[k]);
}
return 0;
}
uint8_t reaArr (uint8_t len, uint8_t *data)
{
uint8_t arr[9] = {20, 55, 66, 33, 5, 4, 45, 27, 59}, i, arrIndex = len;
for (i=0; i<arrIndex; i++)
{
data[i] = arr[i];
}
return *data;
}
return *datais returning the first element of array data because name of an array is pointer to first element of the array so basically you dereferenced the pointer to first element of array data, and returned it.