红联Linux门户
Linux帮助

段错误

发布时间:2010-07-17 17:12:36来源:红联作者:wucongdonglai
今天在Linux系统下编写了一段小程序,其实是模拟扑克牌洗牌,并将其次序输出,花色以abcd代替,代码如下:
1 #include
2 #include
3 #include
4 /*change int to char*/
5 char transform(int n)
6 {
7 char p;
8 switch(n)
9 {
10 case 1:p='a';break;
11 case 2:p='b';break;
12 case 3:p='c';break;
13 case 4:p='d';break;
14 default:break;
15 }
16 return p;
17 }
18
19 int main()
20 {
21 int a[4][13];
22 int n=0,m=0;
23 for(;n<4;n++){for(;m<13;m++)a[n][m]=0;}
24 int t=0;
25 char c=0;
26 printf("the sequence of the cards is \n");
27 while(t<=54)
28 {
29 n=rand()%4;/*storchastic number between 0 and 4*/
30 m=rand()%13;
31 if(a[n][m]==0)/*if the number hasn't been printed,print it and change its property*/
32 {
33 n++;m++;
34 printf(" %c%d ",transform(n),m);
35 t++;a[n][m]=1;
36 }
37 }
38 printf("\nIs the sequence of the cards right?\n");
39 return 0;
40 }
结果输出如下:
the sequence of the cards is
d10 d2 a11 a12 d9 d4 c6 d5 c10 a6 d3 d5 a4 c7 a11 a12 a5 c12 a3 c6 c11 d2 a3 a4 d1 a8 a10 d6 c13 d10 a13 c7 a11 a9 d5 d6 c13 c6 a6 a4 d5 c6 a6 a2 a1 c9 c8 d6 a4 a5 d3 d6 a12 c9 a11 a3 a3 a5 a6 a6 a7 c12 d3 a4 c10 a2 d2 c9 a3 a7 a2 c13 a2 a8 a1 a11 a3 c8 a8 c13 a4 a4 d5 d2 a12 a1 c8 d4 a1 a10 a7 c12 a10 c8 a10 a9 a13 a1 a4 c8 a4 a8 c9 a8 a3 c12 a12 a5 c13 a2 d6 a3 a7 a2 d5 c11 a13 a11 a2 a13 c9 a2 d3 d5 a12 a3 d1 d1 d4 d4 a1 d1 a11 d6 c7 c9 a2 c9
Is the sequence of the cards right?
段错误
我怎么都想不明白,这里的数字又不大的,怎么会段错误?而且结果怎么会是错的?按理说所有数据只会被打印一遍,因为打印一次之后数组元素a[n][m】就为1了,也就不会打印了啊?
文章评论

共有 7 条评论

  1. wucongdonglai 于 2010-07-21 09:19:51发表:

    7# deepwhite
    哦,这样的啊!我再调试调试

  2. deepwhite 于 2010-07-20 16:04:48发表:

    好好整理一下你代码的逻辑吧,肯定是逻辑上的错误,不是GCC的问题。

    还有,vi肯定是能自动缩进的,而且缩进出来的代码很漂亮。如果他连缩进都没有,就不
    会有那么的程序员天天用了。

  3. deepwhite 于 2010-07-20 16:00:47发表:

    GDB自己慢慢调吧,呵呵。

  4. wucongdonglai 于 2010-07-19 14:12:05发表:

    4# deepwhite
    的确应该是52张,那是我想错了!因为对4取余之后是0到3,对13取余后是0到12,但是牌应该是1到13的,所以当时就直接对m和n都自加了,所以在switch里面是1、2、3、4。我应该把 a[n][m] = 1;放在自增之前,这样就不会有影响到越界了!但是我在之后运行,结果老半天没输出!
    然后我把输出的牌数设成30,结果可以正常输出!但是设为31的话,除了输出
    the sequence of the cards is
    别的什么输出都没有!是不是在GCC中所有循环结束后才一起输出的?另外我这是什么问题呢?为什么到31后就不行了?
    你的风格的确很不错!在vc下是自动缩进的,在vi下没有缩进,我也就懒得自己去敲空格了!

  5. deepwhite 于 2010-07-17 22:11:35发表:

    [i=s] 本帖最后由 deepwhite 于 2010-7-17 22:13 编辑 [/i]

    See the comment.[code]
    #include
    #include
    #include
    /*change int to char*/
    char transform(int n)
    {
    char p;
    switch(n) { /* XXX: 一个int对4取余, 余数范围应该为 0~3,而不是1~4. */
    case 0:p='a'; break;
    case 1:p='b'; break;
    case 2:p='c'; break;
    case 3:p='d'; break;
    default:
    printf ("Program should not be here!\n");
    break;
    }
    return p;
    }

    int main()
    {
    int a[4][13];
    int n = 0, m = 0;
    for(n = 0; n < 4; n++) {
    for(m = 0; m < 13; m++)
    a[n][m] = 0;
    }

    int t = 0;
    char c = 0;
    printf("the sequence of the cards is \n");
    while(t < 52) { /*XXX: 没有设计大王小王,所以应该是52张 */
    n = rand()%4;
    m = rand()%13;
    if(a[n][m] == 0) {
    /* n++;m++; */ /*XXX: 为什要自增?这样可能会引起越界操作,
    例如n=3 或者m=12的时候,
    再自增,后面代码中的赋值就越界了。 */
    printf("(%d, %d): %c%d\n", n, m, transform(n),m);
    t++;
    a[n][m] = 1;
    }
    }
    printf("\nIs the sequence of the cards right?\n");
    return 0;
    }
    [/code]

    我调了一下格式和代码风格,其中风格这个东西没有必要和我的一样,但是格式和风格调整的好一些,对提高代码的可读性有很大帮助。

  6. shenhao0129 于 2010-07-17 20:38:47发表:

    没仔细看,不过肯定是你没有对下标进行仔细的检查,很有可能有越界访问的问题,才会出现段错误

  7. 咖啡男孩 于 2010-07-17 18:51:55发表:

    麻烦把代码格式控制好再发一遍,谢谢……