char str[], char *str;

This is a discussion on char str[], char *str; within the C++ Programming forums, part of the General Programming Boards category; Some recent experimentation has lead me to believe that the following two declarations are the same: Code: char str[]; char ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    char str[], char *str;

    Some recent experimentation has lead me to believe that the following two declarations are the same:
    Code:
    char str[];
    char *str;
    Is this true? I used the following code to check:
    Code:
    char str[]="Hello World!";
    char *ptr=str;
    cout << ptr << endl;
    The string was outputed, which means that an address must have been stored in str, which makes it a pointer. Am I correct?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    A pointer only points to it so yes they would be the same thing but you risk overwriting other variables I believe.... the pointer will give you a larger space and the declaration with th [] has a set space.... kinda like if I were using assembly.... and i told it i wanted a Data byte(used for strings) at DS = 100 and another data byte at ds = 105 if the first string over lapped the second then you would have hellohello world assuming both strings were hello world to begin with.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I think I'll stick with pointers. Much more straight forward.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    You still have the possibility to overwrite your strings with pointers.... thats the main reason I dont use them unless I really need to.... at least with declaring it with the [] you can make sure that the space is there tho it will limit the size of the string.... ex.

    char name[10];
    name = "Jimbo Jones"

    when it was couted you would have jimbo jone the s would be dropped off because of the null at the end of the string.

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    not really...........

    if you write this by itself:
    Code:
    char str[];
    compiler will flag an error b/c when you declare an array like this (on the stack) you ought to give it it's capacity...
    and whether you declrare an array statically (as above) or dynamically, once you set its size you can't really change it...

    to be o.k. on the terminallogy, by writing the following:

    Code:
    char str[] = "hello";
    you have declared an array of 6 characters, b/c by enclosing the word (hello) in double quotes (" "), implicitly you include the terminating character (\0) which signifies the end of the C-style string...

    and now when you write:
    Code:
    char *ptr = str;
    you must understand that the the name of your array (str) IS THE POINTER TO THE FIRST CHARACTER IN YOUR ARRAY, so in reality you are assigning (ptr) to be pointing to the same address as (str) is, which of course in this case is the first character in the array ( first character also being accessed by: str[0] )

    keep in mind that if your character array wouldn't be initialized in double quotes, meaning it would not carry the terminating character at the end (\0) then you only would see the FIRST character on the screen, not the entire string...

    for example:

    Code:
    char str[] = { 'h', 'e', 'l', 'l', 'o'};
    char *ptr = str;
    cout << ptr << endl;           // prints to screen only letter     h...
    hope that makes things a lot clearer...

    keep up the good work...


    ................
    matheo917

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So, let's recap.

    Code:
    char str[]="Hello";
    The compiler allocates 6 bytes on the local stack, puts the null-terminated string into those bytes, then puts the address of the first byte in str.

    Does this mean that a string enclosed in double quotes is not always stored somewhere else in the executable, but is sometimes entered byte-by-byte into an array?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by matheo917
    keep in mind that if your character array wouldn't be initialized in double quotes, meaning it would not carry the terminating character at the end (\0) then you only would see the FIRST character on the screen, not the entire string...

    for example:

    Code:
    char str[] = { 'h', 'e', 'l', 'l', 'o'};
    char *ptr = str;
    cout << ptr << endl;           // prints to screen only letter     h...
    hope that makes things a lot clearer...
    No, it wouldn't just display the first character -- It would keep displaying until it came to a byte in memory which was set to the value 0. You'd actually end up printing AT LEAST hello but mostlikely more than that as well. It will never, however, only print out an h.

  8. #8
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    Does this mean that a string enclosed in double quotes is not always stored somewhere else in the executable, but is sometimes entered byte-by-byte into an array?
    i don't think i quite understand your question, please rephrase...???????????


    note: declaring an array (statically) happens during compilation time, declaring an array (dynamically - on the heap) happens during the run-time, while the "executable" file is running...

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Well, I have been under the impression that a string enclosed in double quotes is stored as a sort-of resource in the executable. So:

    Code:
    char *str="Hello";
    Those 6 bytes are stored in the resource section of the executable, and "Hello" is replaced with a pointer to the string.

    That's what I thought happened. I'm pretty sure I read it somewhere.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    sorry i'm not really clear on the terminology of "resource in the executable" -------------> that's the first time i've seen such terminology.........

    maybe someone else can help you out with this..........


    ohhh yeah.........i made a mistake on the previous post, Polymorphic was correct..........

    thanx Polymorphic

    .....................
    matheo917

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by bennyandthejets
    Well, I have been under the impression that a string enclosed in double quotes is stored as a sort-of resource in the executable. So:

    Code:
    char *str="Hello";
    Those 6 bytes are stored in the resource section of the executable, and "Hello" is replaced with a pointer to the string.

    That's what I thought happened. I'm pretty sure I read it somewhere.
    Yup.

    The "Hello" is put in the text area of the program while in memory (which is usually made unwritable at runtime) and str points to the H.

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Good. At least I got something right for once.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Right.

    char * s = "I live in the .exe, itself";
    char t[] = "Not me, I live on the stack.";

    's' is a static variable.
    't' will be destroyed when it goes out of scope.

    Of course,

    char * s;
    char t[] = "I live on the stack.";
    s = t;

    In this case, 's' is not a static variable.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM

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