[c++] structor(구조체) 본문

객체지향프로그래밍 (C++)

[c++] structor(구조체)

미니모아 2020. 7. 7. 15:57
반응형
  1. 구조체 배열
    struct Student {
     int id;//4
     char name[20];//1
     float grade[2];//8
    };
    
    int main() {
      Student sinfos[4] = {
      {202001,"Lee",{4.3f,4.1f}},
      {202001,"Choi",{4.3f,4.1f}},
      {202001,"Park",{4.3f,4.1f}},
      };
    
      for (auto i = 0; i < 4; i++) {
          cout << sinfos[i].id << endl;
          cout << sinfos[i].name << endl;
          cout << sinfos[i].grade[0] << endl;
          cout << sinfos[i].grade[1] << endl;
      }
    return 0;
    }

 

2. 구조체 포인터


struct Rectangle {
    int x, y;
    int w, h;
};

int main() {
    Rectangle r = { 15,10,50,70 };
    Rectangle* pr = &r;

    cout << r.x << " "<< (*pr).x << endl;
    cout << (*pr).y << " " << pr->y << endl;
    }
  • LinkedList

    struct LinkedList {
      int data;
      LinkedList* p;
    };
    int main() {
      LinkedList a, b, c;
      a.data = 99;
      a.p = &b;
      b.data = 93;
      b.p = &c;
      c.data = 94;
      c.p = &a;
    
      cout << c.data << endl;
      cout << b.p->data << endl;
      cout << (*b.p).data << endl;
      cout << a.p->p->data << endl; 
      cout << c.p->p->p->data << endl;
      return 0;
    }
    

 

반응형

'객체지향프로그래밍 (C++)' 카테고리의 다른 글

[c++] 데이터 표현  (0) 2020.07.07
[c++] Union (공용체)  (0) 2020.07.07
[c++] Pointer (포인터)  (0) 2020.07.07
[c++] inline variable  (0) 2020.07.07
[c++] class , object ( 클래스, 객체)  (0) 2020.07.07
Comments