Thread: structure array

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    7

    structure array

    Hey guys - Ive been looking through the archives but have not found what I am looking for. What I want to do is have a mailing list - that is a maximum of 10 entries long (structure array) - It compiles but gives me some warnings in gcc which I will post below.

    ---------------------------------
    discostu@Darkstar:~/c_code/12$ pico 1.c
    discostu@Darkstar:~/c_code/12$ gcc -o 1 1.c
    1.c: In function `main':
    1.c:18: warning: useless keyword or type name in empty declaration
    1.c:29: warning: passing arg 1 of `fgets' makes pointer from integer without a cast
    1.c:40: parse error before `{'
    -----------------------------------------

    when I enter info in it will give a segmentation error once all the fields are filled out for the first time.


    Code:
    /* This program will develop a mailing list that will do no more than be practice for structures */
    
    #include <stdio.h>
    
    
    
    
    int main ()
    
    {
    
    typedef struct list {
        char name[60];
        char address[60];
        char city[30];
        char state[3];
        long int zip;
    };
    
    struct list mailing[10];
    int counter = 0;
    char another;
    int count = 0;
    /*Looping for input */
    
    while (1) {
        if (count > 0) {
            puts("Do you want to add another field? (y/n): ");  /* Checking to see if they want to add another field*/
            fgets(another, sizeof(another), stdin);
    
               if ((another == 'y') || (count > 10)) {
                   break;
           }
       }
        printf("Enter in the name (last, first): ");
            fgets(mailing[count].name, sizeof(60), stdin);
        printf("Enter in the address: ");
            fgets(mailing[count].address, sizeof(60), stdin);
        printf("Enter the city: ");
            fgets(mailing[count].city, sizeof{30), stdin);
        printf("Enter State: ");
            fgets(mailing[count].state, sizeof(3), stdin);
        printf("Enter Zip: ");
            scanf("%d", mailing[count].zip);
       ++count;
    
    }
    
    
    
    /* Loop until where counter left off last time and print data to screen */
    for (counter; counter < count; ++counter) {
      printf("%s\n", mailing[counter].name);
      printf("%s\n", mailing[counter].address);
      printf("%s\n", mailing[counter].city);
      printf("%c, ", mailing[counter].state);
      printf("%d\n", mailing[counter].zip);
      printf("\n");
    }
    
    return(0);
    
    }
    FYI - this isn't homework
    Last edited by disco_stu; 06-03-2002 at 05:28 PM.

  2. #2
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    You don't need to typedef your structure.

    The first argument of fgets() must be a char *.

    (There might be other errors)

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    7
    ok - I did those modifications. Now everytime I run the program it prints out all the printf statements in the loop when I press enter.

    Code:
    char *another;
    int count = 0;
    /*Looping for input */
    
    for(count; count < 10; ++count) {
        if (count > 0) {
            puts("Do you want to add another field? (y/n): ");  /* Checking to see if they want to add another field*/
            fgets(another, sizeof(another), stdin);
                  if ((*another == 'y') || (count > 10)) 
                   break;
           }
    
    
        printf("Enter in the name (last, first): ");
            fgets(mailing[count].name, sizeof(60), stdin);
        printf("Enter in the address: ");
            fgets(mailing[count].address, sizeof(60), stdin);
        printf("Enter the city: ");
            fgets(mailing[count].city, sizeof(30), stdin);
        printf("Enter State: ");
            fgets(mailing[count].state, sizeof(3), stdin);
        printf("Enter Zip: ");
            scanf("%d", &mailing[count].zip);
    Sorry to be so bothersome - it has just been really getting on my nerves. I probobly should have got a book that explained it a bit better

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    To add to what's already been said:

    >fgets(another, sizeof(another), stdin);
    You can't use fgets() to get you one character. The function will only read upto n-1 characters, and in your case n is 1, so 0 characters will be read. Suggest you change another to be an array, then change the following code you use strcmp for comparison testing.

    >fgets(mailing[count].city, sizeof{30), stdin);
    See the highlighted character? It's a {, should be a (.

    >for (counter; counter < count; ++counter)
    Should be
    >for (counter = 0; counter < count; ++counter)

    >sizeof(60)
    This is an incorrect use of sizeof, it is probably giving you 2 not 60 (it's sizeof an int). There's at least couple of ways to fix this:

    - just use 60.
    - Use #define's to make field length variables. Like this
    >#define LEN_CITY 60
    >fgets(mailing[count].city, LEN_CITY, stdin);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > >fgets(another, sizeof(another), stdin);
    > You can't use fgets() to get you one character.
    It's worse than that - another is an uninitialised pointer

    char another[10];
    would be an easy fix

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Salem
    > >fgets(another, sizeof(another), stdin);
    > You can't use fgets() to get you one character.
    It's worse than that - another is an uninitialised pointer
    It wasn't in the first post!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM