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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:34:27+00:00 2026-06-17T16:34:27+00:00

gcc 4.7.2 c89 Hello, I have this function that assigns a pointer to an

  • 0
gcc 4.7.2
c89

Hello,

I have this function that assigns a pointer to an another pointer that is an element of a string structure.

The purpose of this string structure is to get and set the length of the actual string.

However, I don’t understand why they have used a ternary operator. It doesn’t make sense to me. There doesn’t seem to be a condition apart from str->len = src. Which only assigns and doesn’t compare anything.

typedef struct tag_str_t str_t;
struct tag_str_t {
    char *buf;
    size_t len;
};

static void string_set(str_t *str, const char *src)
{
    str->buf = (char*)src;
    str->len = src ? strlen(src) : 0;
}

Would it be just as simple and more readable by doing just this?

str->len = strlen(src);

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-06-17T16:34:28+00:00Added an answer on June 17, 2026 at 4:34 pm

    The code:

    str->len = src ? strlen(src) : 0;
    

    is equivalent to:

    str->len = ((src != 0) ? strlen(src) : 0);
    

    I’ve added the parentheses and the extra expression to indicate where the components of the expression are. Your comment seems to indicate that you think str->len = src is the condition in the ternary operator, but that is wrong; assignment has a very low precedence.

    As Jesus Ramos also explained, the purpose of the test is to ensure that strlen() is not called with a NULL pointer, which would lead to a crash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc (GCC) 4.7.0 c89 Hello, I have the following structure that I am trying
gcc 4.4.2 c89 I have a function that has to run (config_relays). It make
gcc 4.7.2 c89 Hello, The function GetCallRef(...) expect a pointer to a buffer for
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
gcc 4.7.2 c89 Hello, I have the following function: void module_param_destroy(module_param_t *param) { param
gcc 4.4.2 c89 I have this code snippet that I have to repeat in
gcc 4.6.2 c89 I am passing a pointer to a function. This pointer will
gcc (GCC) 4.6.3 c89 Hello, I am just wondering if this is the best
gcc 4.6.2 c89 I have the following 2D array that I want to pass
gcc 4.7.2 c89 Hello, I am getting the following warning: pointer/integer type mismatch in

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.