Char Pointer error!

This is a discussion on Char Pointer error! within the C++ Programming forums, part of the General Programming Boards category; First of all, i know this code is wrong and i know how to fix it hopefully! However, i do ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    Char Pointer error!

    First of all, i know this code is wrong and i know how to fix it hopefully! However, i do not know why the green point is wrong! I mean that i try to have a char pointer named "name" but...what is going wrong with this?
    thanks in advance...

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void){
        char *name;
             cout << "name: ";
             cin >> name;
             cout << "your name is: " << &name << "\n";
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    One problem is that you only have a pointer to char; you don't have any space to actually store a name. Another problem is that if you want to print the name, you should be printing name, not &name.

    I suggest that you fix your program by #include <string> and using a std::string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by brack View Post
    First of all, i know this code is wrong and i know how to fix it hopefully! However, i do not know why the green point is wrong! I mean that i try to have a char pointer named "name" but...what is going wrong with this?
    thanks in advance...

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void){
        char *name;
             cout << "name: ";
             cin >> name;
             cout << "your name is: " << &name << "\n";
    return 0;
    }
    Dude your code is fine, except that you are printing the address of name and not what name points to..

    just remember that cin would not store a whitespace character..
    so if i enter "John Doe" it would only print John.

    Just remove the ampersand from name, and it should be fine..
    it worked for me here anyways

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by Eman View Post
    Dude your code is fine, except that you are printing the address of name and not what name points to..

    just remember that cin would not store a whitespace character..
    so if i enter "John Doe" it would only print John.

    Just remove the ampersand from name, and it should be fine..
    it worked for me here anyways
    you may be right but in dev++ compiler it crushes...

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    is there a dev++ compiler?

    i used a dev c++ here and all it asked was for me to put a newline at the printf function..

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by brack View Post
    you may be right but in dev++ compiler it crushes...
    Because your code is not fine.

    Just remove the ampersand from name, and it should be fine..
    it worked for me here anyways
    It worked because luckily the 'name' pointed to a valid memory location.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    As laserlight said:
    One problem is that you only have a pointer to char; you don't have any space to actually store a name.
    You need to allocate memory for name before use. This means changing your
    Code:
    char *name;
    to
    Code:
    char name[30];
    or using dynamic memory.

    But as laserlight also suggested you would be better off changing to std::string instead of character arrays.


    Jim

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Unhappy

    Quote Originally Posted by kmdv View Post
    Because your code is not fine.



    It worked because luckily the 'name' pointed to a valid memory location.
    I would use this to refresh my memory on C strings....
    so what would be the way to fix it?

    to declare
    char *name and immediately initialise it to a string?
    like so:



    Actually, I just used the debugger in Dev C++ and it didn't work, embarrassed here


    I guess i would have to
    Code:
    char *name ="name"  ;
    initialize the pointer on declaration either to a hard coded string or I could do this

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
        char *name ;
        char _name[10] ;
             cout << "name: ";
            
    		 cin >> _name ;
    		 
    		 name = _name ; 
    		 
             cout << "your name is: " << name << "\n";
        
        system("pause") ;
        return 0;
    }
    And that should work.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by Eman
    I would use this to refresh my memory on C strings....
    so what would be the way to fix it?
    jimblumberg's example of declaring a 30 character array would be applicable (though the code as it is would then be vulnerable to buffer overflow).

    Quote Originally Posted by Eman
    to declare
    char *name and immediately initialise it to a string?
    No. You cannot change a string literal.

    Quote Originally Posted by Eman
    or I could do this (...) And that should work.
    The pointer to char named name would also be useless since one could print _name directly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    No. You cannot change a string literal.


    yes, I know. but it is still possible to declare a pointer to a string on initialization of the char* pointer and not get a segmentation fault (I am not talking about changing it after).

    The pointer to char named name would also be useless since one could print _name directly.[/QUOTE]

    lol yeah

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    Originally Posted by laserlight
    jimblumberg's example of declaring a 30 character array would be applicable (though the code as it is would then be vulnerable to buffer overflow).
    I agree about the vulnerability for buffer overflow. This is the case for any Cstring. That is why using std::string is a much better alternative.

    Jim

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by Eman View Post
    Dude your code is fine, except that you are printing the address of name and not what name points to..

    just remember that cin would not store a whitespace character..
    so if i enter "John Doe" it would only print John.

    Just remove the ampersand from name, and it should be fine..
    it worked for me here anyways
    If it worked for you, you got very very unlucky, because this is memory error. The only way it worked for you is that you got "lucky" enough to write over memory that happened to be in your address space

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

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