Thread: Question about strings and floats

  1. #1
    Registered User
    Join Date
    May 2010

    Question about strings and floats

    Hi everyone.

    i have 3 questions, the first one is:
    why isnt this possible:
    char a[10];
    and this is:

    char *a;

    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.

  2. #2
    Registered User
    Join Date
    Jun 2010
    (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).

  3. #3
    The larch
    Join Date
    May 2006
    (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.

    const char* a = "hello";
    If you want a modifiable C-style string:
    char a[10] = "hello";
    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:
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    May 2010
    Ok tks for the quick answers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A good way to convert strings to floats?
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2009, 12:08 AM
  2. user defined template question
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2006, 05:01 AM
  3. Array of Strings
    By mjpars in forum C Programming
    Replies: 8
    Last Post: 08-21-2003, 11:15 PM
  4. strings to floats
    By bob2509 in forum C++ Programming
    Replies: 2
    Last Post: 04-20-2002, 01:49 PM
  5. floats and strings
    By Stephen in forum C Programming
    Replies: 3
    Last Post: 11-07-2001, 02:43 AM