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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:34:05+00:00 2026-05-16T03:34:05+00:00

here is code which returns size of struct without using sizeof keyword #include <iostream>

  • 0

here is code which returns size of struct without using sizeof keyword

#include <iostream>
using namespace std;
struct  point{
    int x;
    int y;
    };
struct point pt={0,0};

int main(){
    point *ppt=&pt;
    unsigned char *p1,*p2;
    p1=(unsigned char *)ppt;
    p2=(unsigned char *)++ppt;
    printf("%d",p2-p1);


     return 0;
}

it returns 8 as i understand because sizeof char is 1 byte and this struct contains integer types first it convers it to char using char pointers and returns sizeof char? or?i dont understand exactly how it works
thanks

  • 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-16T03:34:06+00:00Added an answer on May 16, 2026 at 3:34 am

    The cast here happens after the ++

    p2 = (unsigned char *)++ppt;
    

    It works because ++ on a pointer increases the pointer the number of bytes equal to the size of the type pointed to. Then you cast to char, because minus divides the difference in pointers by the size of the type (so divide by 1 because it’s now char*).

    One caveat if you plan to use this — sizeof is definitely done at compile time where this code may or may not be recognized by the optimizer as being a constant expression.

    Also, as pointed out by the commenter and other question, it won’t match sizeof if the type needs alignment (some systems require that types start on memory boundaries divisible by 2, 4, etc).

    Finally, (from the comments), once the pointer has been incremented, it is invalid and cannot be used (even for subtracting, comparison — i.e. even in ways that don’t dereference it)

    From the C Rationale Document: http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf

    Implicit in the Standard is the notion
    of invalid pointers. In discussing
    pointers, the Standard typically
    refers to “a pointer to an object” or
    “a pointer to a function” or “a null
    pointer.” A special case in address
    arithmetic allows for a pointer to
    just past the end of an array. Any
    other pointer is invalid.

    An invalid pointer might be created in
    several ways. An arbitrary value can
    be assigned (via a cast) to a pointer
    variable. (This could even create a
    valid pointer, depending on the
    value.) A pointer to an object becomes
    invalid if the memory containing the
    object is deallocated or moved by
    realloc. Pointer arithmetic can
    produce pointers outside the range of
    an array.

    Regardless how an invalid pointer is
    created, any use of it yields
    undefined behavior. Even assignment,
    comparison with a null pointer
    constant, or comparison with itself,
    might on some systems result in an
    exception.

    Consider a hypothetical segmented
    architecture on which pointers
    comprise a segment descriptor and an
    offset. Suppose that segments are
    relatively small so that large arrays
    are allocated in multiple segments.
    While the segments are valid
    (allocated, mapped to real memory),
    the hardware, operating system, or C
    implementation can make these multiple
    segments behave like a single object:
    pointer arithmetic and relational
    operators use the defined mapping to
    impose the proper order on the
    elements of the array. Once the memory
    is deallocated, the mapping is no
    longer guaranteed to exist. Use of the
    segment descriptor might now cause an
    exception, or the hardware addressing
    logic might return meaningless data.

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

Sidebar

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could try cycling through its subviews and look for… May 17, 2026 at 2:19 am
  • Editorial Team
    Editorial Team added an answer Check the history on the order, it's possible that there… May 17, 2026 at 2:19 am
  • Editorial Team
    Editorial Team added an answer This is normal, manually firing a click on an anchor… May 17, 2026 at 2:19 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have some struct containig a bitfield, which may vary in size. Example: struct
In a project I'm writing code for, I have a void pointer, implementation, which
I had a problem with a part of my code, which after some iterations
Here is my code int main(int argc,char *argv[]) { mysql_init(&mysql); connection = mysql_real_connect(&mysql,.............,3306,0); if
I'm trying to write a little helper for Windows which eventually will accept a
I have a memory mapped file, from which I wish to parse the contents
I am trying to implement a function called inet_pton which will convert a string
I've got a button which when clicked copies and appends the text from a
Let's say you have the following structure in C#: struct Piece : IEquatable<Piece> {
Yesterday I discovered an odd bug in rather simple code that basically gets text

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.