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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:49:16+00:00 2026-05-31T00:49:16+00:00

I declared the following struct in my C++ program: struct person { char name[10];

  • 0

I declared the following struct in my C++ program:

    struct person {
        char name[10];      /* first name */
        char id[10];        /* ID number */
        off_t pos;      /* position in file, for demonstration */
    } people[] = {
        { "arnold", "123456789", 0 },
        { "miriam", "987654321", 10240 },
        { "joe",    "192837465", 81920 },
    };

        j = sizeof(people) / sizeof(people[0]);     /* count of elements */

gives j = 3 here, i.e, no of elements in the array; always even if you add or reduce the elements…

But

         char b[8];
         i = sizeof(b)/sizeof(b[0]);

gives the value of i = a constant = 4 on my machine.

Now thats justified as the sizeof(char*) is constant on my machine and the sizeof(char) too is constant..

But as soon as I declare the struct person, the sizeof(person*) and sizeof(person) should also be constant, and it should also yield a constant value, isn’t it???

  • 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-31T00:49:17+00:00Added an answer on May 31, 2026 at 12:49 am

    Your compiler is wrong.

    char b[8];
    i = sizeof(b)/sizeof(b[0]);
    

    should yield i==8.

    The result you’re getting for the struct size is correct. I would switch compilers if I were you.

    If you were to pass b to a function calculating the size, than you’d be right. But as the code is now, no.

    Also, if it was a function calculating sizeof(people) / sizeof(people[0]) which received people as a parameter, you’d also get a constant.

    This is because arrays decay to pointers when passed as arguments.

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

Sidebar

Related Questions

I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
I've declared the following bean in my Spring config <bean id=templateCacheClearingTask class=org.springframework.scheduling.timer.ScheduledTimerTask> <property name=delay
In following program . I have one doubt. I have declared one global variable
I currently have the following code in my program: struct mystruct { int x;
I have a struct that's defined with a large number of vanilla char* pointers,
I wrote the following program: typedef struct blahblah { int x; int y; }
I've declared the following array of strings: char *arrayIndices[100] = {0}; I do a
Consider the following program: #include <iostream> #include <algorithm> using namespace std; template<class T> struct
Say I have a struct declared like the following: public struct Test { public
How does the following code remove window borders? //note the struct is declared elsewhere,

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.