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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:17:34+00:00 2026-06-02T07:17:34+00:00

I want to create a function which gets a void pointer. This pointer points

  • 0

I want to create a function which gets a void pointer. This pointer points to an arbitrary user data. This is not relevant now. Which is more important a descriptor, which describe this user data. It is under this data. I want to get it’s address, there are two different solution:

struct data_desc {
     size_t size;
     data_type_t type;
     /* And so on... */
};

/* Operate on the descriptor */
void operate_on_data(void *ptr)
{
    struct data_desc *desc;
    /* Now I want to get the desc /*

    /* This is the first approach, simply fails */
    desc = ((struct data_desc *)ptr) - sizeof(struct data_desc);
    /* This is the second, it works...*/
    desc = (struct data_desc *)ptr;
    desc--; 
    /* Do something with desc */
}

As you can see I use two diffrent approach to get the descriptor’s address. I thought the first is more natural, but it’s just not works. I used more parentheses to avoid the precedence problems.

I know that these methods are not safe. The first does not work, why? What is the reason behind this behavior?

Thanks in advance!

  • 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-06-02T07:17:36+00:00Added an answer on June 2, 2026 at 7:17 am

    The problem is pointer arithmetic. When you take a pointer and subtract 1, you are really subtracting (1*sizeof(struct)). The first equation with the “-sizeof”, you are really subtracting (sizeof(struct) * sizeof(struct)) bytes from the pointer. Make sense?

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

Sidebar

Related Questions

I want to create a macro (or function) which will contain package-prefixed symbols. This
I want to create a function which grabs every users latest tweet from a
I want to create a simple menu function which can call it example get_menu()
I want to create NUnit test to ensure that my function does not throw
I want to create a function, which has a paramater ( a string which
I want to create one insert function in php, which I will use to
I created webservice which contain Add function as 'webmethod' when we want to deploy
I want to create a function such that each time the function runs, the
I want to create SQL function with JDBC CREATE OR REPLACE FUNCTION TEST.TO_STRING( nt_in
I want to create a function that takes a 2 dimensional list and outputs

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.