Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:56:01+00:00 2026-06-14T12:56:01+00:00

Consider this code: typedef struct fruits_s { char* key; char value; } fruits_t; static

  • 0

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 am still learning when pointers are necessary/used. I come spoiled from a Java background. So, in the above code, what I’m confused of is should the function return a pointer fruits_t*? Or something else? When I do fruit_array[i] is that a pointer to my struct, or the struct itself?

That being said, later in my code when I want to use the function, is it this:

 fruits_t* temp = getFruitFromValue(1);

or

 fruits_t temp = getFruitFromValue(1);

or

 fruits_t temp = &getFruitFromValue(1);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:56:03+00:00Added an answer on June 14, 2026 at 12:56 pm

    The function could return either — your choice. You’ve said you’ll return a pointer; that’s OK as long as you do.

    When you write:

    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];
            }
        }
    }
    

    There are several problems:

    1. fruit_array[i] is a structure, not a pointer. Use return &fruit_array[i];.
    2. If the loop exits, you don’t return a value from the function at all.

    Fixing those leads to:

    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];
        }
        return NULL;
    }
    

    This is OK because the pointer you return is to static data that will outlive the function. If you tried to return a pointer to non-static data, you would (probably) have a bug on your hands, unless you used dynamic memory allocation (via malloc() et al).

    You could also return the structure; handling the error return becomes harder. If you’ve got C99, you can use a ‘compound literal’:

    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];
        }
        return (fruits_t){ .key = "", .value = 0 };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this piece of code: struct Trade { float Price; char* time; int shares;
Consider this code: enum { ERR_START, ERR_CANNOTOPENFILE, ERR_CANNOTCONNECT, ERR_CANNOTCONNECTWITH, ERR_CANNOTGETHOSTNAME, ERR_CANNOTSEND, }; char* ERR_MESSAGE[]
Consider this code: char buffer[] = abcdefghijklmnopqrstuvwxyz, *val = malloc(10), *pbuf = buffer, *pval
Consider the following C code snippet: typedef struct node{ int val; struct node* left;
Consider this code: struct A {}; struct B { B(A* a) : a(a) {}
Consider this code: struct foo { int a; }; foo q() { foo f;
Consider the following code template<typename T, int N> struct A { typedef T value_type;
Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Consider this code snippet public class ConstantFolding { static final int number1 = 5;
Consider this code: <tr> <td> <input type=checkbox name=20081846 value=20081846 class=chk-input> </td> <td>20081846</td> <td>AGONOY, JAY

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.