473,473 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding to char*

60 New Member
Hi Guys,
I have a segment of code here that I am not able to analyze
Expand|Select|Wrap|Line Numbers
  1. template <typename FieldType>
  2. int CheckHeaderField(
  3.                 FieldType& field, const char* buffer, unsigned long bufferSize,
  4.                 bool continueParse
  5.         )
  6. {
  7.         const char* fieldptr = reinterpret_cast<const char*>(&field);
  8.         const char* endptr = buffer + bufferSize;
  9.         AliHLTUInt32_t bufferRemaining = endptr > fieldptr ? endptr - fieldptr : 0;
  10.         /*....And the rest of the code here*/
  11. }
  12.  
What is the meaning of adding a variable of unsigned long to a variable of char*
I printed endptr but the output is just funny characters.
Jul 2 '07 #1
1 2042
weaknessforcats
9,208 Recognized Expert Moderator Expert
When you add to a pointer, the compiler does a calculation to get the correct address as a result:
Expand|Select|Wrap|Line Numbers
  1. int arr[5];
  2. int* ptr= arr;
  3.       ptr = ptr + 3;
  4.  
Assume the array is located at address 1000:

1000 arr[0]
1004 arr[1]
1008 arr[2]
1012 arr[3]
1016 arr[4]

So, ptr is initialized to the address of arr[0].

When you add to a pointer, the address in the pointer is increased by the sizeof the type it points at. Here ptr is an int*. So, ptr + 3, will be
ptr + 3 * sizeof(int). sizeof(int) is 4, so ptr is increased by 3*4, or 12.

Location 1012 is &arr[3]. It is also &ptr[3]. It is also ptr + 3.

To use the int at that location you can use arr[3], ptr[3], or *(ptr+3).
Jul 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Iker Arizmendi | last post by:
Hello all. Is there a convenient scheme within a C extension to add methods to a type in such a way as to allow me to transparently add a "proxy" around them? For example: typedef PyObject*...
1
by: Sagaert Johan | last post by:
Hi I am construncting a string containing some control chars (STX/ETX) I noticed that adding a byte with value 2 is the same as adding a character '2' ??? How can i solve this problem ?...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
4
by: thoseion | last post by:
Hi, I am trying to get a program working whereby directory and file names are read into a list. I have been given the original list structure - it appears that the directory names should be added...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.