mardi 31 octobre 2017

unintended result of memcpy

I have the following code. I'm a newbie and would like to know what's wrong with the code below. The intent is to get the mac value from the function getMac correctly to the main.

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

void getMac(unsigned char *);

int main()
{
    unsigned char mac[6];
    getMac(mac);
    printf("back to main(), mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
        mac[0],mac[1],mac[2], mac[3], mac[4], mac[5]);
}

void getMac(unsigned char *mac)
{
    unsigned char eth[] = "44:bg:23:1f:20:77";
    printf("eth: %s\n", eth);
    memcpy(mac, eth, 6);
    return;
}

Aucun commentaire:

Enregistrer un commentaire