我正在尝试使用c填充一副牌(我是c的新手)然而我一直收到错误,错误:可能无法为行初始化可变大小的对象
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:en="urn:enumtest"
targetNamespace="urn:enumtest"
elementFormDefault="qualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:simpleType name="Country">
<xs:restriction base="xs:string">
<xs:enumeration value="CH"/>
<xs:enumeration value="AT"/>
<xs:enumeration value="DE"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="Street" type="xs:string"/>
<xs:element name="No" type="xs:integer"/>
<xs:element name="Code" type="xs:integer"/>
<xs:element name="Country" type="en:Country"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Items">
<xs:sequence>
<xs:element name="Address" type="en:Address" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Document">
<xs:complexType>
<xs:sequence>
<xs:element name="Items" type="en:Items"/>
</xs:sequence>
<xs:assert test="count(distinct-values(en:Items/en:Address/en:Country)) = 1"/>
</xs:complexType>
</xs:element>
</xs:schema>
这是完整的代码。我试图填充一副牌并将牌组存放在阵列牌[52]中,使用颜色,套装和数值阵列来获得牌组中的每张牌。如果我的逻辑是错误的,那么如何将套装,面部和颜色放入套牌中以填充一副牌?
char **deck[r] = values[v], suits[d], colour[s];
如果我尝试
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef unsigned char card;
static char *suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
static char *values[] = { "Ace", "Two", "Three", "Four", "Five", "Six", \
"Seven", "Eight", "Nine", "Ten", "Jack", \
"Queen", "King" };
static char *colour[] = { "Black", "Red" };
void filldeck(card deck[52]);
int main()
{
filldeck(deck);
return 0;
}
void filldeck(card deck[52])
{
int r;
r = 0;
int v;
int d;
int s;
for ( v = 0; v < 13; v++)
{
for ( d = 0; d < 4; d++)
{
for ( s = 0; s < 2; s++)
{
char **deck[r] = values[v], suits[d], colour[s];
printf("%c", deck[r]);
r++;
}
}
}
return;
}
警告:赋值在没有强制转换的情况下从指针生成整数
P.S。甲板上有52张牌
请帮忙
答案 0 :(得分:2)
1)首先,你没有使用正确的数据类型,它对于char来保存那些信息是不可能的。您应该定义我自己的数据类型,如下所示。
typedef struct
{
unsigned int suits;
unsigned int value;
unsigned int colour;
}card_t;
2)我建议使用int存储每张卡并使用枚举在每个值之间进行如下操作:
enum suits{HEARTS=0; DIAMONDS, CLUBS, SPADES};//Diamonds =1, clubs=2,etc
enum values{ ACE=1, TWO=2, (...) ,KING=13};
enum colour{BLACK=0,RED};
3)你从未贬低过甲板,然后你将它传递给了它。这是对此的修复
int main(void)
{
card_t deck[52];
filldeck(deck);
return 0;
}
4)这是一个指针数组,你试图将三个指针放入数组中的一个位置
char ** deck [r] = values [v],适合[d],color [s];
这就是为什么你需要一个结构
void filldeck(card_t *deck)
{
int r=0,v,d,s;
for ( v = 0; v < 13; v++)
for ( d = 0; d < 4; d++)
for ( s = 0; s < 2; s++)
{
deck[r].suits=d;
deck[r].value=v;
deck[r].colour=s;
r++;
}
}
Ps:检查你的循环我认为你填充了104个空格(13 * 4 * 2)你可能需要更大的甲板尺寸,因为你使用了两种颜色
继续修复整个事情
#include <stdio.h>
#include <stdlib.h>
//static char *suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
//static char *values[] = { "Ace", "Two", "Three", "Four", "Five", "Six", \
//"Seven", "Eight", "Nine", "Ten", "Jack", \
//"Queen", "King" };
//static char *colour[] = { "Black", "Red" };
enum suits{HEARTS=0; DIAMONDS, CLUBS, SPADES};//Diamonds =1, clubs=2,etc
enum values{ ACE=1, TWO=2, (...) ,KING=13};
enum colour{BLACK=0,RED};
typedef struct
{
unsigned int suits;
unsigned int value;
unsigned int colour;
}Card_t;
void filldeck(card deck[52]);
int main(void)
{
card_t deck[52];//might be 104
filldeck(deck);
return 0;
}
void filldeck(card_t *deck)
{
int r=0,v,d,s;
for ( v = 0; v < 13; v++)
for ( d = 0; d < 4; d++)
for ( s = 0; s < 2; s++)
{
deck[r].suits=d;
deck[r].value=v;
deck[r].colour=s;
r++;
}
}
如果您想使用字符而不是数字(不太实用),请将结构定义为以下
typedef struct
{
char * suits;
char * value;
char * colour;
}Card_t;
循环如下
void filldeck(card_t *deck)
{
int r=0,v,d,s;
for ( v = 0; v < 13; v++)
for ( d = 0; d < 4; d++)
for ( s = 0; s < 2; s++)
{
deck[r].suits=suits[d];
deck[r].value=values[v];
deck[r].colour=colour[s];
r++;
}
}