current position:Home>C language wants to insert new data into an array structure with only three elements, but on the one hand, it is garbled. On the other hand, it cannot judge the value of N and return it

C language wants to insert new data into an array structure with only three elements, but on the one hand, it is garbled. On the other hand, it cannot judge the value of N and return it

2022-02-02 20:21:53 CSDN Q & A

#include<stdio.h>#include<math.h>#include<string.h>#include<ctype.h>#include<stdlib.h>#include<conio.h>struct Cate{    int No;    char Name[30];};int main(){    struct Cate ca[3]=    {        {
   
   
   
   1,"Starter"},        {
   
   
   
   2,"Curries"},        {
   
   
   
   3,"Barbecues"}    };    char str[100]={
   
   
   
   0};    GATE:    printf("Input:");// Type in the new str  Insert it into the structure as a new element and become ca[4]    scanf("%s",str);    int i=3;    strcpy(ca[i+1].Name,str);// Because you may continue to insert , So I used a i    printf("%s\n",ca[i+1]);    int n;    printf("Enter n:");// If n be equal to 1, Return to continue input     scanf("%d",n);    int j=0;    if(n==1)    {        i++;        goto GATE;    }    else// If n It's not equal to 1, Just output all the structural elements     {        for(j=0;j<i;j++)        {        printf("%d\t%s\n",ca[j].No,ca[j].Name);        }    }    return 0;}

img




Refer to the answer 1:

The structure array defined in the code ca[3] Is static , Can't expand , Cannot insert new record .




Refer to the answer 2:



Refer to the answer 3:

If you want to insert a new record , You can choose to overwrite the original , Or apply with dynamic structure




Refer to the answer 4:



Refer to the answer 5:

Need a big data implementation , such as ca【1000】
in addition 29 That's ok ,n Before to add &




Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022021503591.html

Random recommended