- typedef can use to give a type a new name.
- Following is an example to define a term Velraj for one-byte numbers −
- typedef unsigned char Velraj;
Program:
/* typedef program by Velraj.K
Check : http://velrajcoding.blogspot.in
*/
#include <stdio.h>
#define MAC_BUF_LEN 6
/* This also can bee used to access the single field.
Here there is no magic, just use unsigned char mac[6] replace with MacAddress mac */
typedef unsigned char MacAddress[MAC_BUF_LEN];
// This typedef used to access the single char filed in mac
typedef unsigned char MacAddress_access;
int main()
{
MacAddress mac_2 = {0x00,0x0c,0x29,0x44,0x11,0x33}; // This is corrent this is equivalent to unigned char mac[6]
MacAddress_access mac[6] = {0x00,0x0c,0xFF,0x44,0x00,0xEE}; // This is wrong because this is equivalent to unsigned char mac[6][6]
MacAddress *pmac= NULL; // This is equilaent to unsigned char *mac[6]
// pmac = &mac_2;
printf("Size of tMacAddr mac_2 = %lu mac = %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX \n", sizeof(mac_2), mac_2[0], mac_2[1], mac_2[2], mac_2[3], mac_2[4], mac_2[5] );
printf("Base address = %p address + 1 = %p addd +2 = %p \n", &mac_2, &mac_2[1], &mac_2[2]);
printf("Pointer tMacAddr+1 = %p pmac[1] Add = %p pmac[2] = %p \n", pmac+1, &pmac[1], &pmac[2]);
printf("print the mac = %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX \n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return 0;
}
Check : http://velrajcoding.blogspot.in
*/
#include <stdio.h>
#define MAC_BUF_LEN 6
/* This also can bee used to access the single field.
Here there is no magic, just use unsigned char mac[6] replace with MacAddress mac */
typedef unsigned char MacAddress[MAC_BUF_LEN];
// This typedef used to access the single char filed in mac
typedef unsigned char MacAddress_access;
int main()
{
MacAddress mac_2 = {0x00,0x0c,0x29,0x44,0x11,0x33}; // This is corrent this is equivalent to unigned char mac[6]
MacAddress_access mac[6] = {0x00,0x0c,0xFF,0x44,0x00,0xEE}; // This is wrong because this is equivalent to unsigned char mac[6][6]
MacAddress *pmac= NULL; // This is equilaent to unsigned char *mac[6]
// pmac = &mac_2;
printf("Size of tMacAddr mac_2 = %lu mac = %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX \n", sizeof(mac_2), mac_2[0], mac_2[1], mac_2[2], mac_2[3], mac_2[4], mac_2[5] );
printf("Base address = %p address + 1 = %p addd +2 = %p \n", &mac_2, &mac_2[1], &mac_2[2]);
printf("Pointer tMacAddr+1 = %p pmac[1] Add = %p pmac[2] = %p \n", pmac+1, &pmac[1], &pmac[2]);
printf("print the mac = %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX \n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return 0;
}
Output:
sample$ ./a.out
Size of tMacAddr mac_2 = 6 mac = 00:0C:29:44:11:33
Base address = 0x7ffc47bce8d0 address + 1 = 0x7ffc47bce8d1 addd +2 = 0x7ffc47bce8d2
Pointer tMacAddr+1 = 0x6 pmac[1] Add = 0x6 pmac[2] = 0xc
print the mac = 00:0C:FF:44:00:EE
No comments:
Post a Comment