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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:11:48+00:00 2026-05-23T17:11:48+00:00

================================================================ typedef struct { union { struct { char fn[5]; char ln[5]; } fullname;

  • 0

================================================================

typedef struct {
    union {
        struct {
            char fn[5];
            char ln[5];
        } fullname;
        char name[5+5+1];
    }
    unsigned int age;
    unsigned int area_code;
} my_struct;

The above is a struct that I have no control over. I personally am not a fan, but the struct is “legal”.

================================================================

My code:

void caller {
    my_struct str;
    str = (my_struct){}; //initialise
    fill(&str);

    printf("%s [%s/%s]\n", str.name, str.fullname.fn, str.fullname.ln); // PROBLEM!
}

void fill(my_struct * str) {
    //first name
    printf("Enter first name: ");
    fgets(str.fullname.fn, sizeof(str.fullname.fn), stdin);
    if (str.fullname.fn[strlen(str.fullname.fn) - 1] == '\n')
        str.fullname.fn[strlen(str.fullname.fn) - 1] = '\0';

    //last name
    printf("Enter last name: ");
    fgets(str.fullname.ln, sizeof(str.fullname.fn), stdin);
    if (str.fullname.ln[strlen(str.fullname.ln) - 1] == '\n')
        str.fullname.ln[strlen(str.fullname.ln) - 1] = '\0';

    sprintf(str.name, "%s %s", str.fullname.fn, str.fullname.ln);

    printf("Age: ");
    scanf("%ud", &str.age);
    getchar();

    printf("Area Code: ");
    scanf("%ud", &str.area_code);
    getchar();
}

================================================================

If the input was:

  • joe
  • moe
  • 18
  • 214

The printout at // PROBLEM is:

joe moe [joe moe/oe]

instead of

joe moe [joe/moe]

Any ideas? I cannot, for the life of me, figure out why the values of fn and ln are changing…

  • 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-23T17:11:48+00:00Added an answer on May 23, 2026 at 5:11 pm

    The problem is that name and fullname share the memory (because of the union). So

    sprintf(str.name, "%s %s", str.fullname.fn, str.fullname.ln);
    

    also writes over fn and ln.

    Not a bad question, but I don’t really see how to cleanly solve this. The way I’d do it: get rid of the sprintf above, and do it on your own.

    void caller
    {
        fill(&str);
    
        printf("%s %s [%s/%s]\n", str.fullname.fn, str.fullname.ln, str.fullname.fn, str.fullname.ln);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct: typedef struct _n { int type; union { char *s;
I have following struct defination: typedef union U08_16_t { unsigned long s32;//message32 unsigned char
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
typedef struct Model { int recordId; char *name; }Model; typedef struct ModelArray { //keeps
I have a struct like this typedef struct bookStruct { char title[80]; char author[80];
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have the following construction: typedef struct bucket { char *key; ENTRY *data; struct
typedef struct { unsigned char a, unsigned char b, unsigned char c }type_a; typedef
typedef struct What_if { char price [2]; } what_if ; what_if what_if_var[100]; int format_input_records();
typedef union _Value { signed char c; unsigned char b; signed short s; unsigned

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.