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

  • Home
  • SEARCH
  • 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 9123465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:20:49+00:00 2026-06-17T06:20:49+00:00

Have a problem accessing some elements of a structure in a right way after

  • 0

Have a problem accessing some elements of a structure in a right way after typecasting.
Here my code:

void get_description(struct shmstruct *ptr/*, int number*/) {
    char buff[MESGSIZE];
    struct shmData *dparse;
    snprintf(buff, MESGSIZE, "%s", &ptr->msgdata[0]);
    dparse = (struct shmData *) buff;
    printf("Number: %s", dparse->number);
    printf("Description: %s", dparse->description);
}

Problem now is, that I get the number, like 123, but also the description in the first
line ->
printf(“Number: %s”, dparse->number);
like: 123 description

How can I get only the number?

(P.S.

struct shmData{
    char number[4];
    char description[1020];
};

)

  • 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-17T06:20:50+00:00Added an answer on June 17, 2026 at 6:20 am

    It sounds like the number is not nul-terminated. You have the number field and then immediately following you have the description field. printf() assumes that you are giving it a nul-terminated string, and it will keep going until it hits the terminating nul character. It looks like in your case, there isn’t a nul terminating the number field, so printf() just keeps going and gets the description also.

    Can all four characters from number be used for digits?

    If the numbers are only 3-digits or fewer, then you can put a terminating nul right into the number buffer. If you might need to read a 4-digit number from there, you need to copy out the digits into a temp buffer of at least 5 chars, and then nul-terminate.

    // max 3 digits:
    dparse->number[3] = '\0'; // ensure nul-termination of number
    
    // max 4 digits:
    #define DPARSE_NUMBER 4  /* size of number field in dparse */
    char temp[DPARSE_NUMBER + 1];
    strncpy(temp, dparse->number, sizeof(temp));
    temp[sizeof(temp) - 1] = '\0'; // strncpy() does not guarantee to nul-terminate, so we must do this
    

    strncpy() doesn’t guarantee to nul-terminate, which is unfortunate. So we should always put in a terminating nul character in the last position so that no matter what, it is nul-terminated. Note that sometimes strncpy() will put a nul for us, but it does no harm to make sure one is there.

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

Sidebar

Related Questions

i have problem with libhid . i found that there 2 way 4 accessing
I have a problem accessing JSON data. I'm new to JSON and jquery so
I am having a problem accessing the ViewData object through javascript. I have set
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have some JS code which generates the following object, return { type: some
I have some code that simply accesses a datetime field so activerecord converts it
I have a Problem accessing an Object function using this. In case of the
I have a problem accessing the values in my array for the purpose of
I'm having a problem accessing a class after deploying my website to a server.

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.