Thread: Declaring a string as char ptr

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    Declaring a string as char ptr

    when you declare a string lets say

    char *string;

    can you give it a value? fe: gets(string)
    or can you only use it as a const string like

    char *string = "blabla";

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can allocate memory using say, malloc(), and then get that pointer to point to the first character of the memory allocated. Or, you could get it to point to the first character of a character array, similiar to getting it to point to the first character of a string literal, except that you would be able to modify the contents of the array through the pointer.

    Oh, and do not use gets() even if you were working with a character array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    yeah i used gets for a quick example

    i also thought you were able to declare a string like char *conststring; and then give it a value via gets() or fgets() or what ever, but you just werent able to change it with lets say *(conststring + i) = 'a'... Guess I was wrong or am I missing something?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What you are missing is that the pointer needs to point to something. Functions like gets and fgets write to what the pointer points to, thus you cannot just pass the pointer to them when it is a null pointer or does not point to anything valid.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    I get what you mean laserlight, you are right.
    But isn't there a way to give a value to a char pointer that isn't pointing to anything? I really thought there was a possibility to do this? Just declare char *string; and give a const string to it that you cannot alter. Did i really dream this lol

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by boxden
    But isn't there a way to give a value to a char pointer that isn't pointing to anything?
    Yes, and I listed two: dynamic memory allocation and setting it to point to the first character (or somewhere else) of a character array. Getting it to point to the first character of a string literal is a third way, but not very relevant for what you intend to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    yes i know but i mean without allocating. But I guess not than thank you

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by boxden View Post
    I get what you mean laserlight, you are right.
    But isn't there a way to give a value to a char pointer that isn't pointing to anything? I really thought there was a possibility to do this? Just declare char *string; and give a const string to it that you cannot alter. Did i really dream this lol
    I think you're thinking of
    Code:
    char *foo = "const string";
    foo now points to a legitimate memory address, but that memory address is read-only (and so foo should really be a "const char *" instead).

    (EDIT: And re-reading the thread, that's where we started, isn't it? So yes, there's nothing else.)
    Last edited by tabstop; 04-17-2010 at 05:33 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well... how do you store cookies in a jar when you don't have a jar and cannot obtain one?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM