Consider this code:
typedef struct fruits_s { char* key; char value; } fruits_t; static fruits_t fruit_array[] = { { "Apple", 1 }, { "Banana", 2 }, { "Grape", 3 }, { "Orange", 4 } }; static fruits_t* getFruitFromValue(char value) { int i; for (i = 0; i < sizeof(fruit_array)/sizeof(fruit_array[0]); i++){ if (value == fruit_array[i].value){ return fruit_array[i]; } } }
I am new to C and I am still involved when pointers are needed / used. I am spoiled from Java background. So, in the above code, what confuses me is, should the function return the fruits_t* pointer? Or something different? When I do fruit_array[i] , is it a pointer to my structure or to my own structure?
As they say, later in my code, when I want to use this function, it is:
fruits_t* temp = getFruitFromValue(1);
or
fruits_t temp = getFruitFromValue(1);
or
fruits_t temp = &getFruitFromValue(1);
source share