0

Why would this not work:

#define PORT_ID_MAX_CHAR                6

typedef struct  {
    int phys;
    char name[PORT_ID_MAX_CHAR];
}tPortMap;

struct tPortMap c_portMap[] = { 0, "test" }, { 1,"test" };

GCC barks at me saying myfile.c:8:46: error: expected identifier or ‘(’ before ‘{’ token struct tPortMap c_portMap[] = { 0, "test" }, { 1,"test" }; and I don't know why... I'm puzzled...

EDIT1

With extra braces I get the error: struct tPortMap c_portMap[] = {{ 0, "test" }, { 1,"test" }};

myfile.c:8:17: error: array type has incomplete element type struct tPortMap c_portMap[] = {{ 0, "test" }, { 1,"test" }};

8
  • 2
    does struct tPortMap c_portMap[] = {{ 0, "test" }, { 1,"test" }}; solve the problem? Commented Mar 14, 2016 at 18:33
  • I almost missed that 6 ... Commented Mar 14, 2016 at 18:34
  • Please see EDIT1 above Commented Mar 14, 2016 at 18:35
  • 2
    It's not struct tPortMap, it's just tPortMap Commented Mar 14, 2016 at 18:36
  • Yes I do, it's all in the same file. Commented Mar 14, 2016 at 18:36

2 Answers 2

2

You need another pair of braces that surround the data for the elements of the array.

Also, you don't need to use struct tPortMap since you have already typedefed tPortMap.

tPortMap c_portMap[] = { { 0, "test" }, { 1,"test" } };
                       ^^                            ^^

When you use

struct tPortMap c_portMap[] = { { 0, "test" }, { 1,"test" } };

the compiler thinks you are declaring a new struct, which obviously is not complete.

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

1 Comment

Please see EDIT1 above
0

Try this instead:

#include <stdio.h>
#define PORT_ID_MAX_CHAR                6

typedef struct tPortMap {
    int phys;
    char name[PORT_ID_MAX_CHAR];
}tPortMap;

int main(void)
{
    tPortMap c_portMap[] = { { 0, "test" }, { 1,"test" } };

    printf("%s\n", c_portMap[0].name);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.