Problem with parsing a BYTE*
-
Here's my code
int ParsePacket(BYTE *aPacket,short int iStartPos); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; int l = ParsePacket(aPacket,5); printf("%d\n",l system("pause"); } int ParsePacket(BYTE *aPacket,short int iStartPos) { bool bFound = false; int iPos = 0; for (int i = 0;i < (sizeof(aPacket) - 1); i++) { printf("Test: %x\n",aPacket[i]); } return iPos; }
This piece of code should output twelve values ( sizeof(aPacket) = 13 ), but instead it only outputs the first three bytes of aPacket... why? -
Here's my code
int ParsePacket(BYTE *aPacket,short int iStartPos); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; int l = ParsePacket(aPacket,5); printf("%d\n",l system("pause"); } int ParsePacket(BYTE *aPacket,short int iStartPos) { bool bFound = false; int iPos = 0; for (int i = 0;i < (sizeof(aPacket) - 1); i++) { printf("Test: %x\n",aPacket[i]); } return iPos; }
This piece of code should output twelve values ( sizeof(aPacket) = 13 ), but instead it only outputs the first three bytes of aPacket... why?TO get a hint as to the problem, you should have added an printf for "sizeof(aPacket)". In the context of the ParsePacket function, sizeof(aPacket) is 4, because ParsePacket recieves aPacket as a BYTE *, and on the 32-bit x86 architecture, all pointers are 4 bytes. The sizeof operator has no way of determining the length of an array pointed to by aPacket. i.e.: //----- #include #include int ParsePacket(BYTE *aPacket,short int iStartPos); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; printf("size (in main): %i\n",sizeof(aPacket)); int l = ParsePacket(aPacket,5); printf("%d\n",l); system("pause"); } int ParsePacket(BYTE * aPacket,short int iStartPos) { bool bFound = false; int iPos = 0; printf("size (in ParsePacket): %i\n",sizeof(aPacket)); for (int i = 0;i < (sizeof(aPacket) - 1); i++) { printf("Test: %x\n",aPacket[i]); } return iPos; } //----- outputs: ----- size (in main): 13 size (in ParsePacket): 4 Test: 59 Test: 89 Test: 59 0 Press any key to continue . . . ----- You need to pass the length of the array to the ParsePacket method. i.e.: //----- #include #include void ParsePacket(BYTE *aPacket,int aPacketLength); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; ParsePacket(aPacket,sizeof(aPacket)); system("pause"); } void ParsePacket(BYTE * aPacket,int aPacketLength) { for (int i = 0; i < aPacketLength; i++) { printf("Test: %x\n",aPacket[i]); } } //----- outputs: ----- Test: 59 Test: 89 Test: 59 Test: 59 Test: 23 Test: 59 Test: 59 Test: 59 Test: 89 Test: 59 Test: 0 Test: 54 Test: 65 Press any key to continue . . . -----
-
TO get a hint as to the problem, you should have added an printf for "sizeof(aPacket)". In the context of the ParsePacket function, sizeof(aPacket) is 4, because ParsePacket recieves aPacket as a BYTE *, and on the 32-bit x86 architecture, all pointers are 4 bytes. The sizeof operator has no way of determining the length of an array pointed to by aPacket. i.e.: //----- #include #include int ParsePacket(BYTE *aPacket,short int iStartPos); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; printf("size (in main): %i\n",sizeof(aPacket)); int l = ParsePacket(aPacket,5); printf("%d\n",l); system("pause"); } int ParsePacket(BYTE * aPacket,short int iStartPos) { bool bFound = false; int iPos = 0; printf("size (in ParsePacket): %i\n",sizeof(aPacket)); for (int i = 0;i < (sizeof(aPacket) - 1); i++) { printf("Test: %x\n",aPacket[i]); } return iPos; } //----- outputs: ----- size (in main): 13 size (in ParsePacket): 4 Test: 59 Test: 89 Test: 59 0 Press any key to continue . . . ----- You need to pass the length of the array to the ParsePacket method. i.e.: //----- #include #include void ParsePacket(BYTE *aPacket,int aPacketLength); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; ParsePacket(aPacket,sizeof(aPacket)); system("pause"); } void ParsePacket(BYTE * aPacket,int aPacketLength) { for (int i = 0; i < aPacketLength; i++) { printf("Test: %x\n",aPacket[i]); } } //----- outputs: ----- Test: 59 Test: 89 Test: 59 Test: 59 Test: 23 Test: 59 Test: 59 Test: 59 Test: 89 Test: 59 Test: 0 Test: 54 Test: 65 Press any key to continue . . . -----
-
You're very welcome. Glad I could help. :)