嘘~ 正在从服务器偷取页面 . . .

C语言难点——结构体


结构体的声明

举例:图书信息的结构体变量

struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;

访问结构体成员

#include < stdio.h >
#include < string.h >
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

int main (void){
   struct Books Book1;    

   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali");
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   printf( "Book 1 title : %s\n", Book1.title);
   printf( "Book 1 author : %s\n", Book1.author);
   printf( "Book 1 subject : %s\n", Book1.subject);
   printf( "Book 1 book_id : %d\n", Book1.book_id);
} 

结果:

Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407

结构作为函数参数

typedef struct {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
}Books;

void printBook( Books book ) {

   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);
}

int main (void){
   Books Book1;    

   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali");
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   printBook( Book1 );
} 

结构体的指针

typedef struct {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
}Books;

void printBook( Books *book ) {

   printf( "Book title : %s\n", book->title);
   printf( "Book author : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);
}

int main (void){
   Books Book1;    

   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali");
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   printBook( &Book1 );
} 

需要补充一下关于点运算符( . )和箭头运算符( -> )的区别

typedef struct          // 定义一个结构体类型:Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
}Books;       

Books book;              // 声明一个结构体变量
Books *pbook;            // 声明一个指向结构体的指针

book.book_id = 0001;          // 结构体变量通过点运算符( . )访问
pbook->book_id = 0001;        // 指向结构体的指针通过箭头运算符( -> )访问

结构体数组

typedef struct{
    char  title[50];
    char  author[50];
    char  subject[100];
    int   book_id;
}Books;

int main (void){
    Books book[2];
    book[0].book_id = 1;
    book[1].book_id = 2;
    printf("%d\n",book[0].book_id);
    printf("%d\n",book[1].book_id);
}

结构体的内存运算

struct Books {
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
};

int main() {
    printf("%d\n", (int) sizeof(struct Books)); /*50+50+100+4=204*/
    return 0;
}

参考文献

《C语言结构体史上最详细的讲解》

如果有其他问题欢迎留言或邮件提问

QQ:1269112498

Email:1269112498@qq.com

相关文章


文章作者: 刘天翼
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 刘天翼 !
评论
  目录