0

This code return me "in printf" only ID2[0],ID2[1] and the first "character" of ID2[2]. Where is the problem ?

#include <stdio.h>
#include <string.h>

char *getMAC(char *buf, int ID[6]);

int main() {
int ID2[6]={0x00, 0x0d, 0x3f, 0x8d, 0x07, 0x3a};
    char mac[18];
    getMAC(mac,ID2);
    printf("MAC:%s", mac);
return 0;
}
char *getMAC(char *buf,int ID[6])
{
    snprintf(buf, sizeof(buf), "%02X%02X%02X%02X%02X%02X",
            ID[0],ID[1],ID[2],ID[3],ID[4],ID[5]);
    return buf;
}

I have like MAC:000D3F8.

JLo
  • 53
  • 1
  • 7
  • 2
    `sizeof(buf)` is not how to get size of passed buffer. Pass size of buffer explicitly for safety. – MikeCAT Jun 06 '16 at 14:53
  • Thanks a lot @MikeCAT. Can you explain why happen this ? – JLo Jun 06 '16 at 14:55
  • 1
    Because size of pointers in your system is 8, only 7 characters + terminating null character is stored to avoid buffer overrun. – MikeCAT Jun 06 '16 at 14:56

0 Answers0