Ex18-구조체 변수로 1차원 포인터 사용

CODEDRAGON Development/C, C++

반응형

   

구조체 변수로 1차원 포인터 사용

   

   

학습내용

(*sp).no = sp->no

   

->

구조체 포인터 변수에서만 사용

   

1차원 포인터 변수

int a=3;

int* p = NULL;

p=&a;

   

   

메모리 구조

   

소스코드

   

#include <stdio.h>

struct student{
        char no[10];            //

        char name[20];          //

        double total;           //

};

int main(void){
        struct student stu = {"123456", "Alice", 100};  //
구조체 변수
        struct student* sp=NULL;        // 1
차원 구조체 포인터 변수 선언, sp struct student 주소를 저장


        sp = &stu;
        printf("%s %s %lf \n", stu.no, stu.name, stu.total);
        printf("%s %s %lf \n", (*sp).no, (*sp).name, (*sp).total);  // 1
차원 포인터를 이용한 접근
        printf("%s %s %lf \n", sp->no, sp->name, sp->total);        // 1
차원 포인터를 이용한 접근(구조체포인터변수에서만 사용)

        return 0;
}


   

   

출력결과

 

반응형