473,396 Members | 1,866 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.

Doubt in program

Hi All,
First Wish you a happy new year.

Please conform me, my understanding is correct on below program.

#include <stdio.h>

main()
{
int *p;

printf("%d", ((int )(p+1) - (int )p));
}



If I remove cast operation (int ). compiler explicitly will give the difference between the int * pointer. ie. 1.

If I use cast operator, compiler will update the difference of two integer..ie 4.

Thanks in advance.
Jan 1 '10 #1
3 1399
newb16
687 512MB
I'm not sure about 'update' part of 'compiler will update the difference of two integer'. If we assume that int and pointer are 32 bit, then yes, most likely it will return differnce in byte addresses between this and next element pointer by p, i.e. 4. However "The format in which this integer value represents a pointer is platform-specific". It's not an exact quote from the standard, but it means that there may be a system (with 32 bit integer and pointer) where it will return something other than 4.
Jan 1 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
When you subtract two pointers, the compiler subtracts the two addresses and then divides the answer by the sizeof the type. So the different between p+1 and p is 1 int.

The pointers contain addresses. Not int values. That cast tells the compiler to treat the addresses as int values. What you see is the difference between the addresses in bytes. On a 32-bit system an int is usually 4 bytes.
Jan 1 '10 #3
Thanks for you reply
Jan 2 '10 #4

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

Similar topics

0
by: mahesh | last post by:
Hi all, I have a doubt with OOAD and Dialogs. I have a Dialog Class. tkSimple.py It contains a class body as below. Now I am calling the tkSimple.py in another python program and using the...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
8
by: raajagopal.v | last post by:
i am a beginner to comp sci field , i am a student , when i came to the storage class specifiers area i got this doubt that is "i want to store a variable in the register and that must be of static...
77
by: muttaa | last post by:
Hello all, My doubt is going to be so primitive that i ask you all to forgive me beforehand.... Here's the code snippet: int main() { int x=5;
13
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only...
5
by: subramanian | last post by:
Consdier the following program: #include <stdio.h> struct typeRecord { int id; char str; } records = { {0, "zero"}, {1, "one"},
2
archulu
by: archulu | last post by:
hai this is archulu, i have a some confusion and doubt in my downloading program.that doubt was in my program i am upload some gif and img file to some path.it's good, after usage of that image i...
1
by: sundarpalaniappan | last post by:
i had a doubt in fat32 program i want to know what type of parameter i want to give in SOURCE_MOUNT_FILE_AS_DRIVE i want to know how to convert the drive in to image file and i want to what are all...
2
by: rajendra | last post by:
hi friends, i have placed code because i want to explain my doubt clearly to you ,and i have placed my doubt in Main of the program (static void Main(string args)) by // before that ...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.