Another dumb question about file i/o

This is a discussion on Another dumb question about file i/o within the C++ Programming forums, part of the General Programming Boards category; I have no idea why it's working for you, trying to assign to a string literal is well defined as ...

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I have no idea why it's working for you, trying to assign to a string literal is well defined as being wrong and causing a run-time error. Here's the difference:
    char *buffer = "anything goes here";
    char buffer[] = "anything goes here";

    The first a is a string literal, the pointer points to an address in memory that contains the characters 'anything goes here\0'. You cannot modify this string literal, doing so will result in an error. The second is an array of characters containing 'anything goes here\0'. An array can be modified, so assigning a new string to the array is perfectly legal.

    When I run your program with a string literal, I get a run-time error and when I run it with an array it works fine.

    -Prelude
    My best code is written with the delete key.

  2. #17
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    well, I think I understand what you're saying.. except..

    first, how can you ever use a char* variable for anything without assigning it a value when it is declared, if you cannot change it? wouldnt that make it a const char* variable??


    my next question is:
    according to my understanding, if char* buffer is a pointer, it points to the address in memory where the first character of its associated string resides.
    when you access this variable, it reads from the address in memory where the first character resides, up untill it encounters a '\0'.
    therefore, I may ask, why is it not possible to modify this string?
    or more precisely, will replacing this string with a shorter string not use less memory, rather than more? thusly my meaning would be, if I attempt to write a larger string than the initial string assigned to buffer, it will overrun the bounds of memory allocated to the variable buffer, but if I attempt to write a string shorter than the original string in buffer, it will not.
    so the question is, wouldn't doing all this make it possible, although not likely, to run the program error-free? which would explain my managing to actually run it without errors.
    If you can actually figure all that out, and your brain still works, might you be able to give me an answer?


    ps.. im still trying to figure out what i just wrote

  3. #18
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    how can you ever use a char* variable for anything without assigning it a value when it is declared, if you cannot change it?
    ...
    therefore, I may ask, why is it not possible to modify this string?
    because, the linker may place literals in read-only segments or pages. The only reason it doesn't have to be const char* is to maintain backwards compatibilty with older C code (but this doesn't mean it's going to work).

  4. #19
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    okay so.. what you're saying is that char* variables are not (necessarilly) variable?? so what is the difference between a char* and a const char* ?
    hmm maybe I should go lock myself in a room full of C++ reference books and start reading again... im now utterly confused about char pointers.. (although of course when wasn't i ^.^)
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  5. #20
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >what is the difference between a char* and a const char* ?

    They're both pointers, but when using const char*'s you can modify the contents of what the pointers pointing to. Pointers have more uses than just working with string literals. A char pointer that points to a stack or heap variable could modify the contents of what it points to.

  6. #21
    Unregistered
    Guest
    Here's more than you probably want to know about literal strings. Prelude's post has all the necessar information. This might help with the whys and whererfores.

    char *buffer = "anything goes here";
    char buffer[] = "anything goes here";
    cout << "anything goes here";

    The third line effectively says display this string on the screen. However, the string doesn't have any "type" associated with it like usual variables do. It's not a null terminated char array and it's not a string from the apstring or STL string class. Yet it is a valid string. The name for this type of string is literal string or string literal. The compiler handles it a bit different than other strings. For example, as already stated you can't change the string literal once you have started the program. It's not considered a variable. It's not stored on the stack like other variables. You don't have to declare any memory for it like you do other variables. String literals don't have a name like variables do. String literals Do have an address like variables, so you can assign it from one pointer to another if you like.

    Although not as obvious, the string in the first line is also a string literal. Why? Because, just like in the third line there is no memory declared for the string to be stored in. Remember, the declaration for char *buffer does not declare any memory for buffer to point to. In line one buffer can point to a single char, but we don't know which char where just by looking at the declaration itself. The right hand side of the = operator tells us what char and where the char is that the pointer called buffer will point to. In particular, in the first line buffer points to the first char of the string literal. When pointing to a string literal you cannot change the string literal with the pointer, just like you can't change a variable with the pointer if the variable is declared constant.

    In the second line above, the declaration of an array does declare memory for the string on the other side of the = operator. How much memory is declared is determined by the compiler when it looks at the right hand side of the = operator.
    Once the memory has been declared the compiler then copies the string on the right hand side into the memory set aside for buffer. Since the value of the string is now contained in the variable memory, you can now change the value.

  7. #22
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Oh! kay.. now I get it. so if I had done something similar to

    char* buffer;

    buffer = new char;

    and then assigned values to buffer it would have worked? (note i wrote that off the top of my head so i might(probably) be wrong about the exact syntax but anyways.. you get the point?)

    because, then, I would be actually assigning a particular area of memory for buffer to use, and then actually putting any values into that area of memory, rather than attempting to write to the read-only spot where buffer pointed?

  8. #23
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >and then assigned values to buffer it would have worked?

    Yes (using strcpy() to copy the literal to the allocated memory). You can do what you want with memory that you've allocated explictly on the free store or implicity on the stack.

  9. #24
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    woohoo! I actually understand now..
    too bad I didn't remember sooner that pointers don't actually have any memory allocated when they are declared.. it would have saved me a few days worth of attempting to debug my program
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 10:46 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-17-2005, 11:09 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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