explain the brackets in this struct

This is a discussion on explain the brackets in this struct within the C Programming forums, part of the General Programming Boards category; 8 #include <stdio.h> 9 10 const struct dialing_code country_code[] = { 11 {"Argentina", 54}, 12 {"Colombia", 57}, 13 {"Brazil", 55}, ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    explain the brackets in this struct

    8 #include <stdio.h>
    9
    10 const struct dialing_code country_code[] = {
    11 {"Argentina", 54},
    12 {"Colombia", 57},
    13 {"Brazil", 55},
    14 {"Ethiopia", 251},
    15 {"Germany", 49),
    16 {"Indonesia", 62},
    17 {"Italy", 39}
    18 };
    19
    20
    21 main()
    22 {
    23

    if you are to program it so a user enters a country, the program checks to see if it is listed and if so return the country code,
    how do you access the country code (the number in the secondary brackets in the above structure????

    Is Argentina = dialing_code country_code[0] element and it's corresponding dialing code # 54 = element [1] ? I don't get this set up. Have no examples to go by in my book.
    Sue B.

    dazed and confused


  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > struct dialing_code
    Paste this bit as well in particular
    Code:
    struct dialing_code {
        // this bits which are here
    };
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your structure looks like this:

    struct mystruct
    {
    char *country;
    int number;
    };

    Ok, so what you're doing, is deifning a "table". All a "table" is, is an array of structures. Since there is no value in the [], you have a table who's size is unspecified. Thus:
    Code:
    struct mystruct my_table[] =
    {
       { "Japan", 1 },
       { "China", 14 },
       { "CCCP", 3 }
    };
    Usually, when you have a table of undetermined length, you have some way of designating the end. Here is an example of that:
    Code:
    struct mystruct my_table[] =
    {
       { "Japan", 1 },
       { "China", 14 },
       { "CCCP", 3 },
       { "", -1 }
    };
    Naturally there are numerous ways (specific values) to actually denote the end, but this is one such way.

    Then, to display the table, I could do:
    Code:
    for(x=0;my_table[x].number != -1;x++)
       printf("Country: %s, Number: %d.\n",
          my_table[x].country, my_table[x].number );
    Enjoy.

    Quzah.

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    That wont work you need to state the data types of the data items and you cant declare country_code there it should be declared after the closing brace and it should have a number specified for the dimension in this case 7
    Code:
    struct dialing_code{
       char name[10];
       int code;
    }country_code[7];
    
    country_code[0].name = "Argentina";
    country_code[0].code = 54;
    
    and so on
    
    The user's input can be compared like so
    
    if(! strcmp( country_code[0].name, users_choice))
        printf("\nThe code is %d", country_code[0].code);
    else
       printf("\nNot in list");
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > That wont work you need to state the data types of the data
    > items and you cant declare country_code there it

    Actualy, it would work if you did it all on one line, and you do not have to specify an array size:
    Code:
    struct mystruct
    {
       char *country;
       int number;
    } my_table[] =
    {
       { "Japan", 1 },
       { "China", 14 },
       { "CCCP", 3 },
       { "", -1 }
    };
    Quzah.

  6. #6
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    I stand corrected

    Would it be easier just to have 2 array's, one for the name's and one for the code's
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Would it be easier just to have 2 array's, one for the name's
    > and one for the code's

    It would, except if you have to pass them as arguments a number of times. It would get tedious to have to pass them both as seperate arguments.

    myFunction( myStructInstance );

    This is much cleaner.

    Quzah.

  8. #8
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    yuneek

    Hey, I get this a bit better now:



    struct dialing_code{
    char name[10];
    int code;
    }country_code[7];

    country_code[0].name = "Argentina";
    country_code[0].code = 54;

    and so on
    I knew that I had 1 structure and country_code was a variable of that type, but the deal with an array and an integer as part of the members were messing me up.

    Thanks, C-Coder.

    I might get this yet.

    struct my_brain {
    float grey_matter;
    const int struggle_to_get_this_stuff;
    } scrambled_mess;


    Sue B.

    dazed and confused


  9. #9
    Unregistered
    Guest
    #include <stdio.h>

    struct dialing_code {
    char country[15];
    int code;
    };

    const struct dialing_code country_code[]={
    {"Argentina", 54},
    {"Columbia",57}
    };

    main()
    {
    printf("%d"\n", country_code[0].code);
    return 0;
    }

    The above program would return '54'. Hopefully this layout makes it easier for you to see the logic. Good luck.

  10. #10
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Here's what I got so far:



    Code:
    #include <stdio.h>
    
    
    /****** COUNTRY needs to be pointer as instructions demonstrate ********/
    struct dialing_code {
       char *country;
       int code;
    };
    
    main()
    {
    
       const struct dialing_code country_code[] = {
             {"Argentina",      54},
             {"Bangladesh",    880},
             {"Brazil",         55},
             {"China",          86},
             {"Colombia",       57},
             {"Egypt",          20},
             {"Ethiopia",      251},
             {"France",         33},
             {"Germany",        49},
             {"India",          91},
             {"Indonesia",      62},
             {"Iran",           98},
             {"Italy",          39},
             {"Japan",          81},
       };
    
       printf("Program will give country dialing code\n");
       printf("Input a country name to look up code : ");
       
    /********* user input : country name ************/
    
       scanf(" %s",country_code.country);
    
    /****** check for NULL or empty string **********/
    
       if (country_code.country != \0)   {
    
    
    /*******  loop to check each element of structure array ******/
    
              for (i=
    
    /****** what is proper call of print function to print out code of user input **********/
    
       printf("The dialing code of %s is %d",country_code,
                            country_code.dialing_code.code);
    
    
    
    }
    Sue B.

    dazed and confused


  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct dialing_code {
       char *country;
       int code;
    };
    
    int main() {
        const struct dialing_code country_code[] = {
            {"Argentina",      54},
            {"Bangladesh",    880},
            {"Brazil",         55},
            {"China",          86},
            {"Colombia",       57},
            {"Egypt",          20},
            {"Ethiopia",      251},
            {"France",         33},
            {"Germany",        49},
            {"India",          91},
            {"Indonesia",      62},
            {"Iran",           98},
            {"Italy",          39},
            {"Japan",          81},
        };
        char find_country[100];
        int i;
        int num_codes = 14;
    
        printf("Program will give country dialing code\n");
        printf("Input a country name to look up code : ");
        scanf( "%s", find_country );
    
        for ( i = 0 ; i < num_codes ; i++ ) {
            if ( strcmp( find_country, country_code[i].country) == 0 ) {
                printf( "Code for %s is %d\n",
                    country_code[i].country, country_code[i].code );
                break;
            }
        }
    
        return 0;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    country_code[0].name = "Argentina";
    This statement that you made above is illegal because a char array can only be assigned a string literal when an instance of the structure is defined such as:

    const struct dialing_code country_code[] = {
    {"Argentina", 54} };

    This may sound confusing but after the structure is defined and you want to assign a string to a char array member you must use a function like strcpy.

    Code:
    strcpy(country_code[0].name,"Argentina");
    You must add this library header file to the top of your code: #include<string.h>

    Forgot to say that you have a constant structure so that no changes can be made to it. But this would apply otherwise.

  13. #13
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Salem:

    code works beautifully, but how do I
    print error message when user inputs a country
    name that is not listed ??

    Another loop, I guess? nested in the first for loop??
    Sue B.

    dazed and confused


  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > but how do I print error message when user inputs a country
    > name that is not listed ??

    Well for starters, you could give it more than a few seconds
    thought, look at the current code, and simply add in a statement
    for "oops, no error found" when there isn't one.

    Since you don't want to put any thought into it, I'll do it for you:
    Code:
    for ( i = 0 ; i < num_codes ; i++ ) {
            if ( strcmp( find_country, country_code[i].country) == 0 ) {
                printf( "Code for %s is %d\n",
                    country_code[i].country, country_code[i].code );
                break;
            }
    }
    if ( i >= num_codes )
    {
       puts("Country not found.");
    }
    Very simple. You need to learn how to break down what you want
    done into simple steps.

    Quzah.

  15. #15
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok, I was extremely tired last night. Not feeling well.
    Thanks Quzah for giving me a little boost of help.

    now I would like the program to continue on until user
    inputs something (like DONE)

    I am so confused about where to put the loop in and which loop is best for different situations

    Help give me an idea.

    Code:
        
    
    #include <stdio.h>
    
    struct dialing_code {
       char *country;
       int code;
    };
    
    main()
    {
    
       const struct dialing_code country_code[] = {
             {"Argentina",      54},
             {"Bangladesh",    880},
             {"Brazil",         55},
             {"China",          86},
             {"Colombia",       57},
             {"Egypt",          20},
             {"Ethiopia",      251},
             {"France",         33},
             {"Germany",        49},
             {"India",          91},
             {"Indonesia",      62},
             {"Iran",           98},
             {"Italy",          39},
             {"Japan",          81},
       };
    
        char find_country[100];
        int i;
        int num_codes = 14;
    
    
       printf("Program will give country dialing code\n");
       printf("Input a country name to look up code : ");
       scanf(" %s",find_country);
    
    /* would i put an if statement here 
        
        if (find_country = "DONE")
             return ;    <-- is this correct to skip to the end and stop.
        else   {       }  around the for and and if statement below
    
        and then insert a line like
    
       printf("If done checking for country dialing code "
                 "type DONE.");
    
                 ^----- Where do I put this??
    */
    
       for ( i = 0 ; i < num_codes ; i++ ) {
          if (strcmp(find_country,country_code[i].country) == 0)
    
            {
               printf( "Code for %s is %d\n",
                 country_code[i].country, country_code[i].code);
                break;
            }
       }
    
       if ( i >= num_codes )   {
            puts("Country not found.");
       }
    
     return 0;
    
    }
    Sue B.

    dazed and confused


Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21