所以我创建了一个程序来玩鱼,我开始洗牌并打印它。我的问题是:
我在" shuffle"中遇到了分段错误在分配数组值时在某处运行。
GCC抱怨" deck"中的多字符字符常量。我不知道如何宣布一副纸牌。
我一直在"下标值既不是数组,也不是指针,也不是矢量"尝试打印数组值时。
我知道我的代码中可能存在很多明显的错误。请原谅我。我是初学者。
这是代码(主要功能最后):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
int shuffle(int deck[52], int version)
{ //this is to store the shuffled deck after it is finished
int newdeck[52] = {0};
printf("In shuffle\n");
//I plan to create other combinations for a shuffled deck, but starting with only one
switch (version)
{ //this sets a value in newdeck to a value in deck, obviously mixing the order
//this is also where I get my segmentation fault.
case 1: newdeck[0] = deck[26];
newdeck[1] = deck[13];
newdeck[2] = deck[33];
newdeck[3] = deck[45];
newdeck[4] = deck[19];
newdeck[5] = deck[36];
newdeck[6] = deck[42];
newdeck[7] = deck[24];
newdeck[8] = deck[51];
newdeck[9] = deck[22];
newdeck[10] = deck[15];
newdeck[11] = deck[10];
newdeck[12] = deck[3];
newdeck[13] = deck[6];
newdeck[14] = deck[43];
newdeck[15] = deck[48];
newdeck[16] = deck[27];
newdeck[17] = deck[18];
newdeck[18] = deck[1];
newdeck[19] = deck[31];
newdeck[20] = deck[14];
newdeck[21] = deck[0];
newdeck[22] = deck[21];
newdeck[23] = deck[12];
newdeck[24] = deck[41];
newdeck[25] = deck[35];
newdeck[26] = deck[16];
newdeck[27] = deck[4];
newdeck[28] = deck[9];
newdeck[29] = deck[40];
newdeck[30] = deck[25];
newdeck[31] = deck[34];
newdeck[32] = deck[44];
newdeck[33] = deck[7];
newdeck[34] = deck[5];
newdeck[35] = deck[38];
newdeck[36] = deck[46];
newdeck[37] = deck[37];
newdeck[38] = deck[49];
newdeck[39] = deck[39];
newdeck[40] = deck[23];
newdeck[41] = deck[50];
newdeck[42] = deck[20];
newdeck[43] = deck[2];
newdeck[44] = deck[11];
newdeck[45] = deck[47];
newdeck[46] = deck[28];
newdeck[47] = deck[17];
newdeck[48] = deck[30];
newdeck[49] = deck[8];
newdeck[50] = deck[29];
newdeck[51] = deck[32];
break;
}
printf("Leaving shuffle\n");
//return the shuffled deck
return newdeck;
}
int main(void)
{ //declare a deck of cards.
int deck = {'2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS', 'AS', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH', 'AH', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC', 'AC', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD', 'AD'};
//a counter
int i;
//this is here for debugging purposes as well as the other similar occurrences
printf("Entering shuffle\n");
//shuffles the deck
deck = shuffle(deck, 1);
printf("Printing deck\n");
for ( i = 0 ; i < 52 ; i++ )
{
printf("%d ", deck[i]);//print the values in deck (now shuffled and set equal to newdeck from "shuffle" function. I also get the error "Subscripted value is niether array, nor pointer, nor vector."
}
return 0;
}
答案 0 :(得分:0)
问题代码尝试分配:
int deck = {&#39; 2S&#39;,&#39; 3S&#39;,&#39; 4S&#39;,&#39; 5S&#39;,&#39; 6S&#39 ;,&#39; 7S&#39;&#39; 8S&#39;,&#39; 9S&#39;,&#39; TS&#39;,&#39; JS&#39;,&#39 ; QS&#39;,&#39; KS&#39;&#39; AS&#39;,&#39; 2H&#39;,&#39; 3H&#39;,&#39; 4H&#39; ,&#39; 5H&#39;,&#39; 6H&#39;,&#39; 7H&#39;,&#39; 8H&#39;,&#39; 9H&#39;,&#39; TH&#39;,&#39; JH&#39;,&#39; QH&#39;,&#39; KH&#39;,&#39; AH&#39;,&#39; 2C&#39;, &#39; 3C&#39;,&#39; 4C&#39;,&#39; 5C&#39;,&#39; 6C&#39;,&#39; 7C&#39;,&#39; 8C& #39;,&#39; 9C&#39;,&#39; TC&#39;,&#39; JC&#39;,&#39; QC&#39;,&#39; KC&#39;,& #39; AC&#39;,&#39; 2D&#39;,&#39; 3D&#39;,&#39; 4D&#39;,&#39; 5D&#39;,&#39; 6D&# 39;,&#39; 7D&#39;,&#39; 8D&#39;,&#39; 9D&#39;,&#39; TD&#39;,&#39; JD&#39;,&# 39; QD&#39;,&#39; KD&#39;,&#39; AD&#39;};
此尝试分配存在多个问题。
deck
是一个整数。无法将多个值分配给整数。'
来定义字符常量。 (即:&#39; a&#39;&#39;&#39;&#39; c&#39;,&#39; X&#39;,&#39; Y&#39;,&# 39; Z&#39;,&#39; 1&#39;,&#39; 2&#39;&#39; 3&#39;,...)。上述分配中的字符值是非法的,因为它们分别是两个字符。
也许这就是预期的目的:
char * deck [] = { &#34; 2S&#34;,&#34; 3S&#34;,&#34; 4S&#34;,&#34; 5S&#34;,&#34; 6S&#34;,&#34; 7S& #34;,&#34; 8S&#34;,&#34; 9S&#34;,&#34; TS&#34;,&#34; JS&#34;,&#34; QS&#34;,& #34; KS&#34;,&#34; AS&#34;, &#34; 2H&#34;,&#34; 3H&#34;,&#34; 4H&#34;,&#34; 5H&#34;,&#34; 6H&#34;,&#34; 7H& #34;,&#34; 8H&#34;,&#34; 9H&#34;,&#34; TH&#34;,&#34; JH&#34;,&#34; QH&#34;,& #34; KH&#34;,&#34; AH&#34;, &#34; 2C&#34;,&#34; 3C&#34;,&#34; 4C&#34;,&#34; 5C&#34;,&#34; 6C&#34;,&#34; 7C& #34;,&#34; 8C&#34;,&#34; 9C&#34;,&#34; TC&#34;,&#34; JC&#34;,&#34; QC&#34;,& #34; KC&#34;,&#34; AC&#34;, &#34; 2D&#34;,&#34; 3D&#34;,&#34; 4D&#34;,&#34; 5D&#34;,&#34; 6D&#34;,&#34; 7D& #34;,&#34; 8D&#34;,&#34; 9D&#34;,&#34; TD&#34;,&#34; JD&#34;,&#34; QD&#34;,& #34; KD&#34;,&#34; AD&#34; };
上面将deck定义为一个字符指针数组。字符指针可以指向字符数组(或字符串)的第一个字符。字符指针数组被初始化为指向每个3个字符的字符串(两个可见字符,加上一个字符串终止字符'\0'
。
上面列出的问题(对C类型,指针等的有限理解)渗透到问题代码中。
看到一个更正确的剧集&#39;问题代码here的版本。