-2

I want to take a number input from the user. I want it to work as a user defined array of structure.

    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    
    int main(void)
    {
         struct Data
         {
             char Name[10];
             int Age;
             char Gender;
         
         };
            int i, n;
            struct Data D[n]; 
               
            printf(" \n enter the number of user : ");
            scanf("%d",&n);
    
            printf("enter the User details :");
    
            for(i=0;i<n;i++)
            scanf("%s" "%d" "%c",D[i].Name,&D[i].Age,&D[i].Gender);

    return 0;
            
     }
Rob
  • 14,746
  • 28
  • 47
  • 65

1 Answers1

1

Try using this instead , if you want to declare an array with unknown number of parts , you must read the n value from the user first , and then procced in your programm , quick note : " The struct declaration is an alternatif you can use but the one you used is also correct " , hope I helped you ;)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


typedef struct
     {
         char Name[10];
         int Age;
         char Gender;

     }Data;


int main()
{

        int i, n;


        printf(" \n enter the number of users : ");
        scanf("%d",&n);

        Data D[n];

        printf("enter the users details ....\n\n");

        for(i=0;i<n;i++){
        printf("Enter the user number %d informations : \n",i+1);
        scanf("%s %d %c",D[i].Name,&D[i].Age,&D[i].Gender);
        }

return 0;

 }