#include <stdio.h>


struct smart
{
  int A;
  short B;
  float C;
  char D;
  int E;
  
};

int main()
{
  struct smart test = {100,50,4.1,10,1};    //만들자마자 초기화 하는 방법
  void * vp = &test;  //배열과 다르게 취급하니 주소표시(&)붙이자
  printf("A=%d \n", test.A); 
  printf("A=%d \n", *((int *)vp));     //vp 보이드 라서 캐스팅함.
  vp = (char *)vp+4;
  printf("B=%d \n", *((short *)vp)); 
  vp = (char *)vp+4;
  printf("C=%f \n", *((float *)vp)); 
  vp = (char *)vp+4;  
  printf("D=%d \n", *((char *)vp)); 
  vp = (char *)vp+4;  
  printf("E=%d \n", *((char *)vp)); 
  return 0;
}

변수 메모리 배치

A(int)

 B(short)

 

 

 C(Float)

 D(char)

 

 

 

 E(int)




 #include <stdio.h>


struct smart
{
  int A;
  short B;
  float C;
  char D;
  int E;
  
};

int main()
{
  struct smart test = {100,50,4.1,10,1};    
  struct smart *  stP = &test;

  printf("A=%d \n", stP->A);       
  printf("B=%d \n", stP->B); 
  printf("C=%f \n", stP->C); 
  printf("D=%d \n", stP->D); 
  printf("E=%d \n", stP->E); 

  printf("\n");

  printf("A=%d \n", test.A);       
  printf("B=%d \n", test.B); 
  printf("C=%f \n", (*stP).C); 
  printf("D=%d \n", (*stP).D); 
  printf("E=%d \n", (*stP).E); 

 결과  printf("A=%d \n", stP->A);  
       printf("A
=%d \n", test.A);

       printf("C=%f \n", (*stP).C);

                모두 같음.             

 
  return 0;
}



'C언어' 카테고리의 다른 글

20140414 atmega구조  (0) 2014.04.14
20140411 구조체  (0) 2014.04.11
20140409 구조체 효율높이기  (0) 2014.04.09
20140408 구조체  (0) 2014.04.08
20140307 입출력함수  (0) 2014.04.07
by 날라차숑 2014. 4. 10. 14:23