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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:53:46+00:00 2026-06-18T01:53:46+00:00

I have a simple function in C which provides a void* pointer to a

  • 0

I have a simple function in C which provides a void* pointer to a data array. I know the size (in bytes) of each individual data-point within this memory block, and need to guarantee that I can modify each data-point in this block without accidentally altering neighboring data-points. In this example, I want to decrement each value by 1.

All data points are either 8-bit, 16-bit, or 32-bit.

eg:

void myFunction(void* data, size_t arraySize, size_t widthPerDataPoint)
{
  if(!data)
    return -1;
  size_t w = widthPerDataPoint;
  int numPoints = arraySize / widthPerDataPoint;
  int i;
  for(i=0; i<numPoints; i++)
  {
    if(w==1)       // 8 bit
      (*((int8_t*)data + i))--;
    else if(w==2)  // 16 bit
      (*((int16_t*)data + i))--;
    else if(w==4)  // 32 bit
      (*((int32_t*)data + i))--;
  }
}

Unfortunately, the int8_t, etc, datatypes only guarantee their minimum size, according to C99 specifications, and not an exact size. Is there any way to re-cast and modify the data in-place and guarantee I won’t smash my array or touch neighboring data points? Also, is there an equivalent technique that would somehow work for other data widths (ie: 24-bit, 60-bit, etc)?

  • 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-18T01:53:48+00:00Added an answer on June 18, 2026 at 1:53 am

    int8_t is guaranteed to be exactly 8 bits, and if CHAR_BIT==1, exactly 1 byte.

    Quoting the N1570 draft of the latest C standard, section 7.20.1.1:

    The typedef name intN_t designates a signed integer type
    with width N, no padding bits, and a two’s complement
    representation. Thus, int8_t denotes such a signed integer type
    with a width of exactly 8 bits.

    Though for your purposes it might make more sense to use uint8_t, uint16_t, et al.

    If the implementation doesn’t support types with the required characteristics, it won’t define them; you can detect this by checking, for example:

    #include <stdint.h>
    #ifdef UINT8_MAX
    /* uint8_t exists */
    #else
    /* uint8_t doesn't exist */
    #endif
    

    (If CHAR_BIT != 8, then neither int8_t nor uint8_t will be defined.)

    It’s the [u]intleast_N_t and [u]intfast_t types for which the standard only guarantees minimum sizes.

    You’ll have to guarantee that both the array and the offsets within it are properly aligned for the types you’re using to access it. I presume you’re already taking care of that.

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

Sidebar

Related Questions

I have a simple function which takes an array of characters as an argument,
I have a simple C function which I declare as: int strLen(const char* str_)
Using xlwDotNet, I have created a simple function which returns one number. Sometimes, something
I have a simple function, which I shall call myFunction . It takes two
I have written a simple jQuery.ajax function which loads a user control from the
I have a simple asp page with a script including a function myFunction which
I have a relatively simple class which deletes a post: function delete_post($postid, $reason){ //Stuff
I have a simple function that looks at an incoming mySQL data type and
I have a simple function as the following: static Task<A> Peirce<A, B>(Func<Func<A, Task<B>>, Task<A>>
I have a simple function that works in chrome, but in Internet explorer (9)

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.