Click here to Skip to main content
13,793,266 members
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 29 Feb 2004
Licenced CPOL

Pointer Arithmetic and Portable Code

, 3 Mar 2004
Rate this:
Please Sign up or sign in to vote.
Avoid writing code that depends on pointer arithmetic to make your code portable.


Once, one of my students after completing her degree, went to a job interview and got this question in her test.

char* pC = "Hello World";

int* pInt = (int*)pC;
char* pChar = (char*)pInt;

cout << *pChar << endl;

She was asked to guess the output of this program. She tried her best to answer the question. After coming back home, she contacted me to confirm her understanding. I was surprised to see this code, due to two reasons. First, the cout clearly shows that code is written in C++, so it is not recommended to use C style cast in C++ code [MEY98]. They should use new C++ style cast, so first improvement in the code should be something like this:

char* pC = "Hello World";

int* pInt = reinterpret_cast<int*>(pC);
char* pChar = reinterpret_cast<char*>(pInt);

cout << *pChar << endl;

Although this code is now better than previous one and standard C++ code, which will compile on any standard C++ compiler, it is not portable. The output of this code depends on the platform on which this program will run. According to the Standard of C++, section, "Plain integers have the natural size suggested by the architecture of the execution environment." [ISO98].

Well, one might think of using the sizeof operator. Wait before we discuss the problems of sizeof, remember you are doing pointer arithmetic here and addition of 1 in integer pointer is not add one in its address. In addition, the output of sizeof is also not portable across different platforms. According to section 5.3.3 of C++ standard, "the result of sizeof applied to any other fundamental type is implementation defined."[ISO98]. Here, any other means other than char, signed char and unsigned char types.

The increment of pointer is 4 bytes on 32 bits platform and 2 bytes on 16 bits platform. The output of this program is "o" where the size of integer is 4 and "l" where the size of integer is 2 bytes.

This is not limited to character pointer only, in fact the size of bool and wchar_t is also implementation dependent [ISO98] and any code assuming any assumption about its size are not portable.

char* pC = "Hello World";
pC += sizeof(int);

cout << *pC << endl;

And similarly, this code is not portable too:

char* pC = "Hello World";
pC += sizeof(bool);

cout << *pC << endl;

It is even worst when you call a function, which internally uses pointer arithmetic and you pass different types as parameters to it.

void fun(wchar_t* pC)
    int iLen = strlen(reinterpret_cast<char*>(pC));
    // do something

The value of iLen is one where wchar_t is implemented as multi byte characters instead of the actual length of the string, because NULL is placed after each character of the string. Some situations are even more dangerous when you try to write in memory using pointer arithmetic directly or indirectly. One such example is:

void fun(wchar_t* pC1, wchar_t* pC2)
    // Do something
    // Do something

This code may run correctly on some platforms where char and wchar_t are same but it may crash on some of them where these are not same. Write portable code across all the platforms. Do not assume anything about the size of fundamental types and be careful when using pointer arithmetic.

Thanks to Mahwish Waheed Khan to share her experience and give me example code, which is not portable across platforms.


  1. [ISO98] International Standard Programming Language C++ ISO/ICE 14882
  2. [MEY98] Effective C++ 50 specific ways to improve your programs and design, 2nd edition, Scott Meyers


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Zeeshan Amjad
Team Leader American Institute for Research
United States United States
Working as a Team leader in American Institute for Research

You may also be interested in...


Comments and Discussions

GeneralIt's really C code that used cout instead of printf Pin
scienceprogrammer18-Jan-09 19:43
memberscienceprogrammer18-Jan-09 19:43 
Generalreinterpret_cast&lt;type *&gt;(...)&amp;(type *)... Pin
TKD10-Mar-04 1:25
memberTKD10-Mar-04 1:25 
GeneralRe: reinterpret_cast&lt;type *&gt;(...)&amp;(type *)... Pin
Mike Dimmick10-Mar-04 2:32
memberMike Dimmick10-Mar-04 2:32 
QuestionIs sizeof(char) guaranteed to be one byte/machine word? Pin
Don Clugston1-Mar-04 15:14
memberDon Clugston1-Mar-04 15:14 
AnswerRe: Is sizeof(char) guaranteed to be one byte/machine word? Pin
rockonedge1-Mar-04 15:37
memberrockonedge1-Mar-04 15:37 
GeneralRe: Is sizeof(char) guaranteed to be one byte/machine word? Pin
Don Clugston1-Mar-04 18:12
memberDon Clugston1-Mar-04 18:12 
Generalmissing the point of the test Pin
Harold Bamford1-Mar-04 6:21
memberHarold Bamford1-Mar-04 6:21 
GeneralRe: missing the point of the test Pin
WREY1-Mar-04 8:07
memberWREY1-Mar-04 8:07 
GeneralRe: missing the point of the test Pin
Harold Bamford1-Mar-04 8:13
memberHarold Bamford1-Mar-04 8:13 
GeneralRe: missing the point of the test Pin
WREY1-Mar-04 11:52
memberWREY1-Mar-04 11:52 
GeneralRe: missing the point of the test Pin
JohnTesla9-Sep-06 12:02
memberJohnTesla9-Sep-06 12:02 
AnswerRe: missing the point of the test Pin
Harold Bamford11-Sep-06 6:29
memberHarold Bamford11-Sep-06 6:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.181207.3 | Last Updated 4 Mar 2004
Article Copyright 2004 by Zeeshan Amjad
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid