Thread: My program is giving me an error when I tried to run it.

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    6

    My program is giving me an error when I tried to run it.

    When I ran my program I'm getting the following Error: assignment to expression with array type

    Can someone help me with this error?
    I'm new to data structure.

    Problem:
    I'm trying to create a C program to find the average life expectancy for all High-income countries in the region Latin America & Caribbean between the periods of 1960 to 1964 and state which of those countries(s) has the greatest average of life expectancy during that stated period. The data was gathered from an excel spreadsheet.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        struct Countries {
        int years;
        char *countryName[100];
        char *countryCode[5];
        char *region[200];
        char *incomeGroup[100];
        float lifeExpectancy;
        };
    
    int main()
    {
        int i = 0, n = 16;
    
        struct Countries countries[n];
            countries.countryName = "Curaçao";
            countries.countryCode = "CUW";
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Cayman Islands";
            countries.countryCode = "CYM"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "St. Kitts and Nevis";
            countries.countryCode = "KNA"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "St. Martin (French part)";
            countries.countryCode = "MAF"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Panama";
            countries.countryCode = "PAN"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Puerto Rico";
            countries.countryCode = "PRI"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Sint Maarten (Dutch Part)";
            countries.countryCode = "SXM"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Turks and Caicos Islands";
            countries.countryCode = "TCA"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Trinidad and Tobago";
            countries.countryCode = "TTO"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "British Virgin Islands";
            countries.countryCode = "VGB"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Virgin Islands";
            countries.countryCode = "VIR"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Chile";
            countries.countryCode = "CHL"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Barbados";
            countries.countryCode = "BRB"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Barbados";
            countries.countryCode = "BRB"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Bahamas, The";
            countries.countryCode = "BHS"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Antigua and Barbuda";
            countries.countryCode = "ATG"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            countries.countryName = "Aruba";
            countries.countryCode = "ABW"
            countries.region = "Latin American & Caribbean";
            countries.incomeGroup = "High Income";
            countries.lifeExpectancy = "No data";
    
            printf(Country List\n\n");
        for (i = 0; i < n; i++) {
            printf("Country Name %s\n", countries.countryName);
            printf("Country Code %s\n", countries.countryCode);
            printf("Region %s\n", countries.region);
            printf("Income Group %s\n", countries.incomeGroup);
            printf("Country Name %.2f\n", countries.lifeExpectancy);
        }
    
    
        return 0;
    }
    Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    Code:
        char *countryName[100];
        char *countryCode[5];
        char *region[200];
        char *incomeGroup[100];
    You either want
    Code:
        char *countryName;
        char *countryCode;
        char *region;
        char *incomeGroup;
    or
    Code:
        char countryName[100];
        char countryCode[5];
        char region[200];
        char incomeGroup[100];
    I'd recommend the former for what you have in the rest of the code.


    Code:
    countries.countryName = "Curaçao";
    ..
    countries.countryName = "Cayman Islands";
    You have an array, so you need subscripts.
    Code:
    countries[0].countryName = "Curaçao";
    ..
    countries[1].countryName = "Cayman Islands";
    > countries.lifeExpectancy = "No data";
    This is a float, not a string.

    You should have made sure just one or two elements worked, before copy/pasting the same mistake 16 times.
    A development process
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,786
    The error means that you tried to assign to an array. That is not allowed in C. You can initialise an array with an aggregate, e.g.,
    Code:
    int numbers[] = {1, 2, 3};
    But once the array exists, you cannot assign to it, so this would give you an error similiar to what you encountered:
    Code:
    numbers = {1, 2, 3};
    Going back to your code, I think you have another problem. Let's take a look at:
    Code:
    struct Countries {
    int years;
    char *countryName[100];
    char *countryCode[5];
    char *region[200];
    char *incomeGroup[100];
    float lifeExpectancy;
    };
    Firstly, struct Countries makes it sound like this struct represents many countries, but it looks like you want it to represent just one country. I would therefore rename it to struct Country. Next, countryName sounds like it should be a string, which means that it would either be an array of char, of a pointer to char that would point to the first character of an array of char. But you declared countryName is be an array of pointers to char. The same problem occurs for countryCode, region, and incomeGroup. Consequently, I suggest:
    Code:
    struct Country {
        int years;
        char countryName[100];
        char countryCode[5];
        char region[200];
        char incomeGroup[100];
        float lifeExpectancy;
    };
    This way, you can solve your problem of being unable to assign to arrays by using say, strcpy instead of assignment (but note that strcpy does not do bounds checking, so you should only use it when you know the destination array is large enough to store the string that is to be copied, which is a reasonable assumption in this case since you are setting initial values with string literals).

    Another possibility for your case is to simply initialise countries, which is likely to be a better approach.

    Note that you have declared countries to be a variable length array. If this was not intended, you should change n to be constant (defined by a macro or enum), and rename it to something more descriptive, e.g., COUNTRIES_MAX_SIZE.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 10-01-2012, 08:57 PM
  2. Compiler giving error
    By Nathan the noob in forum C++ Programming
    Replies: 2
    Last Post: 06-26-2008, 06:55 AM
  3. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM
  4. RegQueryValueEx Giving error 234
    By Narcose in forum C Programming
    Replies: 4
    Last Post: 07-14-2006, 10:16 AM
  5. WSAGetLastError() not giving error
    By Hunter2 in forum Networking/Device Communication
    Replies: 1
    Last Post: 08-04-2003, 01:03 PM

Tags for this Thread