-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.

4
  • Check this out stackoverflow.com/questions/1088622/… Commented Oct 6, 2014 at 16:59
  • 3
    Did you mean char *standards[] = {"GPRS", "EDGE", ...};? Commented Oct 6, 2014 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). Commented Oct 6, 2014 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. Commented Oct 6, 2014 at 17:45

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Nope, I keep getting the same error
@damehanicar: Check here
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.
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

Comments

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.

1 Comment

Did you even read other answers (the same answers) before you posted your?
0

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

Comments

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"};

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.