Passing pointers.

This is a discussion on Passing pointers. within the C Programming forums, part of the General Programming Boards category; I'm sure I'm doing something wrong here. I can't figure out what tough. This is the function that gives trouble: ...

  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,185
    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
    21,602
    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.
    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

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, 09:14 AM
  3. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 05: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, 12:36 AM

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