Results 1 to 6 of 6

Thread: char pointer not work right

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Posts
    417

    char pointer not work right

    Hi,

    suppose I have a struct w/ a char pointer if i try to set the value of this character to a filled char variable I get weird characters.

    char Name[ ] = "Nobody";

    MyStruct->charPointer = Name;

    cout << "my name is " << MyStruct->charPointer <<endl;

    gives weird characters instead of the string(not a memory location like 0xcdaddc but symbols)

    anyone know why?

    MyStruct->charPointer = "Nobody"; <- works fine.

  2. #2

    Re:char pointer not work right

    Just a guess here (I dont' know my ass from my elbow in C), but could it be due to the fact that you're trying to point to a memory range with a single pointer? Perhaps see if pointing the pointer to the first array element brings you more success, or perhaps replacing the array of chars with a linked list.

  3. #3

    Re:char pointer not work right

    [quote author=Tyr_7BE link=board=9;threadid=6469;start=0#60989 date=1046437933]or perhaps replacing the array of chars with a linked list.[/quote]
    arrays of chars are for strings.

    lets see your declaration for your struct. is it a char * or a char [255]? it should be a char * because it needs to point to something, not have a buffer to be filled.

  4. #4

    Re:char pointer not work right

    Also make sure your C++ strings are NULL-terminated

  5. #5

    Re:char pointer not work right

    [quote author=Ashcrow link=board=9;threadid=6469;start=0#61070 date=1046530679]
    Also make sure your C++ strings are NULL-terminated
    [/quote]

    yeah i've had that problem before... why does my string foo show up as "foos@59iso9*%)(@(vjos9"?

  6. #6

    Re:char pointer not work right

    Strings show up with garbage after them because they keep printing until they hit the termination character. So whatever garbled stuff you see after the string.... ONE character after that is the string termination character... Generally its \0

    The following code will do pretty much what you want... however, if you want a default name of nobody, my suggestion is create a constructor in your struct (might as well make it a class if you do this) and allocate maybe 10 bytes of memory to the pointer, then set the name to the default...

    #include<iostream.h>

    typedef struct
    {
    char *name;
    }PERSON;


    int main(void)
    {
    PERSON bob;
    bob.name = new char[10];
    bob.name = "nobody";
    cout << bob.name;

    return 0;
    }

Similar Threads

  1. ToolTips Covered Up Using Large Mouse Pointer
    By regix in forum Windows - General Topics
    Replies: 0
    Last Post: 01-04-2005, 03:06 AM
  2. Just got a laser pointer
    By ndogg in forum General Chat
    Replies: 6
    Last Post: 05-19-2002, 04:34 AM
  3. int in char*
    By in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 04-12-2002, 07:24 AM
  4. I need help with char arrays in c
    By t048 in forum Linux - Software, Applications & Programming
    Replies: 6
    Last Post: 02-12-2002, 11:49 PM
  5. What is char-major-10-135?
    By Digital_Froggy in forum Linux - General Topics
    Replies: 2
    Last Post: 12-19-2001, 01:01 PM

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
  •