Thread: pointers

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> author = a;
    You cannot assign C style strings. You have to allocate memory for author and then use strcpy to copy the string.

  2. #17
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    You can't assign a pointer to another pointer. You can assign a pointer to a reference or a value but then you wouldn't be able to pass in a pointer. At least I think. Also, in your destructor you're trying to delete author. You can only use keyword delete when you allocate memory with keyword new.

    Code:
    char * author;
    author = new char[x];  //allocating x amount of chars
    delete [] author;
    I don't think you should assign author to the reference of "a" though. When your program goes through the destructors you'll most likely get a segmentation fault from trying to access inaccessible memory.

    You can keep what you're doing now and just do this:

    Code:
    int length = strlen(a);
    author = new char[length + 1];
    strcpy(author, a);
    author[length] = '\0'; //null character, you actually don't need this, more of an FYI
    Last edited by warfang; 05-09-2007 at 09:48 PM.

  3. #18
    0x01
    Join Date
    Sep 2001
    Posts
    88
    Code:
    	if (toLowerChoice == 'b')
    	{
    		pttt = new ...
    	}
    	else if (toLowerChoice == 'r')
    	{
    		pttt = new ...
    	}
    
    	// What if neither of the conditions are met...? What will you be returning?
    	return pttt; // could be anything
    
    	// you could try initializing 'pttt' like so,
    	Holding * pttt = 0;
    
    	// or you could try...
    
    	switch (toLowerChoice)
    	{
    	case 'r':
    		{
    			// ...
    			break;
    		}
    	case 'b':
    		{
    			// ...
    			break;
    		}
    	default: // if the user refuses to cooperate
    		{
    			pttt = NULL; // pttt = 0;
    		}
    	};
    
    	return pttt;
    
    	// you could also, place within a do{}while until conditions are met
    	do
    	{
    		cout << "\nEnter B for book or R for recording: " << endl;
    		cin >> holdingChoice;
    
    		switch(toLowerChoice)
    		{
    			// ...
    		}
    
    	}
    	while (pttt == NULL);
    
    	return pttt;

  4. #19
    Registered User
    Join Date
    May 2007
    Posts
    88
    > You can't assign a pointer to another pointer.

    Not true.

    > You cannot assign C style strings

    Oh, but you can.

    Nevertheless, it's bad practice, especially in C++. Use string objects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM