-1

I was trying to get array updated through a function as in below code and this works fine.

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
}

But I thought of making use of double pointer instead of using strcpy(). I tried below code, but I get incompatible pointer type error. How to call getCategoryNumAndName() in below code from addBooks()?

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
    }
Rajesh
  • 1,085
  • 1
  • 12
  • 25

2 Answers2

0

You can only pass pointer address to getCategoryNumAndName function not array address. You can do as below.

    char *categoryName = NULL;
    catNumber = getCategoryNumAndName(&categoryName, choice);

make sure you assign the memory to categoryName in getCategoryNumAndName before dereferencing it .

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
-1

To just force the code to work, you need to cast categoryName to a char**. But reading your code, it seems like you just want to move the pointer? You don't need a fixed size array for the category name. Just use a pointer:

char* categoryName;
Aiden Woodruff
  • 341
  • 3
  • 9
  • A cast should only be used if one really knows what it's doing. For this code, a cast is the absolutely wrong opeartor and definitively malicious. Use the correct type, casts don't fix type errors! – too honest for this site Aug 19 '18 at 19:16