I can write char a[] = "hello world"; , but i can not write
char a[];
a = "hello world";
why is that ??

Dani AI

Generated

— concise clarification and practical options (building on ):

An array declaration with an initializer fixes the array's size and sets its contents; that is initialization, not assignment. After the array object exists it cannot be the left-hand side of = because array names are not modifiable lvalues. In other words, the declaration-with-initializer both allocates and initializes; a later = to the whole array is not allowed. See the language notes on array initialization and why arrays can't be assigned as a unit: Array initialization (C) and .

Practical ways to get the same effect at runtime:

  • Point to a string literal (no copying; do not modify the pointed-at characters).
  • Allocate a buffer with an explicit size and copy the characters into it.
  • Allocate dynamically with malloc after measuring the source length and copy.

Example patterns (different names used from the thread):

const char *lit = "hello world";    /* points at literal; do not modify */

char buf[12];                        /* fixed-size buffer */
memcpy(buf, lit, strlen(lit) + 1);   /* copy including NUL */

size_t n = strlen(lit) + 1;
char *dyn = malloc(n);
if (dyn) memcpy(dyn, lit, n);        /* dynamic storage with copy */

Notes and cautions: when a declaration with an initializer appears in a function, the initialization logically happens when execution reaches that declaration (for static storage it happens before program start). Modifying a string literal is undefined behavior. If the intent is to replace whole contents later, choose a pointer or allocate appropriately-sized storage up front and copy into it.

There are several reasons, but probably not from the K & R.

The assignment is wrong for two reasons. It is to an array of no size, and you have to use strcpy, sprintf, or similar function to assign.

Also, char a[] = "hello world" is handled at compile time, where storage is allocated for that string.

There are several reasons, but probably not from the K & R.

The assignment is wrong for two reasons. It is to an array of no size, and you have to use strcpy, sprintf, or similar function to assign.

Also, char a[] = "hello world" is handled at compile time, where storage is allocated for that string.

1)Sir but if char a[]="hello world" is handled at compile time, then why cant
char a[];
strcpy(a,"hello world"); be handled at compile time ?
2)Moreover, i dont understand the part about compile time;
Say int a;
a=5; is also handled at compile time right ?
So if it can work for integers, then why cant it work for character array ?

Thank you for the response sir, Please reply

The storage for your array is handled at compile time. The a=5 is executed at run time. I suggest getting a copy of the original Kernighan and Ritchie book on C. It's just one of the rules. If you'd state why you want a[], that would help.

The storage for your array is handled at compile time. The a=5 is executed at run time. I suggest getting a copy of the original Kernighan and Ritchie book on C. It's just one of the rules. If you'd state why you want a[], that would help.

Thank you very much for the explanation sir. Yes, i m clear with it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.