2

I know, that C allow I do it.

char *array[] = {"String1", "String2",...};

but I wanna do it.

char **array or char *array[3]; array = {"String1", "String2"...};

Because I think that using a loop to fullfill a array is very bad instead initializing like a showed.

Thanks.

3
  • array[0] = "Foo"; array[1] = "Bar"; array[2] = "Qux"; etc? Using a loop isn't bad unless you care about microseconds. Commented Feb 28, 2011 at 16:45
  • possible duplicate of Initializing an array after declaration Commented Feb 28, 2011 at 16:55
  • Of course not, it is about struct too, and this question is got a lot of interesting answers. Commented Feb 28, 2011 at 17:02

5 Answers 5

3

No -- the initialization syntax only works for initialization, not assignment, so it must be part of the array definition, not afterwards. Afterwards, what you'd have would be assigning an array instead of truly initializing it, and C does not support array assignment.

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

Comments

2

Short answer: no, you can't.

When initialising a non-static array like this, the compiler has to generate code to copy the initial state of the array out of storage and into the new array. (Because it's a new array every time you call the function.) It's not actually much slower to write code to do this yourself.

Long answer: if you're willing to be evil, you can sort of do it, in C99, by wrapping the array in a struct and using structure assignment...

struct arrayWrapper
{
  char* array[4];
};

{
  struct arrayWrapper d;
  ...
  d = (struct arrayWrapper){{1, 2, 3, 4}};
}

(syntax may not be quite right.)

But that's evil, and only works in C99, so don't do it, mmm'kay?

6 Comments

I didn't understand the cast, why it is necessary? I never saw it before.
It's not a cast, it's part of the new 'compound literal' syntax. See this Dr.Dobbs article. C99 is awesome, there's lots of cool stuff in it...
This answered the question, but it doesn't seem like a good idea.
I did warn the OP it was evil!
Because this is a cunning coding trick, and cunning code tricks are usually a sign that you actually want to solve the problem in a different way. Structure initialisation like this actually compiles into a memcpy, copying the array data out of read-only storage and onto the stack. If the problem can be rephrased using pointers, you may be able to refer to the data in read-only storage directly --- which could result in faster, smaller, cleaner, more reliable and more understandable code! But, of course, it all depends what the problem actually is. This trick can be dead handy in its place.
|
2

As @Jerry says, you cannot do this for an array. Your question mentions structures, and you can do it for a structure. e.g.

struct mystruct {
   int a;
   int b;
};

struct mystruct mystruct;

mystruct = (struct mystruct){1, 2};

will assign mystruct.a to 1 and mystruct.b to 2

2 Comments

Okey, and if I use malloc to alloc mem for the array, can I use the same syntax? And why you made a cast to (struct mystruct)? I never saw it before.
@drigo, You can't assign an array after initialisation. The (struct mystruct){1, 2} is not a cast, although it looks like one. It is something called a compound literal - a way of defining a literal value for a compound type.
1

Concepts at work here: modifiable lvalue and compound literals vs. initializers.

An array is not considered a modifiable lvalue, and so the following will never work:

int i[];
int j[];
i = j;

This includes the case where the right-hand side of the assignment is a compound literal like you are using.

Compound literals look a lot like initializers but they are two different concepts with similar syntax. Initializers appear with definitions, whereas compound literals can appear in arbitrary expressions.

Since a pointer is a modifiable lvalue, you can assign a compound literal to it:

char** array;
array = (char* []) {"zero", "one", "two"};

2 Comments

Glad it helped. By the way, you can change the correct answer even if you've already assigned it once.
Yes, I got awesome answers, And ur one is perfect, but David given helped me a lot, cause he was the first to turn the tides to say something correct and different, he also gave me a lesson about compound literal, giving me the Dr.Dobbs article link, Sorry for can't choose two best answers, of course you made my knowledge grow a lot this day, Is hard to imagine how much amazing things I can do with it. many thanks.
0

The answer for Array as Jerry has given is correct it it not for a structure.

    #include <stdio.h>

struct foo {
  int v1;
  int v2;
};


int main(void){
  struct foo v1, v2;

  v1.v1 = 1;
  v1.v2 = 2;
  v2 = v1;

  printf("v2.v1 = %d, v2.v2 = %d\n", v2.v1, v2.v1);
  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.