LoginSignup
3
6

More than 5 years have passed since last update.

c++ 構造体の諸々 No3

Last updated at Posted at 2018-05-03

概要

構造体を読み書きするときに気になった細かいところを個人的に纏めたもの。

目次

1.構造体の基本
2.宣言編
3.関数編

1.構造体の基本

基本的な構造体の扱い方についてはここを参照初心者のためのポイント学習C言語 15章 構造体

2.宣言編

型枠の宣言
struct 構造体タグ名
{
    メンバ変数;
};

構造体変数の宣言
struct 構造体タグ名 構造体変数名

他にtypedefを使う方法。typedefは既に定義されている型に、別の新しい名前をつけて定義する。

typedef 既に定義されている型 定義する新しい型

以下に実際の宣言の例

struct _person{
    char name[20];
    int age;
    double height;
};

struct _person person;


typedef struct _person{
    char name[20];
    int age;
    double height;
}Person;

Person person;

型枠の宣言と同時にその型のポインタの宣言をする方法

struct _person{
    char name[20];
    int age;
    double height;
}*person;

こうすることで struct _person型のポインタ*personを宣言する。(cf. int型のポインタ*p)

3.関数編

構造体を関数に渡して扱うの例。構造体の型枠の宣言をプロトタイプ宣言の前にしておかないと、エラーがでる。

sample1
#include <bits/stdc++.h>
using namespace std;
//cin.sync_with_stdio(false);

#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,n) FOR(i,0,n)

struct _person{
    string name;
    int no;
    int cost[2];
};

void input(struct _person *person);

int main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    struct _person person[3];

    input(person);

    rep(i, 3){
        cout << person[i].name << endl;
        cout << person[i].no << endl;
        rep(j, 2){
            cout << person[i].cost[j] << endl;
        }
        cout << endl;
    }

    return 0;
}

void input(struct _person *structure){

    rep(i, 3){
        (structure + i) -> name = "abcd";
        (structure + i) -> no = i;
        rep(j, 2){
            (structure + i) -> cost[j] = i * j;
        }
    }
}
出力
abcd
0
0
0

abcd
1
0
1

abcd
2
0
2

構造体の宣言と同時にポインタの宣言をすると、関数に渡すときの作業は変わらないが、構造体変数の確保のために(内部のメンバ変数をポインタにしてる時も)malloc関数等を使ってメモリを動的に確保する必要がある

sample2
#include <bits/stdc++.h>
using namespace std;
//cin.sync_with_stdio(false);

#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,n) FOR(i,0,n)

struct _person{
    int no;
    int *cost;
}*person;

void input(struct _person *person);

int person_num = 2;
int cost_num = 3;

int main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    person = (struct _person *) malloc(sizeof(struct _person) * person_num);
    rep(i, cost_num)    person[i].cost = (int *) malloc(sizeof(int) * cost_num);

    input(person);

    rep(i, person_num){
        cout << person[i].no << endl;
        rep(j, cost_num){
            cout << person[i].cost[j] << endl;
        }
        cout << endl;
    }

    return 0;
}

void input(struct _person *structure){

    rep(i, person_num){
        (structure + i)-> no = i;
        rep(j, cost_num){
        (structure + i) -> cost[j] = i * j;
        }
    }
}
出力
0
0
0
0

1
0
1
2

メモリの動的確保に関してはこちらの記事がわかりやすいです。6.メモリを使ったプログラム

参考文献

初心者のためのポイント学習C言語 15章 構造体
構造体を使ったプログラム
構造体

3
6
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
3
6