【发布时间】:2014-10-19 12:40:30
【问题描述】:
我有一个问题,试图在 C 语言的结构中使用 char**。
我的代码的目的是保留字符串的历史记录。该结构有 2 个变量:
codeJuste,它是一个 NB_PION 字符的字符串。 codeProposes,它是一个 NB_COUPMAX 案例的数组。每个案例都有一个代码,它是一个 NB_PION 字符的字符串。
我正在尝试写入代码建议的每种情况,但出现分段错误错误。提前感谢您尝试帮助我(对不起我的英语,我是法国人)。
这是我的测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define NB_PION 4
#define NB_COULEUR 4
#define NB_COUPMAX 6
//Définition d'une structure
typedef struct _Partie{
char* codeJuste;
char** codesProposes;
} Partie;
int main (int argc,char** argv[]){
Partie maPartie;
maPartie.codeJuste = malloc((NB_PION+1) * sizeof(char));
strcpy(maPartie.codeJuste,"AAAA");
maPartie.codesProposes = malloc(NB_COUPMAX * ((NB_PION + 1) * sizeof(char)));
strcpy(maPartie.codesProposes[0],"BBBB");
strcpy(maPartie.codesProposes[1],"CCCC");
printf("1:%s \n",maPartie.codeJuste);
printf("2:%s \n",maPartie.codesProposes[0]);
printf("3:%s \n",maPartie.codesProposes[1]);
}
【问题讨论】:
-
你从哪里得到错误(哪一行),它到底说了什么?
-
o_weisman :编译后启动程序时出现错误。错误是“分段错误:11”