question about strings

This is a discussion on question about strings within the C Programming forums, part of the General Programming Boards category; why do we have to declare a string like char *x = "..."; or char x[] = "..."; ? also ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    question about strings

    why do we have to declare a string like

    char *x = "...";

    or char x[] = "...";

    ?


    also if i say char x[];

    x[] = "..." it wont work why?

    thanks

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    x[]=" "
    doesn't mean anything because when the left hand side isn't a declaration, it expects a value between []. Also, char x[] has the same problem. Instead, you could go with:

    char x[];

    strcpy(x, "...");

    that would copy "..." into x.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Quote Originally Posted by jackhasf View Post
    why do we have to declare a string like

    char *x = "...";

    or char x[] = "...";

    ?
    Elaborate on the why. I don't see a question.

    also if i say char x[];

    x[] = "..." it wont work why?

    thanks
    char x[] defines an array with no size. How does this work?
    x[] = "..." will not work because you don't specify what index to assign something to.
    [] means that you want to assign something to a specific index in an array, to an individual char in this case. [] cannot be used to assign to multiple indexes in one statement.

    Quote Originally Posted by Poincare View Post
    Also, char x[] has the same problem. Instead, you could go with:

    char x[];

    strcpy(x, "...");

    that would copy "..." into x.
    Undefined behavior at its best. DO NOT DO THIS.
    The fact that x has no size specified is disturbing. There is no such thing as an unlimited array. It is not dynamic, thus it always has and MUST have an explicit size. If you try to copy some data into an array whose size you have not specified, what do you think happens?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Elysia View Post
    char x[] defines an array with no size. How does this work?
    x[] = "..." will not work because you don't specify what index to assign something to.
    Yes it will. It's defined as an array of unspecified size, whose size is determined by the size of whatever = gives it. Thus:
    Code:
    chat foo[] = "hello world";
    The size of foo is strlen( "hello world" ) + 1.
    Quote Originally Posted by Elysia View Post
    Undefined behavior at its best. DO NOT DO THIS.
    It is actually defined. Since you have declared it as an array, using [], you are allowed to use the space that was initially created for it. In this case, you would have four characters you could use (in your example, the = was "...", which would be 4 bytes).
    Quote Originally Posted by Elysia View Post
    The fact that x has no size specified is disturbing. There is no such thing as an unlimited array. It is not dynamic, thus it always has and MUST have an explicit size. If you try to copy some data into an array whose size you have not specified, what do you think happens?
    It's not unlimited, it's an unspecified size--meaning YOU didn't type the actual size, but the compiler/language figured it out because of your = usage.

    Note, you cannot do this:
    Code:
    char foo[] = "hello";
    ...
    foo = "world";
    You can't do that, because foo is an array, not a pointer. You also shouldn't attempt to modify string literals--no matter what idiots with ancient compilers tell you.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Quote Originally Posted by quzah View Post
    Yes it will. It's defined as an array of unspecified size, whose size is determined by the size of whatever = gives it. Thus:
    Code:
    chat foo[] = "hello world";
    The size of foo is strlen( "hello world" ) + 1.It is actually defined. Since you have declared it as an array, using [], you are allowed to use the space that was initially created for it. In this case, you would have four characters you could use (in your example, the = was "...", which would be 4 bytes).It's not unlimited, it's an unspecified size--meaning YOU didn't type the actual size, but the compiler/language figured it out because of your = usage.
    Yes, but it's called initialization. Creating an array of unspecified size and then assigning to it is not the same thing. Assigning to it after creating it with an unspecified is undefined behavior.
    Not specifying size when initializing it is fine as the compiler can figure out the size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by quzah
    Yes it will. It's defined as an array of unspecified size, whose size is determined by the size of whatever = gives it. Thus:
    Sure, but your example is not what they are talking about. They are talking about this:
    Code:
    char x[];
    x[] = "...";
    or this:
    Code:
    char x[];
    strcpy(x, "...");
    In both cases, unlike what you are talking about, the array is not initialised by a string literal or initialiser, thus the empty brackets will result in a compile error. As such, Elysia is correct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    In both cases, unlike what you are talking about, the array is not initialised by a string literal or initialiser, thus the empty brackets will result in a compile error. As such, Elysia is correct.
    Ah, I see. I blame being tired. We could of course do something like...
    Code:
    struct foo
    {
        size_t bar
        char baz[];
    };
    And get away with it...


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Hmmm. There is a case when it is allowed and a case when it is not... I did not know this, actually.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by quzah
    We could of course do something like... (...) And get away with it...
    ... if you are programming with respect to the 1999 edition of the C standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about reading in strings and integers
    By sgalland in forum C Programming
    Replies: 6
    Last Post: 11-22-2009, 12:05 AM
  2. Very simple question...annalyze strings
    By BC2210 in forum C Programming
    Replies: 7
    Last Post: 09-21-2009, 10:38 PM
  3. Beginner Question: Problem with scanf and strings. Help!
    By lucidrave in forum C Programming
    Replies: 8
    Last Post: 08-11-2009, 11:22 PM
  4. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 06:09 AM
  5. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-15-2005, 12:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21