Results 1 to 7 of 7

Thread: Character Pointer Const? - C++

  1. #1
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default Character Pointer Const? - C++

    I want to be able to copy some characters into a string from a parameter variable in C++. Here is code I made:

    Code:
    char name[];
    
    void setName(const char *n) {
        name = *n;
    }
    Syntax:

    Code:
    setName("Jimbo");
    I know that the two types of variables are incompatible. How can I do this!? You know, copy a string into another string through parameters. Thanks in advance!!

    -magicyte

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    #include <string.h>
    
    char *name;
    
    void setName(const char *n)
    {
        name = strcpy(malloc(strlen(n) + 1), n);
    }
    Last edited by Twey; 11-27-2008 at 07:38 AM. Reason: Whoops, forgot the const.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Thank you! I was, just a minute ago (TRUE FACT), thinking about strcpy and if you needed string.h to use it. And voula!

    Is it possible to do this without strcpy? Doubt so, but I would like to understand it.

    -magicyte

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well, yes, if you reimplement strcpy():
    Code:
    char *strcpy(char *dest, const char *src)
    {
        int i;
    
        for (i = 0; src[i] != '\0'; ++i)
            dest[i] = src[i];
    
        return dest;
    }
    Most C functions are very naïve. This means that they're easy to reimplement (but horrible to work with). Note that the GNU implementation of this is considerably more complex, for efficiency reasons.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Thanks for the strcpy code! Now, when I compiled the first code w/ string.h and malloc, my compiler said 'malloc' was undeclared. I came up with this code:

    Code:
    char *name;
    
    void setName(char *n) {
        name = n;
    }
    Is this good or bad? I use it since the 'malloc' didn't work. Could you also describe what malloc does? Just from the name, I'm guessing it allocates memory...

    I am also having trouble with this:

    Code:
    char *name; // already has value from setName()
    
    char getName(void) const {
        return *name;
    }
    
    name = getName();
    Also tried this:

    Code:
    char *name; // already has value from setName()
    
    char getName(void) const {
        return name;
    }
    
    name = getName();
    and this:

    Code:
    char *name; // already has value from setName()
    
    char *getName(void) const {
        return *name;
    }
    
    name = getName();
    Even this!!:

    Code:
    char *name; // already has value from setName()
    
    char *getName(void) const {
        return name;
    }
    
    name = getName();
    The last code I gave, compiler compiles, and a big bomb goes off in the DOS console. What do I do? They don't work. My compiler flags an error when I try to compile it.

    -magicyte
    Last edited by magicyte; 11-27-2008 at 06:47 PM.

  6. #6
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Oh. Nevermind. The last code DOES work. I was doing this:

    Code:
    #include "stdio.h"
    #include "conio.h"
    #include "game.h" // has code
    
    int main(void) {
        Warrior mario;
    
        mario.setName("Mario");
    
        printf("%s is the warrior of Strraughtnauten"); // no reference to %s!! silly me
    
        getch();
        return 0;
    }
    Ha! Imagine that...

    -magicyte

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    malloc() is defined in stdlib.h. It allocates the number of bytes provided and returns a pointer to the allocated memory, or NULL if the memory could not be allocated. Memory allocated by the likes of malloc() must be freed with free(). There are also helpers for common cases: see the man page.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •