Thread: Passing pointers.

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Passing pointers.

    I'm sure I'm doing something wrong here. I can't figure out what tough.

    This is the function that gives trouble:
    Code:
    void ten_and_up(int random, char *pjackEtc)
    {
    	if (random == 11)
    	{
    		pjackEtc = "Jack";
    		puts("Jack");
    	}
    	else if (random == 12)
    	{
    		pjackEtc = "Queen";
    		puts("Queen");
    	}
    	else if (random == 13)
    	{
    		pjackEtc = "King";
    		puts("King");
    	}
    	else if (random == 14)
    	{
    		pjackEtc = "Ace";
    		puts("Ace");
    	}
    	else
    	{
    		puts("***Error in void ten_and_up***");
    	}
    	return;
    }
    This is the prototype:
    Code:
    void ten_and_up (int random, char *pjackEtc);
    And this the function call:
    Code:
    ten_and_up(random, pjackEtc);
    The function executes the else statement.

    Thank You.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what is the value of random? Why do you think it is 11, 12, 13, or 14? Obvious thing I would do: in the error statement you're getting, print the value of random as well and see what it is. Especially given the name of the function -- ten_and_up -- I would assume that random might be ten.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One possible problem is that since you are just passing a pointer, assigning to the pointer in the function has no external effect. What you probably want to do instead would be along the lines of:
    Code:
    void ten_and_up(int random, const char **pjackEtc)
    {
        if (random == 11)
        {
            *pjackEtc = "Jack";
            puts("Jack");
        }
        /* ... */
    }
    
    /* ... */
    ten_and_up(random, &pjackEtc);
    Alternatively, you copy the given string literal to the string that is passed, assuming that the string that pjackEtc points to has enough space for the longest possible word to be copied over.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  2. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 10:14 AM
  3. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 06:00 PM
  4. Passing pointers
    By cyberCLoWn in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2004, 12:17 PM
  5. Pointers and reference passing
    By Denis Itchy in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 01:36 AM