我必须编写一个函数来查找具有给定数组的给定代码的产品.如果找到product,则返回指向相应数组元素的指针.
我的主要问题是给定的代码应首先截断为7个字符,然后才与数组元素进行比较.
非常感谢你的帮助.
struct product *find_product(struct product_array *pa, const char *code)
{
char *temp;
int i = 0;
while (*code) {
temp[i] = (*code);
code++;
i++;
if (i == 7)
break;
}
temp[i] = '\0';
for (int j = 0; j < pa->count; j++)
if (pa->arr[j].code == temp[i])
return &(pa->arr[j]);
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数,用于查找文件中最常见的字母字符.该函数应忽略除字母以外的所有字符.
目前我有以下内容:
int most_common(const char *filename)
{
char frequency[26];
int ch = 0;
FILE *fileHandle;
if((fileHandle = fopen(filename, "r")) == NULL){
return -1;
}
for (ch = 0; ch < 26; ch++)
frequency[ch] = 0;
while(1){
ch = fgetc(fileHandle);
if (ch == EOF) break;
if ('a' <= ch && ch <= 'z')
frequency[ch - 'a']++;
else if ('A' <= ch && ch <= 'Z')
frequency[ch - 'A']++;
}
int max = 0;
for (int i = 1; i < 26; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数来读取给定的文件并将其内容打印到屏幕上.目前我有以下内容:
int textdump(const char *filename)
{
int n = 0;
char ch;
FILE *fileHandle;
fileHandle = fopen(filename, "r");
if ((fileHandle = fopen(filename, "r")) == NULL)
{
return -1;
fclose(fileHandle);
}
while ((ch = fgetc(fileHandle) != EOF) )
{
printf("%c", ch);
n++;
}
fclose(fileHandle);
if (fclose(fileHandle) == EOF)
{
return EXIT_FAILURE;
}
return n;
}
Run Code Online (Sandbox Code Playgroud)
该函数成功读取文本文件并正确返回每个文件中的字符数.但后来我试图打印字符,现在我甚至无法运行程序 - 我得到"运行失败 - doc不能为null,无法解析测试结果".
我必须初始化一个只包含NULL指针的字符串数组,没有别的.如果我理解正确分配NULL指针,如下所示:
char **array = NULL;
Run Code Online (Sandbox Code Playgroud)
否则,我试过了
char *array[] = {NULL};
Run Code Online (Sandbox Code Playgroud)
但是应该动态分配NULL指针的空间,在这里我很困惑,我的代码不起作用.
char **array = (char**)malloc(sizeof(char*));
Run Code Online (Sandbox Code Playgroud)
如果你能帮助我,我将非常感激.