struct在C/C++中的定义和使用

目录

C

/**
 * @file have1_without23.c
 */
#include "stdio.h"
int main()
{
    typedef struct
    {
        int x;
        int y;
    };
    return 0;
}
/*报错
missing type-name in typedef-declaration
不同编译器
unnamed struct/union that defines no instances
*/
/**
 * @file have2_without13.c
 */
#include "stdio.h"
struct A
{
    int x;
    int y;
};
int main()
{
    //!定义结构体对象时,需要前置struct字样
    struct A a = {1, 2};
    //!C对结构体赋值方法1
    //!定义时赋值,用到等于和大括号
    printf("(%d,%d)", a.x, a.y);
}
/**
 * @file have3_without12.c
 */
#include "stdio.h"
struct
{
    int x;
    int y;
} B;
//!此种情况,相当于只有B变量是结构体对象
//!不能再有另外的结构体对象
int main()
{
    //!C中对结构体赋值方法2
    //!分别赋值
    B.x = 1, B.y = 1;
    printf("(%d,%d)", B.x, B.y);
}
/**
 * @file have12_without3.c
 */
#include "stdio.h"
typedef struct A
{
    int x;
    int y;
};
//注意结构体内部不初始化
/*警告
'typedef' was ignored in this declaration
相当于没有typedef
*/
void main()
{
    //C风格乱序注释,方法3
    struct A a =
        {
            .y = 2,
            .x = 1};
    printf("(%d,%d)", a.x, a.y);
}
/**
 * @file have13_without2.c
 */
#include "stdio.h"
typedef struct
{
    int x;
    int y;
} B;
//B为结构体别名
void main()
{
    //C++风格乱序注释,方法4
    //前面不带对象加点
    //后面用引号
    //B前面不写struct
    //因为B是一个别名已经包含了struct了
    B b = {
        y : 4,
        x : 1
    };
    printf("(%d,%d)", b.x, b.y);
}
/**
 * @file have23_without1.c
 */
#include "stdio.h"
struct A
{
    int x;
    int y;
} B;
//其中A是结构体名,前面要带上struct
//其中B是结构体对象名
int main()
{
    struct A a = {1, 2};
    B.x = 3, B.y = 4;
    printf("(%d,%d)\n", a.x, a.y);
    printf("(%d,%d)", B.x, B.y);
}
/**
 * @file have123.c
 */
#include "stdio.h"
typedef struct A
{
    int x;
    int y;
} B;
//其中A是结构体名
//定义对象时,前面要带上struct
//其中B是结构体别名,不带struct
int main()
{
    struct A a = {1, 2};
    B b = {3, 4};
    printf("(%d,%d)\n", a.x, a.y);
    printf("(%d,%d)", b.x, b.y);
}
/**
 * @file without123.c
 */
#include "stdio.h"
int main()
{
    struct
    {
        int x;
        int y;
    };
    return 0;
}
/*报错
missing type-name in typedef-declaration
不同编译器
unnamed struct/union that defines no instances
*/

C++

/**
 * @file have1_without23.cpp
 */
#include <iostream>
using namespace std;
int main()
{
    typedef struct
    {
        int x;
        int y;
    };
    return 0;
}
/*报错
missing type-name in typedef-declaration
*/
/**
 * @file have2_without13.cpp
 */
#include <iostream>
using namespace std;
int main()
{
    struct A
    {
        int x = 1;
        int y = 2;
    }; //C++中可以直接在结构体里定义初值
    //C++定义对象A前面不用加struct
    A a = {3, 4};
    cout << "(" << a.x << ","
         << a.y << ")" << endl;
    return 0;
}
//C++中struct与类类似
//区别在于一个默认public
//类默认private
/**
 * @file have3_without12.cpp
 */
#include <iostream>
using namespace std;
int main()
{
    struct
    {
        int x = 1;
        int y = 2;
    } B;
    B = {3, 4};
    //B b;不能如此定义
    //此时只能有B一个对象
    cout << "(" << B.x << ","
         << B.y << ")" << endl;
    return 0;
}
/**
 * @file have12_without3.cpp
 */
#include <iostream>
using namespace std;
int main()
{
    typedef struct A
    //警告,此句中typedef相当于没写
    //'typedef' was ignored in this declaration
    {
        A(int x, int y) : x(x), y(y) {}
        int x = 1;
        int y = 2;
    };
    A a(3, 4);
    //定义构造函数后可以如此定义
    cout << "(" << a.x << ","
         << a.y << ")" << endl;
    return 0;
}
/**
 * @file have13_without2.cpp
 */
#include <iostream>
using namespace std;
int main()
{
    typedef struct
    {
        //B(int x, int y) : x(x), y(y) {}
        //此时只有别名不能定义构造函数
        int x = 1;
        int y = 2;
    } B;
    //C风格乱序定义还是可行
    B b = {.x = 3, .y = 4};
    cout << "(" << b.x << ","
         << b.y << ")" << endl;
    return 0;
}
/**
 * @file have23_without1.cpp
 */
#include <iostream>
using namespace std;
int main()
{
    struct A
    {
        int x = 1;
        int y = 2;
    } B;
    //C++风格乱序定义还是可行
    A a = {x : 3, y : 4};
    cout << "(" << a.x << ","
         << a.y << ")" << endl;
    cout << "(" << B.x << ","
         << B.y << ")" << endl;
    return 0;
}
/**
 * @file have123.cpp
 */
#include <iostream>
using namespace std;
int main()
{
    typedef struct A
    {
        int x = 1;
        int y = 2;
    } B;
    A a = {x : 3, y : 4};
    cout << "(" << a.x << ","
         << a.y << ")" << endl;
    B b = {1, 2};
    cout << "(" << b.x << ","
         << b.y << ")" << endl;
    return 0;
}
/**
 * @file without123.cpp
 */
#include <iostream>
using namespace std;
int main()
{
    struct
    {
        int x = 1;
        int y = 2;
    };
    /*报错
    abstract declarator 'main()
    ::<unnamed struct>' used as declaration
    */
    return 0;
}