473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Type of result of pointer arithmetic

int main( void ) {
int foo[10], *bar=foo, *baz=foo+1;
int quux=baz-bar;
return 0;
}

Is the type of quux (that is, int) strictly correct? Or is there a
type similar to size_t that is more appropriate for the difference of
two pointers?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 2 '05 #1
2 1110
On 2005-12-02, Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
int main( void ) {
int foo[10], *bar=foo, *baz=foo+1;
int quux=baz-bar;
return 0;
}

Is the type of quux (that is, int) strictly correct? Or is there a
type similar to size_t that is more appropriate for the difference of
two pointers?


ptrdiff_t
Dec 2 '05 #2
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
int main( void ) {
int foo[10], *bar=foo, *baz=foo+1;
int quux=baz-bar;
return 0;
}

Is the type of quux (that is, int) strictly correct? Or is there a
type similar to size_t that is more appropriate for the difference of
two pointers?


ptrdiff_t is correct (as Jordan Abel has already pointed out). On the
other hand, that doesn't mean the code above is wrong. As long as you
know the result won't exceed 32767, there's nothing wrong with
assigning it to an int and taking advantage of the implicit
conversion.

But declaring quux as a prtdiff_t is more robust.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 2 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
6
by: Pushkar Pradhan | last post by:
I tried to read the archives and solve this problem, but now I think I better post my problem: int main() { int blkSz = { {2,2}, {2,3}, ....., {6,6} }; write_bc_perf(mflops1, blkSz,...
15
by: Chris Readle | last post by:
Hi all, Somewhat new to C and I'm getting the following error from my latest code. Here's the warning I'm getting: chris_readle_project3_assignment3.c: In function `main':...
14
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable =...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
6
by: KOFKS | last post by:
I have come across a problem when I'm reading "comp.lang.c FAQ list · Question 12.1". This item is about char c; while((c = getchar()) != EOF) ... One paragraph of the item writes:"If type...
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.