在C(Iseries / AS400)中初始化typedef结构的问题

问题描述 投票:0回答:1

[当我尝试初始化此typedef结构时,我做错了。

我在linux中做了同样的声明,我可以创造完美。

这是定义:

typedef struct {                                                               
 char mask;    /* char data will be bitwise AND with this */                   
 char lead;    /* start bytes of current char in utf-8 encoded character */    
 uint32_t beg; /* beginning of codepoint range */                              
 uint32_t end; /* end of codepoint range */                                    
 int bits_stored; /* the number of bits from the codepoint that fits in char */
}utf_t;                                                                        
utf_t * utf[];                                                                 
/*                 mask        lead        beg      end       bits  */         
 utf_t * utf[] = {                                                           
    [0] = &(utf_t){0b00111111, 0b10000000, 0,       0,        6    },          
    [1] = &(utf_t){0b01111111, 0b00000000, 0000,    0177,     7    },          
    [2] = &(utf_t){0b00011111, 0b11000000, 0200,    03777,    5    },          
    [3] = &(utf_t){0b00001111, 0b11100000, 04000,   0177777,  4    },          
    [4] = &(utf_t){0b00000111, 0b11110000, 0200000, 04177777, 3    },          
          &(utf_t){0},                                                         
   };

[当我尝试编译时,我不创建假脱机文件,但是在作业日志中显示此消息:

Message ID . . . . . . :   MCH3601       Severity . . . . . . . :   40       
Message type . . . . . :   Escape                                            
Date sent  . . . . . . :   18/11/19      Time sent  . . . . . . :   10:02:49 

Message . . . . :   Pointer not set for location referenced.                 
Cause . . . . . :   A pointer was used, either directly or as a basing       
  pointer, that has not been set to an address.                               

有人可以告诉我我在做什么错吗?谢谢。

问候,

c ibm-midrange
1个回答
0
投票

您在这里使用复合文字:

两种初始化方式:

utf_t *utf[6];  
utf_t *utf1[] = {                                                           
    &(utf_t){0b00111111, 0b10000000, 0,       0,        6    },          
    &(utf_t){0b01111111, 0b00000000, 0000,    0177,     7    },          
    &(utf_t){0b00011111, 0b11000000, 0200,    03777,    5    },          
    &(utf_t){0b00001111, 0b11100000, 04000,   0177777,  4    },          
    &(utf_t){0b00000111, 0b11110000, 0200000, 04177777, 3    },          
    &(utf_t){0},                                                         
   };                                                               

void foo(void)
{

memcpy(utf, (utf_t *[]){                                                           
    &(utf_t){0b00111111, 0b10000000, 0,       0,        6    },          
    &(utf_t){0b01111111, 0b00000000, 0000,    0177,     7    },          
    &(utf_t){0b00011111, 0b11000000, 0200,    03777,    5    },          
    &(utf_t){0b00001111, 0b11100000, 04000,   0177777,  4    },          
    &(utf_t){0b00000111, 0b11110000, 0200000, 04177777, 3    },          
    &(utf_t){0},                                                         
   }, sizeof(utf));
}

int main()
{
    int arr[1000];
    foo();
    memset(arr,0, sizeof(arr));
    printf("%hhu %hhu %u\n", utf[2] -> mask, utf[2] -> lead, utf[2] -> beg);
    printf("%hhu %hhu %u", utf1[2] -> mask, utf1[2] -> lead, utf1[2] -> beg);
}

https://godbolt.org/z/CegYGb

© www.soinside.com 2019 - 2024. All rights reserved.