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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:57:11+00:00 2026-05-27T06:57:11+00:00

I’m trying to make 128 and 256 bit integers in C++, and noticed casting

  • 0

I’m trying to make 128 and 256 bit integers in C++, and noticed casting char** to int* and int* to int (and backwards) can be used to convert char arrays to integers and integers to char arrays.
Also, char* + int works fine.

However, when I try char* + char* the compiler tells me the types are invalid. Is there any workaround for this, or will I have to write my own functions for the operators?

For example:

int32_t intValue = 2147483647;
char *charPointer = *( char** ) &intValue;
charPointer += 2147483647;
charPointer += 2;
cout << ( *( int64_t* ) &charPointer )  << endl;

output: 4294967296

Basically, what I do should be something like the following:

int32_t intValue = 2147483647;

somewhere in memory:

[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. FF FF FF 7F .. .. ] ( value, in hex )

then:

char *charPointer = *( char** ) &intValue;

somewhere in memory:

[ 58 59 5A 5B 5C 5D 5E 5F ] ( address, in hex )
[ .. .. 07 00 00 00 .. .. ] ( value, in hex )

then:

charPointer += 2147483647;

I honestly have no idea what happens here.
It seems like it does something like this though:

[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. FF FF FF FE .. .. ] ( value, in hex )

then:

charPointer += 2;

Same here.
Something like this:

[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. 00 00 00 00 01 .. ] ( value, in hex )

And at last I just print it as if it were an 8 byte integer:

cout << ( *( int64_t* ) &charPointer )  << endl;

So, can anybody explain why it isn’t the value of the pointer that is added but the value of what’s being pointed to?

  • 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-27T06:57:12+00:00Added an answer on May 27, 2026 at 6:57 am

    These conversions exist, but they don’t do what you think they do. Converting a pointer to an integer is just treating it as an integer; it’s not doing any actual “math”. For example, char * s = "abcd"; int i = (int) s; will not give the same result every time, because s and i are both just the memory address that the string starts at. Neither has anything to do with the actual contents of the string.

    Similarly, char* + int is just taking an offset. To write char * s = "abcd"; char * t = s + 2; is just another way to write char * s = "abcd"; char * t = &(s[2]);; that is, s is the memory location of the 'a', and t is the memory location of the 'c' (s, offset by two char-widths, that is, two bytes). No actual math has taken place, except in the sense that “pointer arithmetic” requires math to compute byte offsets and find memory locations.

    char * + char * doesn’t make sense: what would it mean to “add” two memory locations together?

    Edit: Here is the code you added to your question:

    int intValue = 5198;
    char *charValue = *( char** ) &intValue;
    charValue += 100;
    cout << ( *( int* ) &charValue )  << endl;
    

    Let me expand it a bit, so it’s a bit clearer what’s going on:

    int intValue = 5198;
    int * intPtr = &intValue;
    // intPtr is now the address of the memory location containing intValue
    char ** charPtrPtr = (char**) intPtr;
    // charPtrPtr is now the address of the memory location containing intValue,
    // but *pretending* that it's the address of a memory location that in turn
    // contains the address of a memory location containing a char.
    char *charPtr = *charPtrPtr;
    // charPtr (note: you called it "charValue", but I've renamed it for clarity)
    // is now intValue, but *pretending* that it's the address of a memory
    // location containing a char.
    charPtr += 100;
    // charPtr is now 100 more than it was. It's still really just an integer,
    // pretending to be a memory location. The above statement is equivalent to
    // "charPtr = &(charPtr[100]);", that is, it sets charPtr to point 100 bytes
    // later than it did before, but since it's not actually pointing to a real
    // memory location, that's a poor way to look at it.
    char ** charPtrPtr2 = &charPtr;
    // charPtrPtr2 is now the address of the memory location containing charPtr.
    // Note that it is *not* the same as charPtrPtr; we used charPtrPtr to
    // initialize charPtr, but the two memory locations are distinct.
    int * intPtr2 = (int *) charPtrPtr2;
    // intPtr2 is now the address of the memory location containing charPtr, but
    // *pretending* that it's the address of a memory location containing an
    // integer.
    int intValue2 = *intPtr2;
    // intValue2 is now the integer that results from reading out of charPtrPtr2
    // as though it were actually pointing to an integer. Which, in a perverse
    // way, is actually true: charPtrPtr2 pointed to a memory location that held
    // a value that was never *really* a memory location, anyway, just an integer
    // masquerading as one. But this will depend on the specific platform,
    // because there's no guarantee that an "int" and a pointer are the same
    // size -- on some platforms "int" is 32 bits and pointers are 64 bits.
    cout << intValue2  << endl;
    

    Does that make sense?

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string

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.