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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:59:57+00:00 2026-05-30T02:59:57+00:00

I created an union and I put different types of array inside of it.

  • 0

I created an union and I put different types of array inside of it. I printed outputs in an order and I really didn’t understand some points.

1) Why is my char array’s length always 8 even the content is different? There is only “hello” inside of it. And why does the output is “Cats rock!” when I try to print for second time. I didn’t put anything like that inside array.

2)Again length problem. Lenghts of all my arrays are 8 even the length of union. Why?

3) My last question is why double number’s value changed when I try to print for the second time.

I’m posting you my code and out put that I get. Sorry about long post, but I’m really confused.

char: hello, 8
double: 5.557111111111111, 8
int: 1937006915 1668248096 8555, 8

char: Cats rock!
double: 0.000000000000000
int: 1937006915 1668248096 8555
size of union: 8

my code

#define NUM1 5.557111111111111

#define NUM2 1937006915
#define NUM3 1668248096
#define NUM4 8555
#include <stdio.h>

/*created an union*/
typedef union {
    char  * array1;
    double num;
    int * array2;
} myunion;

int main(int argc, char ** argv)
{
    /* declaring union variable */
    myunion uni;
    /* creating my string */
    char strarray[] = "hello";

    /* declare an int array*/
    int numarray[] = { NUM2, NUM3, NUM4 };

    /* assign the string to the char pointer in the union */
    uni.array1 = strarray;

    /*  print the union and the sizeof of the pointer */
    printf("char: %s, %zu\n", uni.array1,sizeof(uni.array1));

    /* assign NUM1 to the double part of union */
    uni.num = NUM1;

    /* print the double and its sizeof */
    printf("double: %10.15f, %zu\n", uni.num, sizeof(uni.num));

    /* assign array2 of union  to the numarray */
    uni.array2 = numarray;

    /* print the values and the sizeof */
    printf("int: %d %d %d, %zu\n", uni.array2[0], uni.array2[1], uni.array2[2], sizeof(uni.array2));

    /* print the char array, double and int array */
    printf("\nchar: %s \ndouble: %10.15f \nint: %d %d %d\n",uni.array1, uni.num, uni.array2[0], uni.array2[1], uni.array2[2]);

    /* print the size of the union */
    printf("size of union: %zu\n", sizeof(uni));

    return 0;
}
  • 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-30T02:59:58+00:00Added an answer on May 30, 2026 at 2:59 am

    sizeof(uni.array1) is always 8 on 64-bit platforms and 4 on 32-bit ones because it is taking the size of a pointer, not knowing how much data you believe might be behind that pointer. Similar for the arrays. C pointers which you make point to an array are “dumb” and do not understand how large the array is–you need to pass that information around separately.

    This covers your question parts 1 and 2. We like to answer specific questions here, so feel free to move your third inquiry to a separate post.

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

Sidebar

Related Questions

If have a union that currently foresees some simple data types: union Value {
I am creating a web app which has some complex underlying associations. In order
I created a MySQL view using UNION ALL from two tables so that I
I have created a simple type <xsd:simpleType name=IntOrBlank> <xsd:union memberTypes=xsd:int> <xsd:simpleType> <xsd:restriction base=xsd:string> <xsd:enumeration
I have two array I need to merge, and using the Union (|) operator
I am trying to create a view for a UNION of 2 select statements
Created .NET WCF service, tested it - works. Generated schemas from Data and service
Created Private Key & Self signed certficate in a Key Store keytool -genkey -alias
Created an OpenGraph action and object. Trying to submit my action. When I click
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user

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.