0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語 int a, b, c, d ≒ int a[4] ??

Last updated at Posted at 2025-04-21

アセンブリ言語の確認及び実行に使用するサイト
https://godbolt.org/

比較

int a=1, b=2, c=3, d=4

image.png

int a[4]

image.png

考察

配列と並べて宣言した場合ではメモリ上での順番が違っている。

int a=1, b=2, c=3, d=4を配列のようにポインタで指定

メモリ上での順番がa,d,c,bとなっているのでポインタをaに合わせて無理矢理a,b,c,dの順番で表示させる。
image.png

test.c
#include <stdio.h>

int main(){
	int a=1;
	int b=2;
	int c=3;
	int d=4;
	
	int *e = &a;
    printf("%d\n", *e);                          // a (1)
    printf("%d\n", *(int*)((char*)e + 24));      // b (2)
    printf("%d\n", *(int*)((char*)e + 20));      // c (3)
    printf("%d\n", *(int*)((char*)e + 16));      // d (4)
	
	return 0;
}
実行結果.bash
1
2
3
4

int a=1, b=2, c=3, d=4 と int a[4]は全く同じになると思っていましたが、実際に宣言した順番にメモリ上に置かれる訳ではないことが分かりました。
だから何だという話ですが、只の実験です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?