char * problem

This is a discussion on char * problem within the C++ Programming forums, part of the General Programming Boards category; Here is the code: char *tempName; cout<<"Enter the name you wish to compare: "; cin>>tempName; but I am getting some ...

  1. #1
    Unregistered
    Guest

    char * problem

    Here is the code:

    char *tempName;

    cout<<"Enter the name you wish to compare: ";
    cin>>tempName;

    but I am getting some sort of memory error, how do you read directly into the character pointer???

  2. #2
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    Pointer variables hold addresses, they cant hold characters. You have to dereferecne the pointer. Your pointer is only a pointer to a chacter too...it needs to point to an array, and you need to allocate space either on the stack or heap for it...id show you but im sure somone else will and im too hung over...
    -------------------
    "Exception"

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Code:
    #include <iostream.h>
    
    int main (void)
    {
    	char TempChar[55];
    	char *pTempChar = &TempChar[0]; //Dereference
    
    	cout<<"Enter the name you wish to compare: "; 
    	cin>>pTempChar; 
    	cout<<pTempChar<<'\n';
    
        return 0;
    }
    Last edited by Barjor; 04-25-2002 at 11:33 AM.

  4. #4
    Unregistered
    Guest
    char * pTemp = new char[40];
    cout << "enter name to be used in program";
    cin >> pTemp;

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    If you use that solution you need to make sure you use delete when you are done with the char

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    or even better


    Code:
    
    
      main()
      {
        char * pTemp = new char[40]; 
    cout << "enter name to be used in program"; 
    cin.getline(pTemp,40);
      cout<<pTemp;
    
        delete pTemp;
    
      system("pause");
      return 0;
      }

  7. #7
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Shouldnt that be delete[]?
    I dunno since its a pointer....

  8. #8
    Unregistered
    Guest
    yup, should be delete [] pTemp. If you use [] after new you use [] after delete.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    yes i thought i had missed something huge in that code

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    ***No meaning with subjetc****


    just glad to see someone who voted for Nader!!!


    Barjor!

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    who's nader?

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    1
    @Barjor:
    char *pTempChar = &TempChar[0]; //Dereference

    TempChar is already a adress, so you only need:
    char *pTempChar=TempChar;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  2. char problem
    By ForlornOdium in forum C++ Programming
    Replies: 10
    Last Post: 10-29-2003, 01:39 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM

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