abc.name[3] = "OK";
C言語は文字列の代入は出来ません。strcpy()を使いましょう。
・・・添え字はおかしい(3番めはない)
abc.name[0] = 'O';
abc.name[1] = 'K';
abc.name[2] = '\0';
でもいいけど・・・
「追記」
ソース改変しました。
c
1#include <stdio.h>
2//#include <stdlib.h>
3
4typedef struct {
5 char name[3];
6 int number;
7 double average;
8} ABC;
9
10typedef struct {
11 char ID[3];
12 ABC data;
13 int at;
14} DEF;
15
16int main(void)
17{
18 ABC abc;
19 ABC *pabc;
20 pabc = &abc;
21 abc.number = 100;
22 abc.name[0] = 'O';
23 abc.name[1] = 'K';
24 abc.name[2] = '\0';
25 // abc.name[3] = "OK";
26 DEF b[] = { { "t2", { "x6", 200, 21.2 }, 12 },
27 { "t3", { "x7", 300, 13.3 }, 13 },
28 { "t4", { "xA", 400, 77.1 }, 14 } };
29
30 printf("%d\n", pabc->number);
31 printf("%s\n", pabc->name);
32
33 return 0;
34}
bが使われていない!ワーニングは出ますが・・・
text
1usr ~/Project/test/teratail % cc t176513.c
2t176513.c:26:9: warning: unused variable 'b' [-Wunused-variable]
3 DEF b[] = { { "t2", { "x6", 200, 21.2 }, 12 },
4 ^
51 warning generated.
6usr ~/Project/test/teratail % ./a.out
7100
8OK
9usr ~/Project/test/teratail %
もとのソースのアセンブル結果(clang)
asm
1 .text
2 .file "t.c"
3 .globl main # -- Begin function main
4 .p2align 4, 0x90
5 .type main,@function
6main: # @main
7 .cfi_startproc
8# %bb.0:
9 subq $24, %rsp
10 .cfi_def_cfa_offset 32
11 movl $100, 12(%rsp)
12 movl $.L.str, %eax
13 movb %al, 11(%rsp)
14 movl $.L.str.1, %edi
15 movl $100, %esi
16 xorl %eax, %eax
17 callq printf
18 leaq 8(%rsp), %rdi
19 callq puts
20 xorl %eax, %eax
21 addq $24, %rsp
22 .cfi_def_cfa_offset 8
23 retq
24.Lfunc_end0:
25 .size main, .Lfunc_end0-main
26 .cfi_endproc
27 # -- End function
28 .type .L.str,@object # @.str
29 .section .rodata.str1.1,"aMS",@progbits,1
30.L.str:
31 .asciz "OK"
32 .size .L.str, 3
33
34 .type .L.str.1,@object # @.str.1
35.L.str.1:
36 .asciz "%d\n"
37 .size .L.str.1, 4
38
39
40 .ident "clang version 8.0.0 (trunk 350063)"
41 .section ".note.GNU-stack","",@progbits
42 .addrsig
「movb %al, 11(%rsp)」これを見ると、うまい具合wにいけてますね。puts()呼んでる^^;
こちらの結果は、「100以外何も表示されない」です。たぶん'\0'が有った。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。