반응형

일반 변수와 마찬가지로 구조체 변수도 포인터 변수에 주소를 기억 시킬 수 있습니다.

일반 변수는 int *a; 와같이 선언 했다면

구조체 변수는 sutruct student *st;와 같은 형식으로 선언 하시면 됩니다.


일반 변수에서 이런식으로 포인터가 사용되었다면,

int a=5;

int *p;

p=&a;


구조체 변수에서는...

struct student a;

struct student *p;

p=&a;


 



#include <stdio.h>

struct student

{

char name[10];

int score;

int rank;

char grade;

};

int main()

{

struct student a={"hong",99,1,'A'};

struct student *p;

p=&a;


printf("이름 : %s\n",a.name);

printf("P이름 : %s\n",p->name);

return 0;

}



 

일반 구조체변수의 멤버에 접근 할 때는 .(도트연산자)으로 접근 하지만

포인터 구조체 변수의 경우는 -> 기호로 접근 하게 됩니다.


함수에서...


#include <stdio.h>

struct student

{

char name[10];

int score;

int rank;

char grade;

};


void ppp(struct student *p);

int main()

{

struct student a={"hong",99,1,'A'};

ppp(&a);

return 0;

}


void ppp(struct student *p)

{

printf("이름 : %s\n", p->name);

printf("점수 : %d\n", p->score);

printf("등수 : %d\n", p->rank);

printf("등급 : %c\n", p->grade);


}


다른 함수에 구조체 변수의 주소를 인자로 넘겨주고.

함수 내부에서 구조체 멤버에 접근 해보는 소스입니다.


정상적으로 동작할 때는 아래와 같이 -> 기호를 쓰면 접근 가능한 멤버들이 출력되어 집니다.

 

반응형

'Computer > Programing' 카테고리의 다른 글

c언어 파일에 입출력을 해보자  (4) 2014.01.02
c언어 구조체를 함수의 인자로 넘기기  (0) 2013.12.30
c언어 구조체  (0) 2013.12.28

+ Recent posts