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 3430442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:12:18+00:00 2026-05-18T07:12:18+00:00

gcc 4.5.1 c89 I have a function that assigns the elements of the following

  • 0
gcc 4.5.1 c89

I have a function that assigns the elements of the following structure:

static struct Config_t {
    char protocol[LINE_SIZE];
    char mode[LINE_SIZE];
} *app_config = NULL;

The function using malloc and memset to assign and clear the memory.

Once that is done, I have functions that gets the individual elements:

char* get_mode()
{
    if(app_config->mode != NULL) {
        return app_config->mode;
    }
    return NULL;
}

Here I am checking that a value has been assigned. And returning NULL if it hasn’t. So in the calling function I can check if a NULL is returned. However, if there a better way to do this?

Many thanks for any suggestions,

  • 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-05-18T07:12:19+00:00Added an answer on May 18, 2026 at 7:12 am

    In C, returning NULL for an error condition is standard practice for functions returning pointers, so you’re in good shape there. (For functions that don’t return pointers, the usual convention is to return 0 on success and a non-zero error code on error.)


    Separately, and perhaps a bit off-topic: mode can’t be NULL, though, can it? app_config can certainly be NULL, it’s a pointer to a structure, but your mode is defined as an array, an intrinsic part of the struct, not as a pointer. You’ll either have the struct, or not, but you won’t have only part of the struct. Simply allocating the memory for the struct will allocate the LINE_SIZE chars for mode; in fact, sizeof(struct Config_t) == LINE_SIZE + LINE_SIZE, the structure is an array of characters followed by another array of characters. There are no pointers involved (other than app_config, because you’ve defined it as a pointer to the structure).

    Consequently, to fully allocate your struct Config_t, just do this:

    app_config = malloc(sizeof(*app_config));
    

    (or app_config = malloc(sizeof(struct Config_t)); if your platform won’t allow the above.) That allocates mode, nothing else required.

    If mode were defined as a char *, that would be different:

    static struct Config_t {
        char *protocol;
        char *mode;
    } *app_config = NULL;
    

    Now sizeof(struct Config_t) == 2 * sizeof(void*) (see below), the structure itself consists only of two pointers, not any data that they may point to. Allocating the structure does not allocate any storage for them.

    #include <stdio.h>
    
    #define LINE_SIZE (200)
    
    struct Config_t {
        char protocol[LINE_SIZE];
        char mode[LINE_SIZE];
    };
    struct Config_t_with_pointers {
        char *protocol;
        char *mode;
    };
    
    int main(int argc, char* argv[])
    {
        printf("sizeof(struct Config_t) = %zu\n", sizeof(struct Config_t));
        printf("sizeof(struct Config_t_with_pointers) = %zu\n", sizeof(struct Config_t_with_pointers));
        return 0;
    }
    

    (Given your compiler, I felt free to use the z format specifier for size_t arguments, as any recent gcc has it [and it’s in the C99 standard, Matthew tells us].)

    Output (on my 64-bit Linux system):

    sizeof(struct Config_t) = 400
    sizeof(struct Config_t_with_pointers) = 16
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc 4.4.2 c89 I have a function that has to run (config_relays). It make
gcc 4.4.1 c89 I have the following code snippet: #include <stdlib.h> #include <stdio.h> char
gcc 4.4.1 c89 I have the following code: static enum states { ACTIVE, RUNNING,
gcc 4.4.4 c89 I have the following file that contains name, age, and gender.
gcc 4.5.1 c89 I have a buffer that is filled with char characters. I
gcc 4.6.2 c89 I have the following 2D array that I want to pass
gcc 4.4.4 c89 I have the following function but I cannot free the memory.
gcc 4.4.4 c89 I have the following code and 2 structures that have to
gcc 4.4.4 c89 I have the following code in my channel.h file typedef struct
gcc 4.4.2 c89 I have the following enum: enum drop_options_e { drop_ssm, drop_snm, drop_ssb

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.