【发布时间】:2014-11-16 05:09:37
【问题描述】:
所以我最终要做的是在一个数组中搜索一个名称,如果找到该名称,则返回该名称。好吧,要做到这一点,我需要检查每一行和每一列中的每个字符是否匹配。在我能做到这一点之前,我需要确切地知道如何去做,所以我试图弄清楚如何让动态数组打印出第一个字符,然后是第二个等等,以便将它与正在搜索的名称。但我在这样做时遇到了麻烦。所以我的问题是我将如何检查这样一个数组中的每个字符?我已经发布了大部分代码,但我认为我包括了我遇到的主要部分。提前感谢您的帮助。 我是 C 的初学者,如果我做错了什么,非常抱歉,谢谢!
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define STRINGSIZE 20
int main(){
char **firstNames, **lastNames;
int classSize,i;
char sample[21] = "Slack";
printf("Please indicate number of records you want to enter (min 5, max 15):\n");
scanf("%d", &classSize);
firstNames=malloc(classSize*sizeof(char*));
for (i=0; i<classSize; i++) {
firstNames[i]=malloc(STRINGSIZE*sizeof(char));
}
printf("Please input records of students (enter a new line after each record), with following format: first name");
*firstNames="Slack";
printf("\n\n");
printf("%c", *(sample)); //Will print out S
printf("%c", **firstNames); //Will print out S
printf("%c", *(sample+1)); //Will print out l
printf("%c", **(firstNames+1)); //Will give error
printf("%c", **(firstNames)+1); //Will print T (Next ascii char after 'S'
printf("%c", **((firstNames)+1)); //Will give error
}
【问题讨论】:
-
对于“将给出错误”,你得到什么错误?
-
线程 1:EXC_BAD_ACCESS(code=1, address=0x0)
标签: c arrays string dynamic comparison