【发布时间】:2018-08-19 17:43:55
【问题描述】:
我试图通过下面代码中的函数更新数组,这很好。
char bookCategory[][MAX_CATEGORY_NAME_LENGTH] = {"Computer", "Electronics", "Electrical", "Civil", "Mechnnical", "Architecture"};
uint8_t getCategoryNumAndName(char* catName, uint8_t choice)
{
choice = choice - 0x30 - 1; /** Category starts from 1 on the screen */
if (choice >= (sizeof (bookCategory) / sizeof (bookCategory[0])))
{
//catName = NULL;
return (0xff);
}
else
{
strcpy(catName,bookCategory[choice]);
//catName = bookCategory[choice];
return(choice);
}
}
void addBooks(void)
{
// Some code here
char categoryName[30];
uint8_t catNumber;
catNumber = getCategoryNumAndName(categoryName, choice);
// Some code here
}
但我想到了使用双指针而不是使用 strcpy()。我尝试了下面的代码,但出现不兼容的指针类型错误。如何从 addBooks() 调用下面代码中的 getCategoryNumAndName()?
uint8_t getCategoryNumAndName(char** catName, uint8_t choice)
{
choice = choice - 0x30 - 1; /** Category starts from 1 on the screen */
if (choice >= (sizeof (bookCategory) / sizeof (bookCategory[0])))
{
*catName = NULL;
return (0xff);
}
else
{
//strcpy(catName,bookCategory[choice]);
*catName = bookCategory[choice];
return(choice);
}
}
void addBooks(void)
{
// Some code here
char categoryName[30];
uint8_t catNumber;
catNumber = getCategoryNumAndName(&categoryName, choice);
// Some code here
}
【问题讨论】:
标签: c arrays double-pointer