-1
typedef struct mobiltelefon {
    char herstellername[HLEN];
    double displaydiagonale;
    aufloesung_t aufloesung;
    char standards[NUMBER_OF_STRINGS][STRINGLENGTH+1] = {"GPRS", "EDGE", "HSDPA", "HSUPA", "HSPA",
"LTE"};
} telefon_t;

I keep getting an error expected ; at the end of declaration list.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
damir46
  • 57
  • 1
  • 2
  • 7
  • Check this out http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c – NMK Oct 06 '14 at 16:59
  • 3
    Did you mean `char *standards[] = {"GPRS", "EDGE", ...};`? – Kninnug Oct 06 '14 at 17:00
  • Do you want two-dimensional array of `char`, or array of `char*`? Both are "arrays of strings" in C (which does not actually have strings in the sense most other languages have them). – hyde Oct 06 '14 at 17:09
  • 1
    @damehanicar, something you may find that would work better is to ask a new question (with the revisions you have now) and delete this question altogether. People have answered what they thought was the original question however there was a significant piece missing. – Michael Petch Oct 06 '14 at 17:45

5 Answers5

1

Change

char (*standards[6])[5] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};

to

char standards[6][6] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};

A quick example:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;
    char standards[6][6] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};
    for(i=0;i<6;i++)
        printf("%s\n",standards[i]);
    return 0;
}

Kindly note char standards[6][5] is wrong in your case as the longest string in your 2D array is HSDPA and HSUPA which is of length 5, you will need one more byte for terminating '\0' char.

ani627
  • 5,578
  • 8
  • 39
  • 45
  • Nope, I keep getting the same error – damir46 Oct 06 '14 at 17:08
  • @damehanicar: [Check here](http://coliru.stacked-crooked.com/a/c181166678715a8e) – ani627 Oct 06 '14 at 17:13
  • The OP has amended the question. What is now different (and significant) is that not only are they trying to initialize the array, but it was also inside of a struct. They didn't realize that the fact it was also in a `struct` mattered. – Michael Petch Oct 06 '14 at 17:36
0

Remove the * and the {}. Change this:

char (*standards[6])[5] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};

To this:

char standards[6][6] = {"GPRS", "EDGE", "HSDPA", "HSUPA", "HSPA", "LTE"};

To the struct:

You cant initialize a struct member within its declaration. You need to do something like this:

typedef struct mobiltelefon {
    char herstellername[HLEN];
    double displaydiagonale;
    aufloesung_t aufloesung;
    char standards[NUMBER_OF_STRINGS][STRINGLENGTH+1];
} telefon_t;

And the initialize the variable like this (C99/C11 only):

telefon_t iphone = {.standards = {"GPRS", "EDGE", "HSDPA", "HSUPA", "HSPA", "LTE"}};

Or with memcpy:

memcpy(iphone.standards[0], "GPRS", 5);
memcpy(iphone.standards[1], "EDGE", 5);
memcpy(iphone.standards[2], "HSDPA", 6);
memcpy(iphone.standards[3], "HSUPA", 6);
memcpy(iphone.standards[4], "HSPA", 5);
memcpy(iphone.standards[5], "LTE", 4);

But this is a waste of space, if each phone has the same standards I would rather go with a global variable:

char teleon_standards[6][6] = {"GPRS", "EDGE", "HSDPA", "HSUPA", "HSPA", "LTE"};
Marco
  • 7,007
  • 2
  • 19
  • 49
  • 1
    The OP has amended the question. What is now different (and significant) is that not only are they trying to initialize the array, but it was also inside of a struct. They didn't realize that the fact it was also in a `struct` mattered. – Michael Petch Oct 06 '14 at 17:37
0

you can use 2D arrays if you know the maximum lenght of the strings you will be putting in it for example if we know that no string will be over 4 characters long then just do

char arr[6][5] = {"GPRS" , "EDGE" , "HSDPA" , ...};

a better way of doing this is

char *arr[] = { "GPRS" , "EDGE" , "HSPDA"}

this way you don't have to worry about the lenght of the strings or how many strings you can add

Farouq Jouti
  • 1,657
  • 9
  • 15
0

One way to initialize an array of strings is:

char *standards[] = {"GPRS", ...};

You don't have to pass the number of strings, compiler will figure that out.

mrk
  • 3,061
  • 1
  • 29
  • 34
0

Variables in a structure cannot be initialized.Apparently standards can't be initialized inside the structure.

danish
  • 359
  • 1
  • 8