【发布时间】:2014-10-16 14:53:11
【问题描述】:
如何将字符串列表存储在 c 中的数组中。因为通常a字符串例如:'string'在array(?)中存储为s | t | r | i | g。现在,如果我有一个有 10 个索引的数组,并且想在每个索引中存储字符串,我该怎么做?如果不可行,我可以使用什么其他数据结构。
例如:数组 = 'string1'|'string2'|..
我做了一些事情,但它不起作用:
// Array for name of alphabets, and name of states
35 char *nameOfStates[numStates];
36 char buffer[3];
37
38 // Read the nameOfStates
39 int i;
40 for(i=0;i<numStates;i++){
41 printf("Name of STATES:");
42
43 int z=0;
44 char *buffer2;
45 while(z<2 && fgets(buffer,2,stdin) != NULL){
46
47 buffer2 = strndup(buffer,2);
48 z++;
49 }// End while-loop
50 nameOfStates[i] = buffer2;
51
52 }// End of for-loop to read nameOfStates
编辑:我意识到 array[size] 实际上不起作用!我这样做是因为我的 java 背景,我认为它可能会起作用。所以我改变了程序,但它仍然抛出分段错误。我在下面发布完整(编辑的)程序:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
// Function declaration
void analyze(char *string);
void clearNewLines(void);
int main(int argc, char *argv[]){
// Number of states and number of alphabets of DFA
int numStates;
int numAlphabets;
// Read numStates
printf("Enter the number of STATES:");
scanf("%d",&numStates);
// Flush STDIN
clearNewLines();
// Array for name of alphabets, and name of states
char **nameOfStates = malloc(numStates*sizeof(char*));
char *buffer = NULL;
// Read the nameOfStates
int i;
for(i=0;i<numStates;i++){
printf("Name of STATES:");
fgets(nameOfStates[i],2*sizeof(char),stdin);
}// End of for-loop to read nameOfStates
clearNewLines();
// Read numAlphabets
printf("Enter the number of ALPHABETS: ");
scanf("%d", &numAlphabets);
// Flush STDIN
clearNewLines();
// Array for name of alphabets, and name of states
char nameOfAlphabets[numAlphabets];
// Saving transition table
char *transitionTable[numStates][numAlphabets];
// Read name of alphabets
int j;
for(j=0;j<numAlphabets;j++){
printf("Name of ALPHABETS:");
nameOfAlphabets[j] = getchar();
// Flush STDIN
clearNewLines();
}// End for-loop to read alphabets
// Get the transitionTable[states][alphabets]
int row;
for(row=0;row<numStates;row++){
int col;
for(col=0;col<numAlphabets;col++){
printf("Enter Transition From %s to %c: ",nameOfStates[row],nameOfAlphabets[col]);
printf("\n");
}
}
return 0;
}// End of main function
/*
*
* clearNewLines - clear any newline character present at the STDIN
*/
void clearNewLines(void)
{
int c;
do
{
c = getchar();
} while (c != '\n' && c != EOF);
}
【问题讨论】:
-
首先,为指针变量分配内存。
-
一个数组数组。或
char **。 -
数据结构看起来不错(除了你的行缓冲区非常短),但你的循环很奇怪。您从
stdin中读取了i == 0的所有数据。您只需要一个循环,即外循环。如果输入用完,则中断。 (如果发生这种情况,numStates当然是错误的。) -
@SouravGhosh:
strndup分配所需的内存。buffer分配在栈上。 -
@MOehm 是的。
buffer2和fgets(buffer,2,..弄糊涂了。
标签: c arrays scanf fgets getchar