-1

このプログラムでは、いくつかのメンバーと共に構造体を宣言し、この特定の構造体のインスタンスとして 2 つの要素の配列を使用しました。この構造では 5 要素の配列を使用しました。奇妙なことの 1 つは、main 関数で、5 要素配列の存在しない 6 番目の要素のアドレスを取得しようとすると、次の構造体の最初のメンバー整数のアドレスを取得することです。存在しない配列要素にアクセスしようとしたときにエラーが表示されなかったのはなぜですか? また、2 つのメンバーが同じアドレスを持っているのはなぜですか?

#include<stdio.h>
 struct {int num1,num2; char s1; int *ptr; int abc[5]; }a[2];

void main(){

      int start, last; 

      start = &a[1].num1;

      last = &a[0].num1;

      printf("\nSize of the Structure : %d Bytes", start-last);

      printf("\naddress of num1 in structure a[0] is %d", &a[0].num1);

      printf("\naddress of num2 in structure  a[0] is %d", &a[0].num2);

      printf("\naddress of char in structure a[0] is %d", &a[0].s1);

      printf("\naddress of ptr in structure a[0] is %d", &a[0].ptr);

      printf("\naddress of I element in array abc[5] in structure a[0] is %d", &a[0].abc[0]);

      printf("\naddress of II element in array abc[5] in structure a[0] is %d", &a[0].abc[1]);

      printf("\naddress of III element in array abc[5] in structure a[0] is %d", &a[0].abc[2]);

      printf("\naddress of IV element in array abc[5] in structure a[0] is %d", &a[0].abc[3]);

      printf("\naddress of V element in array abc[5] in structure a[0] is %d", &a[0].abc[4]);

      **printf("\naddress of VI element in the 5 element array abc[5] in structure a[0] is %d", &a[0].abc[5]);

      printf("\naddress of num1 in structure a[1] is %d", &a[1].num1);**

      printf("\n");


  }

kevin@kevin-desktop:~/Documents/programs$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

kevin@kevin-desktop:~/Documents/programs$ gcc structure_size.c -o structure_size
structure_size.c: In function ‘main’:
structure_size.c:15: warning: assignment makes integer from pointer without a cast
structure_size.c:17: warning: assignment makes integer from pointer without a cast
structure_size.c:21: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
structure_size.c:23: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
structure_size.c:25: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char *’
structure_size.c:27: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
structure_size.c:29: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
structure_size.c:31: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
structure_size.c:33: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
structure_size.c:35: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
structure_size.c:37: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
structure_size.c:39: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
structure_size.c:41: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
kevin@kevin-desktop:~/Documents/programs$ ./structure_size

Size of the Structure : 36 Bytes
address of num1 in structure a[0] is 134520896
address of num2 in structure  a[0] is 134520900
address of char in structure a[0] is 134520904
address of ptr in structure a[0] is 134520908
address of I element in array abc[5] in structure a[0] is 134520912
address of II element in array abc[5] in structure a[0] is 134520916
address of III element in array abc[5] in structure a[0] is 134520920
address of IV element in array abc[5] in structure a[0] is 134520924
address of V element in array abc[5] in structure a[0] is 134520928
**address of VI element in the 5 element array abc[5] in structure a[0] is 134520932
address of num1 in structure a[1] is 134520932**
kevin@kevin-desktop:~/Documents/programs$ 
4

3 に答える 3

2

これがCの仕組みです。この単純化されたコードでは、現象がより簡単に示されていると思います。

char string[2][5];              // allocates a block of 10 bytes

string == string[0];            // true
string == &string[0][0];        // true
&string[0][5] == &string[1][0]; // true
于 2013-11-06T08:31:53.690 に答える