tehet
February 22nd, 2008, 05:41 PM
Hi,
I'm trying to generate a list of cards (for learning structs and pointers) by looking at example program 5.2 (http://home.netcom.com/~tjensen/ptr/ch5x.htm). First I make a struct card{} and than a deck of 52 cards. Here deck is an array of a new data type card right?
Then I want a pointer of the type card to point to the first card in the deck (lines 23, 24) and finally some loops to generate the cards. Here's the code:
/* Cards */
#include <stdio.h>
#include <string.h>
/* char spade = 'S'; "\u2660"; unicode not working
char club = 'C'; "\u2663";
char heart = 'H'; "\u2665";
char diamond = 'D'; "\u2666"; */
char colours[] = {'S', 'C', 'H', 'D'};
char values[] = {2, 3, 4, 5, 6, 7, 8, 9, 0, 'J', 'Q', 'K', 'A'};
struct card{
char colour[1];
char value[1];
};
struct card deck[52];
int main()
{
struct card *card_ptr;
card_ptr = &deck; /* ------- line 24 -------- */
int i, n, card_num;
for (i = 0; i < 13; i++)
{
for (n = 0; n < 4; n++)
{
strcpy(deck[card_num].colour, colours[n]); /* Segmentation fault :( */
strcpy(deck[card_num++].value, values[i]);
}
}
/* show_deck(card_ptr); */
return 0;
}
It gives the following compiler warnings:
~/test$ gcc -g cards.c -o cards
cards.c: In function ‘main’:
cards.c:24: warning: assignment from incompatible pointer type
cards.c:30: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast
cards.c:31: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast
In short, I don't understand either the assignment warning nor the strcpy warning or how to get rid of them. Any hints on what I'm doing wrong?
And an unimportant question, is it possible to get unicode working in C? (for the spades, clubs, hearts and diamond symbols)
Thanks.
I'm trying to generate a list of cards (for learning structs and pointers) by looking at example program 5.2 (http://home.netcom.com/~tjensen/ptr/ch5x.htm). First I make a struct card{} and than a deck of 52 cards. Here deck is an array of a new data type card right?
Then I want a pointer of the type card to point to the first card in the deck (lines 23, 24) and finally some loops to generate the cards. Here's the code:
/* Cards */
#include <stdio.h>
#include <string.h>
/* char spade = 'S'; "\u2660"; unicode not working
char club = 'C'; "\u2663";
char heart = 'H'; "\u2665";
char diamond = 'D'; "\u2666"; */
char colours[] = {'S', 'C', 'H', 'D'};
char values[] = {2, 3, 4, 5, 6, 7, 8, 9, 0, 'J', 'Q', 'K', 'A'};
struct card{
char colour[1];
char value[1];
};
struct card deck[52];
int main()
{
struct card *card_ptr;
card_ptr = &deck; /* ------- line 24 -------- */
int i, n, card_num;
for (i = 0; i < 13; i++)
{
for (n = 0; n < 4; n++)
{
strcpy(deck[card_num].colour, colours[n]); /* Segmentation fault :( */
strcpy(deck[card_num++].value, values[i]);
}
}
/* show_deck(card_ptr); */
return 0;
}
It gives the following compiler warnings:
~/test$ gcc -g cards.c -o cards
cards.c: In function ‘main’:
cards.c:24: warning: assignment from incompatible pointer type
cards.c:30: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast
cards.c:31: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast
In short, I don't understand either the assignment warning nor the strcpy warning or how to get rid of them. Any hints on what I'm doing wrong?
And an unimportant question, is it possible to get unicode working in C? (for the spades, clubs, hearts and diamond symbols)
Thanks.