-1

I am working on a C module in micropython... if I pass a byte array to a function, only the first 8 bytes make it (according to sizeof). I have to also send in the length, then copy it to access everything in a function.

static void printSomeBytes(char *description, byte *bytes)
{    
    printf("\r\n%s: ", description);
    for (int i = 0; i < sizeof(bytes); ++i )
    {
        printf("%02X", bytes[i]); 
    }
}

static void printAllBytes(char *description, byte *bytes, int length)
{    
    byte copy[length];
    memcpy(copy, bytes, length);

    printf("\r\n%s: ", description);
    for (int i = 0; i < sizeof(copy); ++i )
    {
        printf("%02X", copy[i]); 
    }

    // this also works without making a copy 
    //for (int i = 0; i < length; ++i )
    //{
    //    printf("%02X", bytes[i]); 
    //}
}

byte Kifd[] = { 0x0B, 0x79, 0x52, 0x40, 0xCB, 0x70, 0x49, 0xB0, 0x1C, 0x19, 0xB3, 0x3E, 0x32, 0x80, 0x4F, 0x0B};

printSomeBytes("Kifd", kifd); // prints "Kifd: 0B795240CB7049B0"
printAllBytes("Kifd", kifd, sizeof(kifd)); // prints "Kifd: 0B795240CB7049B01C19B33E32804F0B"

What am I doing wrong / is there a better way to send a pointer to a byte array to a function?

IAmCoder
  • 3,179
  • 2
  • 27
  • 49
  • 1
    Detail: With `printAllBytes("Kifd", kifd, sizeof(kifd))`, the function does not receive a "a pointer to a byte array" but a "pointer to the first element of the byte array" a `byte*`. `&Kifd` is a "pointer to a byte array". Same effective address - different types – chux - Reinstate Monica Jan 30 '18 at 23:44
  • Possible duplicate of [Finding length of array inside a function](https://stackoverflow.com/questions/17590226/finding-length-of-array-inside-a-function) – Yunnosch Aug 30 '18 at 06:32

2 Answers2

3

You've done a poor job of explaining the issue. Are you saying that sizeof(bytes) returns 8?

bytes is a pointer, and sizeof(bytes) is returning the size of that pointer. And pointers may be 8 bytes on your system. That has nothing to do with the number of bytes at the address it points to.

In C, when you get a pointer, there is no way to know how many bytes it points to unless you provide that information as another argument or have a special terminating value in the data.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
3

sizeof(bytes) returns you the number of bytes that a pointer to byte will need to be stored in memory. It doesn't return you the number of bytes that the array pointed to by bytes contains.

For that you need to pass that size to the function:

static void printSomeBytes(char *description, byte *bytes, size_t size)
{
    printf("\r\n%s: ", description);
    for (size_t i = 0; i < size; ++i )
    {
        printf("%02X", bytes[i]); 
    }

    puts("");

}

edit
I also added puts("") there so that the bytes are printed right away. Note that printf is buffered and it won't show the output on the screen unless you flush it (fflush(stdout);) by hand or add a '\n' newline at the end of printf. puts(string) is equivalent to printf("%s\n", string); but without the overhead of having to parse the format argument.
end edit

And then call it:

byte Kifd[] = { 0x0B, 0x79, 0x52, 0x40, 0xCB, 0x70, 0x49, 0xB0, 0x1C, 0x19, 0xB3, 0x3E, 0x32, 0x80, 0x4F, 0x0B};

printSomeBytes("Kifd", Kifd, sizeof Kifd / sizeof *Kifd);

Also the correct way of getting the number of elements of an array is:

sizeof array / sizeof *array

I encourage you to use that formula even when you know that the type is 8bit long. It makes the code more portable.

Pablo
  • 13,271
  • 4
  • 39
  • 59