logo

C Conjunto de estructuras

¿Por qué utilizar una variedad de estructuras?

Considere un caso en el que necesitamos almacenar los datos de 5 estudiantes. Podemos almacenarlo utilizando la estructura que se indica a continuación.

cómo obtener emojis de iPhone en Android
 #include struct student { char name[20]; int id; float marks; }; void main() { struct student s1,s2,s3; int dummy; printf('Enter the name, id, and marks of student 1 '); scanf('%s %d %f',s1.name,&s1.id,&s1.marks); scanf('%c',&dummy); printf('Enter the name, id, and marks of student 2 '); scanf('%s %d %f',s2.name,&s2.id,&s2.marks); scanf('%c',&dummy); printf('Enter the name, id, and marks of student 3 '); scanf('%s %d %f',s3.name,&s3.id,&s3.marks); scanf('%c',&dummy); printf('Printing the details....
'); printf('%s %d %f
',s1.name,s1.id,s1.marks); printf('%s %d %f
',s2.name,s2.id,s2.marks); printf('%s %d %f
',s3.name,s3.id,s3.marks); } 

Producción

 Enter the name, id, and marks of student 1 James 90 90 Enter the name, id, and marks of student 2 Adoms 90 90 Enter the name, id, and marks of student 3 Nick 90 90 Printing the details.... James 90 90.000000 Adoms 90 90.000000 Nick 90 90.000000 

En el programa anterior, hemos almacenado datos de 3 estudiantes en la estructura. Sin embargo, la complejidad del programa aumentará si hay 20 estudiantes. En ese caso, tendremos que declarar 20 variables de estructura diferentes y almacenarlas una por una. Esto siempre será difícil ya que tendremos que declarar una variable cada vez que agreguemos un estudiante. Recordar el nombre de todas las variables también es una tarea muy complicada. Sin embargo, c nos permite declarar una matriz de estructuras mediante las cuales podemos evitar declarar las diferentes variables de estructura; en cambio, podemos hacer una colección que contenga todas las estructuras que almacenan la información de diferentes entidades.

Matriz de estructuras en C

Una serie de estructuras en C Se puede definir como la colección de variables de múltiples estructuras donde cada variable contiene información sobre diferentes entidades. la matriz de estructuras en C se utilizan para almacenar información sobre múltiples entidades de diferentes tipos de datos. El conjunto de estructuras también se conoce como colección de estructuras.

c conjunto de estructuras

Veamos un ejemplo de un arreglo de estructuras que almacena información de 5 estudiantes y la imprime.

 #include #include struct student{ int rollno; char name[10]; }; int main(){ int i; struct student st[5]; printf(&apos;Enter Records of 5 students&apos;); for(i=0;i<5;i++){ printf('
enter rollno:'); scanf('%d',&st[i].rollno); name:'); scanf('%s',&st[i].name); } printf('
student information list:'); for(i="0;i&lt;5;i++){" printf('
rollno:%d, name:%s',st[i].rollno,st[i].name); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Enter Records of 5 students Enter Rollno:1 Enter Name:Sonoo Enter Rollno:2 Enter Name:Ratan Enter Rollno:3 Enter Name:Vimal Enter Rollno:4 Enter Name:James Enter Rollno:5 Enter Name:Sarfraz Student Information List: Rollno:1, Name:Sonoo Rollno:2, Name:Ratan Rollno:3, Name:Vimal Rollno:4, Name:James Rollno:5, Name:Sarfraz </pre> <hr></5;i++){>