2

I'm using visual studio 13. I just declared and initialized an unsigned long array.

 unsigned long z[8] = { 0xffffffff, 0x00000001, 0x00000000, 0x00000000,0x00000000, 0xffffffff, 0xffffffff, 0xffffffff }; 

But it throws the following error

error C2536: : cannot specify explicit initializer for arrays

I read some answers related to this error but its not helping me. Can somebody help me solve this error?

vsoftco
  • 55,410
  • 12
  • 139
  • 252
abejoe
  • 153
  • 2
  • 9
  • 2
    Is that supposed to be class member? If yes, see http://stackoverflow.com/questions/27882915/c-cannot-specify-explicit-initializer-for-arrays. If not, show more relevant code. – R Sahu Nov 12 '15 at 04:59
  • 1
    @R Sahu Its a member of a structure. – abejoe Nov 12 '15 at 05:02
  • I guess, it was addressed to you :) @RSahu – Yeldar Kurmangaliyev Nov 12 '15 at 05:02
  • Post a complete example of your code so we can see exactly what's going on. – Anon Mail Nov 12 '15 at 05:03
  • 1
    @vsoftco My program is in c. All the examples shown are in C++ with classes. How the hell would it help me solve my problem in C with structures? – abejoe Nov 12 '15 at 05:31
  • @vsoftco - You shouldn't do unjustice to OP by tagging duplicate of C++ question to a C question!!! Please reopen it. – Am_I_Helpful Nov 12 '15 at 05:32
  • @Am_I_Helpful The question was tagged C++ initially though (as you can see from the edit history), otherwise I wouldn't have been able to close it. It was changed to `C` after I close it. – vsoftco Nov 12 '15 at 05:33
  • @abejoe You should have mentioned this when asking the question. You tagged it as C++. In any case, I voted to reopen. And please use a civilized tone. – vsoftco Nov 12 '15 at 05:34
  • @Anon Mail Sorry I'm not allowed to post my entire code. Will you be able to help me out with just the information i give you? – abejoe Nov 12 '15 at 05:39
  • @ vsoftco Chill...Tat c++ tag was a mistake..before I could realize it, u marked it as duplicate! – abejoe Nov 12 '15 at 05:43
  • @abejoe All right, I voted to reopen it. I cannot reopen now by myself since it is tagged C. Will tag it C++ again, reopen it, and delete the tag. Darn, still doesn't work. – vsoftco Nov 12 '15 at 05:44
  • @abejoe: You cannot specify initializers in structure declarations. It does not matter whether it is an array or anything else. There's simply no such thing as an "initializer" in that context. What do you expect it to initialize? – AnT stands with Russia Nov 12 '15 at 05:57

1 Answers1

1

In a "natural" context (i.e. an array object declaration) you declaration with initialization is perfectly valid. There's nothing wrong with it.

The only explanation for the error is that, as you noted in the comments, your array is declared as a struct member and you are trying to specify the initializer right in the struct declaration. Doing something like that just does not make any sense in C. You cannot specify initializers for members in struct declarations, regardless of whether these members are arrays or something else.

Initializers are supposed to be specified in object definitions, as in

struct S 
{
  unsigned long z[8];
};

struct S s = { { 0xffffffff, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0xffffffff, 0xffffffff, 0xffffffff } };
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • What if I need to define multiple arrays with different names under the same structure? I mean how to do that? – abejoe Nov 12 '15 at 06:43