C语言结构体数组如何实现非传统赋值方式?

2026-05-08 16:483阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计543个文字,预计阅读时间需要3分钟。

C语言结构体数组如何实现非传统赋值方式?

在C语言中,结构体数组的元素可以同时赋值。以下是一个示例代码:

c#include

struct Student { char name[50]; int age; float score;};

int main() { struct Student students[2]={ {Alice, 20, 85.5}, {Bob, 21, 90.2} };

printf(Student 1: %s, %d, %.1f\n, students[0].name, students[0].age, students[0].score); printf(Student 2: %s, %d, %.1f\n, students[1].name, students[1].age, students[1].score);

return 0;}在这个例子中,我们定义了一个结构体`Student`,它包含三个成员:`name`、`age`和`score`。然后我们创建了一个结构体数组`students`,包含两个`Student`类型的元素。在声明时,我们同时给这两个元素赋了初值。

说到C语言结构体数组的同时赋值,许多人一想就会想到用以下的这种方法,咱们来写一个例子:

#include <stdio.h> struct student { int a; int b ; int c ; }; struct student array1[1000] ; int main(void) { int i ; for(i = 0 ; i < 1000 ; i++) { array[i].a = 1 ; array[i].b = 2 ; array[i].c = 3 ; } for(i = 0 ; i < 1000 ; i++) { printf("array[%d].a:%d array[%d].b:%d array[%d].c:%d \n" , i, array[i].a ,i, array[i].b ,i, array[i].c); } return 0 ; }

这样就可以实现对结构体数组同时赋值了。

阅读Linux内核源代码的时候看到了,原来C语言还有一种更少人知道的方法,使用 "..." 的形式,这种形式是指第几个元素到第几个元素,都是一样的内容。这种用法在标准C上也是允许的,没有语法错误,我们来看看它是怎么用的:

#include <stdio.h> struct student { int a; int b ; int c ; }; //对第0个数组到第999个结构体数组同时赋值一样的内容 struct student array[1000] = { [0 ... 999] = { .a = 1 , .b = 2 , .c = 3 , } }; int main(void) { int i ; //输出赋值后的数值 for(i = 0 ; i < 1000 ; i++) { printf("array[%d].a:%d array[%d].b:%d array[%d].c:%d \n" , i, array[i].a ,i, array[i].b ,i, array[i].c); } return 0 ; }

总结

C语言结构体数组如何实现非传统赋值方式?

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对自由互联的支持。如果你想了解更多相关内容请查看下面相关链接

标签:另类用法

本文共计543个文字,预计阅读时间需要3分钟。

C语言结构体数组如何实现非传统赋值方式?

在C语言中,结构体数组的元素可以同时赋值。以下是一个示例代码:

c#include

struct Student { char name[50]; int age; float score;};

int main() { struct Student students[2]={ {Alice, 20, 85.5}, {Bob, 21, 90.2} };

printf(Student 1: %s, %d, %.1f\n, students[0].name, students[0].age, students[0].score); printf(Student 2: %s, %d, %.1f\n, students[1].name, students[1].age, students[1].score);

return 0;}在这个例子中,我们定义了一个结构体`Student`,它包含三个成员:`name`、`age`和`score`。然后我们创建了一个结构体数组`students`,包含两个`Student`类型的元素。在声明时,我们同时给这两个元素赋了初值。

说到C语言结构体数组的同时赋值,许多人一想就会想到用以下的这种方法,咱们来写一个例子:

#include <stdio.h> struct student { int a; int b ; int c ; }; struct student array1[1000] ; int main(void) { int i ; for(i = 0 ; i < 1000 ; i++) { array[i].a = 1 ; array[i].b = 2 ; array[i].c = 3 ; } for(i = 0 ; i < 1000 ; i++) { printf("array[%d].a:%d array[%d].b:%d array[%d].c:%d \n" , i, array[i].a ,i, array[i].b ,i, array[i].c); } return 0 ; }

这样就可以实现对结构体数组同时赋值了。

阅读Linux内核源代码的时候看到了,原来C语言还有一种更少人知道的方法,使用 "..." 的形式,这种形式是指第几个元素到第几个元素,都是一样的内容。这种用法在标准C上也是允许的,没有语法错误,我们来看看它是怎么用的:

#include <stdio.h> struct student { int a; int b ; int c ; }; //对第0个数组到第999个结构体数组同时赋值一样的内容 struct student array[1000] = { [0 ... 999] = { .a = 1 , .b = 2 , .c = 3 , } }; int main(void) { int i ; //输出赋值后的数值 for(i = 0 ; i < 1000 ; i++) { printf("array[%d].a:%d array[%d].b:%d array[%d].c:%d \n" , i, array[i].a ,i, array[i].b ,i, array[i].c); } return 0 ; }

总结

C语言结构体数组如何实现非传统赋值方式?

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对自由互联的支持。如果你想了解更多相关内容请查看下面相关链接

标签:另类用法