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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:47:27+00:00 2026-05-22T17:47:27+00:00

I have a pointer to a structure and I’d like to fetch all of

  • 0

I have a pointer to a structure and I’d like to fetch all of its members by trial and error. I’m trying to run through the structure by incrementing the pointer by one and derefencing it. It should return a correct value from the structure (every i*sizeof(int) time) but it doesn’t.

What am I doing wrong?

fn (mach_msg_header_t *InHeadP, mach_msg_header_t *OutHeadP)
{

    /*
     assume that all struct members are int types

     typedef    struct 
     {
     mach_msg_bits_t    msgh_bits;
     mach_msg_size_t    msgh_size;
     mach_port_t        msgh_remote_port;
     mach_port_t        msgh_local_port;
     mach_msg_size_t    msgh_reserved;
     mach_msg_id_t      msgh_id;
     } mach_msg_header_t;

     size of the struct is 24.
     */

    printf("ID: %d \n",InHeadP->msgh_id); //prints 'ID: 1337'
    printf("Ptr: %p\n",InHeadP);

    for (int i = 0; i <= 24; i++)
    {
        int deref = *((int*)(InHeadP+i));
        printf("InHeadP[%d]=%d\n",i,deref);
            //no sign of 1337 anywhere

    }
}

P.S. I know that I shouldn’t be doing that, but this is only for testing purposes.

  • 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-22T17:47:27+00:00Added an answer on May 22, 2026 at 5:47 pm

    Because InHeadP is a mach_msg_header_t*, adding an integer to it will actually add that integer times sizeof(mach_msg_header_t), as though you were indexing an array of mach_msg_header_ts (and is actually how array indexing works). You need to cast InHeadP to an int* before performing arithmetic on it, and even then, as the struct has six fields, i should only go up to 6, not 24.

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

Sidebar

Related Questions

I have a Linux C++ application and I'd like to test an object pointer
I have a pointer to a structure and I need to implement a method
I have a pointer of a structure type that I made. On program start
I am trying to pass a structure array pointer and a pointer to a
If I have structure definitions, for example, like these: struct Base { int foo;
I have an array of a large structure that I am trying to output
So I have a pointer to an array of pointers. If I delete it
How can I have a pointer to the next struct in the definition of
I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a
I have to use a smart pointer and I found shared_ptr from boost looks

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.