Thread: Warning from xcode

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    37

    Warning from xcode

    I am using xcode 3.2.5 and I am reading C++ Without Fear.
    At the chapter five there is this code from the author :

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    using namespace std;
    
    //prototypes
    int rand_0toN1(int n);
    void draw_a_card();
    //global declarations
    char *suits[4] = {"hearts", "diamonds", "spades", "clubs"};
    char *ranks[13] = {"ace", "two", "three", "four", "five", "six", "seven", "eight",
    	"nine", "ten", "jack", "queen", "king"};
    
    
    int main (int argc, char * const argv[]) {
        // insert code here...
        int n, i;
    	
    	srand(time(NULL));	//Set seed for random numbers
    	
    	while (1) {
    		cout << "Enter no. of cards to draw (0 to exit): ";
    		cin >> n;
    		
    		if (n == 0)
    			break;
    		
    		for (i = 1; i <= n; i++)
    			draw_a_card();
    	}//end while
    	
    	return 0;
    	
    }//end main
    
    
    //Draw-a-card function
    //Performs one card-draw by getting a random 0-4 and
    //a random 0-12.These are then used to index the
    //string arrays, ranks and suits
    void draw_a_card()
    {
    	int r;	//Random index (0 thru 12) into ranks array
    	int s;	//Random index (0 thru 3) into suits array
    	
    	r = rand_0toN1(13);
    	s = rand_0toN1(4);
    	cout << ranks[r] << " of " << suits[s] << endl;
    }//end draw_a_card function
    
    
    //Random 0-to-N1 Function
    //Generate a random integer from 0 to N-1
    int rand_0toN1(int n)
    {
    	return rand() % n;
    }
    The two lines after //global declarations give me the following warnings :
    Deprecated conversion from string constant to 'char'

    Is there something wrong with the code?
    The author suggest that this is the proper way to declare arrays of strings.
    Thank you.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I have xcode 3.1.1 and I get no deprecation warning.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Try adding "const" in front of the "char" for both pointer arrays.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    const worked, but what if you want to change an item?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If you want to change an item in the array, don't declare your array as const, and don't initialize the array at compile time, but rather at run time.

    Currently, since you are initializing the array at compile time, the compiler treats it as a const and puts (can choose to put) the actual data in read-only memory, so you can't change them at run time.

    For this program, having your two arrays declared as const is appropriate, since that data won't change during the course of your program.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM