initialize char-array with string 
Author Message
 initialize char-array with string

hi all,

this might be a beginner's problem, but I'm sure you can help me with this:

I want to declare a char-array and initialize it with a string.

this listing does work:

char v[20]="text";

this doesn't:

char v[20];
v="text";

the compiler tells me there were "incompatible types in assignment" but why?

thx and so long

Georg Klein
--



Sat, 14 Jan 2006 00:35:39 GMT  
 initialize char-array with string


Quote:
>hi all,

>this might be a beginner's problem, but I'm sure you can help me with this:

>I want to declare a char-array and initialize it with a string.

>this listing does work:

>char v[20]="text";

That is initialisation of an array with another array, a valid operation
in C

Quote:

>this doesn't:

>char v[20];
>v="text";

That is assignment of an array to another which is invalid in C. You
need to use strcpy:

strcpy(y, text);

Quote:

>the compiler tells me there were "incompatible types in assignment" but why?

>thx and so long

>Georg Klein

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sat, 14 Jan 2006 06:27:46 GMT  
 initialize char-array with string

Quote:

> this might be a beginner's problem,

It is.

Quote:
> this listing does work:

> char v[20]="text";

String literals like "text" can be used to initialize arrays.

Quote:
> this doesn't:

> char v[20];
> v="text";

Here v is an array of characters. Once it is declared, individual
characters (array elements) may be modified, but v always refers to
the same storage, and you cannot assign to it. The string literal
"text" here - and everywhere but in initializations like the first one
above, is a static read only array of characters. In an expression
it is converted to a pointer to the memory location where it is
stored. A pointer is not an array, so the compiler tells you there is
a type mismatch.

What you can do is declare

char* v = "text";

to make v point to the memory location of "text". It is quite
different from

char v[] = "text";

though (think of what will happen if you do

v[2] = 's';

in either case).

See FAQ 1.32, 6.1, 6.2.

--

--



Sun, 15 Jan 2006 03:06:34 GMT  
 initialize char-array with string


Quote:
> hi all,

> this might be a beginner's problem, but I'm sure you can help me with this:

> I want to declare a char-array and initialize it with a string.

> this listing does work:

> char v[20]="text";

This is a definition with initialisation. The initialisation is an
array of char (a zero termintated string).
Quote:

> this doesn't:

> char v[20];

declaration or definition without initialisation.

Quote:
> v="text";

This is an assignment. C doesn't know of strings. You can't copy
strings with the '=' operator. Use strcpy().
Quote:

> the compiler tells me there were "incompatible types in assignment" but why?

No wounder. The right hand site results in a pointer (the address of
the constant string). The left hand site is an  pointer to an array.
You can't copy arrays using the '=' operator.

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de   eComStation Reseller in Germany
eCS 1.1 GA englisch wird jetzt ausgeliefert
--



Tue, 17 Jan 2006 05:42:53 GMT  
 initialize char-array with string

Quote:

>>this might be a beginner's problem, but I'm sure you can help me with this:

>>I want to declare a char-array and initialize it with a string.

>>this listing does work:

>>char v[20]="text";

        A string has type array of char. Which is why this intialization
works.
        That is easy enough to understand.

Quote:
>>this doesn't:

>>char v[20];

> declaration or definition without initialisation.

>>v="text";

        This one is harder. In a nutshell, it is wrong because that is
the way it is.
        The standard has a lot of gobbledygook on array types decaying
into ptr-to-array-elem types (which are r-values, and not l-values), and
exceptions to the decay rule for string-initializers and the sizeof
operator.

        That is just a way to precisely formalize the following rules:

char a[20] = "abcd"; is OK

char a[20], *b;
a = "abcd"; is not OK.
b = "abcd"; is OK.

char a[20], *b;
b = a; is Ok.
a = b; is not Ok.

char a[sizeof "abcd"] = "abcd"; is Ok.

char *b = "abcd";
char a [sizeof b] = "abcd"; is not Ok.

Ajoy.
--



Thu, 19 Jan 2006 11:49:01 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. char arrays versus char strings.

2. initializing a const char array

3. Help: dynamically initialize char[][] array ...

4. Initializing arrays of char

5. Initialize a char* with "a string"???

6. How to initialize an array of strings?

7. Need Help Initializing String Array

8. How to: initialize array from string ?

9. Differences between char array[SIZE] and char *array

10. char * to wchar_t (char string to wide string)

11. Converting char array of literals chars to escape chars

12. A char pointer (char *) vs. char array question

 

 
Powered by phpBB® Forum Software