Thread: Need help with structures

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    16

    Need help with structures

    Code:
    #include <stdio.h>
    
    struct worker {
      char name[25];
      char info[250];
      char number[3];
    };
    
    int main()
    {
      FILE *fp;
      int num,x;
    
      fp=fopen( "workerinfo.txt", "w" );
      printf( "How many workers?\n");
      scanf( " %d", &num );
      struct worker guy[num];
      for( x = 0; x <= num; x++ ) {
       printf( "Worker%d info\nName:", x+1 ); 
       scanf( " %s", guy[x].name );
       printf( "Info:" );
       scanf( " %s", guy[x].info );
       printf( "Worker number:" );
       scanf( " %s", guy[x].number );
      }
      for( x = 0; x <= num; x++ ) {
       printf( "\n\n\n\nWorker name:%s\nWorker number:%s\nWorker info:%s\n", guy[x].name, guy[x].number, guy[x].info );
       getchar();
      }
      return 0;
    }

  2. #2

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > struct worker guy[num];
    Now is this about not being able to declare a variable inline?

    > for( x = 0; x <= num; x++ )
    Or the fact that you're running off the end of the array?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures as members of structures & offset
    By trish in forum C Programming
    Replies: 24
    Last Post: 07-16-2011, 05:46 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM