首页  

c++ 结构体例子     所属分类 c 浏览量 305
#include "stdio.h"

struct Student{
    const char *name;
    int age;
    float score;
};

void display(struct Student stu){
    printf("name=%s,age=%d,score=%f\n", stu.name, stu.age, stu.score);
}

int main(){
    struct Student student1;
    
    student1.name = "tiger";
    student1.age = 7;
    student1.score = 92.5;
    
    display(student1);
    return 0;
}

const char *name;
注意 加上 const,否则编译警告信息
 warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
    student1.name = "tiger";

上一篇     下一篇
C C++ 宏定义 # 与 ##

预定义宏 __cplusplus

c/c++标准预定义宏

c++ 面向对象 例子

C++11 智能指针

C++类成员初始化