Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c - Definition of structures and arrays

I have the following code:

struct dmparam {
    char *p;
    char *v;
};

struct dmobj {
    int a;
    int b;
    struct dmparam *dmparam;
};


struct dmarray {
    struct dmobj *dmobj;
    struct dmentry {
        int func;
        struct dmarray *dmarray;
    } *dmentry;
};

struct dmarray atest = {
    {//objs
    {1, 11, NULL},
    {2, 22,
        {//params
        {"p1", "v1"},
        {"p2", "v2"},
        }//params
    }
    },//objs
    {//arays
        {101, {
                {//objs
                {1, 11, NULL},
                {2, 22,
                    {//params
                    {"p1", "v1"},
                    {"p2", "v2"},
                    }//params
                }
                },//objs
                NULL
        }
        }
    }//arrays
};

This code generate the following compilation warnings:

test.c:26:2: warning: braces around scalar initializer [enabled by default]
  {//objs
  ^
test.c:26:2: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:27:2: warning: braces around scalar initializer [enabled by default]
  {1, 11, NULL},
  ^
test.c:27:2: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:27:2: warning: initialization makes pointer from integer without a cast [enabled by default]
test.c:27:2: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:27:2: warning: excess elements in scalar initializer [enabled by default]
test.c:27:2: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:27:2: warning: excess elements in scalar initializer [enabled by default]
test.c:27:2: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:28:2: warning: braces around scalar initializer [enabled by default]
  {2, 22,
  ^
test.c:28:2: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:28:2: warning: initialization makes pointer from integer without a cast [enabled by default]
test.c:28:2: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:28:2: warning: excess elements in scalar initializer [enabled by default]
test.c:28:2: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:29:3: warning: braces around scalar initializer [enabled by default]
   {//params
   ^
test.c:29:3: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:30:3: warning: braces around scalar initializer [enabled by default]
   {"p1", "v1"},
   ^
test.c:30:3: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:30:3: warning: initialization from incompatible pointer type [enabled by default]
test.c:30:3: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:30:3: warning: excess elements in scalar initializer [enabled by default]
test.c:30:3: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:31:3: warning: braces around scalar initializer [enabled by default]
   {"p2", "v2"},
   ^
test.c:31:3: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:31:3: warning: initialization from incompatible pointer type [enabled by default]
test.c:31:3: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:31:3: warning: excess elements in scalar initializer [enabled by default]
test.c:31:3: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:31:3: warning: excess elements in scalar initializer [enabled by default]
test.c:31:3: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:32:3: warning: excess elements in scalar initializer [enabled by default]
   }//params
   ^
test.c:32:3: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:33:2: warning: excess elements in scalar initializer [enabled by default]
  }
  ^
test.c:33:2: warning: (near initialization for ‘atest.dmobj’) [enabled by default]
test.c:35:2: warning: braces around scalar initializer [enabled by default]
  {//arays
  ^
test.c:35:2: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:36:3: warning: braces around scalar initializer [enabled by default]
   {101, {
   ^
test.c:36:3: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:36:3: warning: initialization makes pointer from integer without a cast [enabled by default]
test.c:36:3: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:36:3: warning: braces around scalar initializer [enabled by default]
test.c:36:3: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:37:5: warning: braces around scalar initializer [enabled by default]
     {//objs
     ^
test.c:37:5: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:38:5: warning: braces around scalar initializer [enabled by default]
     {1, 11, NULL},
     ^
test.c:38:5: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:38:5: warning: initialization makes pointer from integer without a cast [enabled by default]
test.c:38:5: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:38:5: warning: excess elements in scalar initializer [enabled by default]
test.c:38:5: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:38:5: warning: excess elements in scalar initializer [enabled by default]
test.c:38:5: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:39:5: warning: braces around scalar initializer [enabled by default]
     {2, 22,
     ^
test.c:39:5: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:39:5: warning: initialization makes pointer from integer without a cast [enabled by default]
test.c:39:5: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:39:5: warning: excess elements in scalar initializer [enabled by default]
test.c:39:5: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:40:6: warning: braces around scalar initializer [enabled by default]
      {//params
      ^
test.c:40:6: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:41:6: warning: braces around scalar initializer [enabled by default]
      {"p1", "v1"},
      ^
test.c:41:6: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:41:6: warning: initialization from incompatible pointer type [enabled by default]
test.c:41:6: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:41:6: warning: excess elements in scalar initializer [enabled by default]
test.c:41:6: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:42:6: warning: braces around scalar initializer [enabled by default]
      {"p2", "v2"},
      ^
test.c:42:6: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:42:6: warning: initialization from incompatible pointer type [enabled by default]
test.c:42:6: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:42:6: warning: excess elements in scalar initializer [enabled by default]
test.c:42:6: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:42:6: warning: excess elements in scalar initializer [enabled by default]
test.c:42:6: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:43:6: warning: excess elements in scalar initializer [enabled by default]
      }//params
      ^
test.c:43:6: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:44:5: warning: excess elements in scalar initializer [enabled by default]
     }
     ^
test.c:44:5: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:47:3: warning: excess elements in scalar initializer [enabled by default]
   }
   ^
test.c:47:3: warning: (near initialization for ‘atest.dmentry’) [enabled by default]
test.c:47:3: warning: excess elements in scalar initializer [enabled by default]
test.c:47:3: warning: (near initialization for ‘atest.dmentry’) [enabled by default]

What I m doing wrong?

What's the best way to write this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This code compiles as a complete source file:

struct dmparam {
    char *p;
    char *v;
};

struct dmobj {
    int a;
    int b;
    struct dmparam *dmparam;
};

struct dmarray {
    struct dmobj *dmobj;
    struct dmentry {
        int func;
        struct dmarray *dmarray;
    } *dmentry;
};

struct dmparam dp1 = { "abc", "xyz" };
struct dmparam dp2 = { "Mercury", "Uranus" };

struct dmobj do1 = { 1, 1, &dp1 };
struct dmobj do2 = { 2, -987, &dp2 };

struct dmentry de1 = { 0, 0 };

struct dmarray da1 = { &do1, &de1 };

Observe that you effectively have:

struct dmentry {
    int func;
    struct dmarray *dmarray;
};

struct dmarray {
    struct dmobj *dmobj;
    struct dmentry *dmentry;
};

So there are only two items in each struct dmarray, both pointers. Also, the struct dmentry is not restricted in scope; the rules in C++ are different.

All that is standard C90 code (as well as C99 and C11). In C99 or later, you can also do the initialization like this:

struct dmarray da2 =
{
    .dmobj = &(struct dmobj){ 9, 99, &(struct dmparam){ "Frigid", "Roasting" } },
    .dmentry = &(struct dmentry){ 1, &(struct dmarray){ 0, 0 } }
};

Note that you end up with needing a pointer to a struct dmarray while you're initializing another one. I ducked and use a null pointer in the nested structure, but you could go deeper if you wanted to.

Just for the record, the code was tested on Mac OS X 10.10.5 using 'gcc' (actually clang) from XCode 6.4:

$ gcc -std=c99 -pedantic -O3 -g -Wall -Wextra -Werror -c so.32014285.c
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
$

There's an outside chance that despite requesting pedantic adherence to the C99 standard, GCC is letting an extension through.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...