# Question about strings and floats

• 07-27-2010
shiroaisu
Hi everyone.

i have 3 questions, the first one is:
why isnt this possible:
Code:

```char a[10]; a="Hello";```
and this is:

Code:

```char *a; a="Hello"```
??

i know that when i call both variables they both point to a[0] then why cant the first one work as the 2º one (but of course, it would store less chars)??

the 2 q:
why does this: char *a="TESTING TESTING" takes the same space as this: char *a=" " (both take only 4 bytes. and why does this: char a[10] takes 10 bytes. i know that one automaticaly reserves space for 10 chars and the other one doest, but how can we store like... 10 chars in 4 bytes??

the 3 q:
how is a float number like 24,123342 represented in bits? and why does a number like 3*10^20 only takes 4 bytes of space?? (the same as an integer).

Help pls, those may be stupid questions for some but those things dont make sence to me yet.
tks.
• 07-27-2010
Steve Cao
ANS:
(1)In char a[10] , a is a constant.So you just can not assign "Hello" to it with "=".The second one "a" is a string pointer variable,so you can do it.
(2)Well the string pointer "a" points to is stored in the constant memory area, and pointer "a " points to the first character of the string. The 4 bytes which is the result of sizeof(a) is just the size of an integer pointer,not the size of the string.
(3)The same as (2).
• 07-27-2010
anon
(1) a is an array. You can't assign to an array.

(2) a is a pointer. The string literal also decays to a pointer to the first character. However, a string literal is read-only. Therefore you should only point const pointers to it.

Code:

`const char* a = "hello";`
If you want a modifiable C-style string:
Code:

```char a[10] = "hello"; //or char a[] = "hello"; //necessary size deduced from the initialization```
If you can't use initialization, then you should use the strcpy library function to copy a string into an array:
Code:

```char a[10]; strcpy(a, "hello");```
(3) You should read about floating point numbers. Short answer: an int stores the number exactly, a floating point represents an approximation of the value and has limited precision. For example, a float will not store a value like 3.45629385648392038460039281 * 10 ^ 50 exactly; some of the precision will be lost and you'll rather end up storing 3.45629385648 * 10 ^ 50.
• 07-27-2010
shiroaisu
Ok tks for the quick answers