-  1 //利用结构体读取Pascal串
-  2 
-  3 #include <stdlib.h>
-  4 #include <stdio.h>
-  5 
-  6 struct StrPascal
-  7 {
-  8     unsigned short int nLength;
-  9     char string[0];  //结构体中柔性数组
- 10 };
- 11 
- 12 int main()
- 13 {
- 14     unsigned char PascalData[48] = {
- 15         0x0C, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x0D, 0x00,
- 16         0x77, 0x77, 0x77, 0x2E, 0x70, 0x65, 0x64, 0x69, 0x79, 0x2E, 0x63, 0x6F, 0x6D, 0x04, 0x00, 0x6A,
- 17         0x6A, 0x79, 0x79, 0x03, 0x00, 0x43, 0x2B, 0x2B, 0x06, 0x00, 0x62, 0x61, 0x63, 0x6B, 0x65, 0x72
- 18     };
- 19 
- 20     struct StrPascal *pStrPascal = (struct StrPascal *)PascalData;  //定义结构体指针,并赋值强转后的数组指针
- 21 
- 22     while ((int)pStrPascal < (int)PascalData + sizeof(PascalData))  //指针数值小于 原数组首地址加总大小 则继续
- 23     {
- 24         for (int i = 0; i < pStrPascal->nLength; i++) //按数组中存放的字符串长度输出
- 25         {
- 26             putchar(pStrPascal->string[i]);
- 27         }
- 28         
- 29         //指针重新赋值    当前指针地址 + 2个字节(存储字符串长度所占用的内存空间) + 当前字符串长度
- 30         pStrPascal = (struct StrPascal *)((int)pStrPascal + sizeof(pStrPascal->nLength) + pStrPascal->nLength);
- 31         puts("");
- 32     }
- 33 
- 34     system("pause");
- 35     return 0;
- 36 }