473,379 Members | 1,326 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,379 software developers and data experts.

two pointers same address

#include <stdio.h>
int main(void)
{
int a=1,*p=&a,*q=p;
printf("%p %p",p,q);
return 0;
}
I have a doubt in the above code. There are two pointers p and q .when i print the address of p and q it comes out to be same. how is this possible. I know that both are pointing to same location but still they are two different variables then address corresponding to them sud also be different then how come they have same address??
Apr 10 '10 #1
9 4805
weaknessforcats
9,208 Expert Mod 8TB
You are not printing the address of p and q. You are printing the value of p and q. If these were ints you would see the value of the ints.

To see the addresses of p and q, print &p and &q.
Apr 10 '10 #2
whodgson
542 512MB
Both p and q hold the address of 'a' 'which is not the address of p or q. If p and q are two different pointers their addresses will be different also.
Apr 11 '10 #3
#include <stdio.h>
int main(void)

{
int a=1,*p=&a,*q=p;
printf("%p %p",p,q);
p++;
printf("\n%p",p);

getch();
}
LOok at this code snippet.
Why does this gives 1 i thought it will print 4 on my machine as int takes 4 bytes on my machine.

Also if i use this line
printf("\n%p",(char*)p-(char*)q);
it prints 4 .
Iam unable to makeout what is going on.
someone look into this problem.
Apr 11 '10 #4
newb16
687 512MB
On my mingw setup if prints
0022FF74 0022FF74
0022FF78
, the value in the second line is 4 bytes higher than in the first line, as expected.
Apr 11 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
The %p formatter is implementation dependent. On Visual Studio.NET 2008 I get 0012FF60 which is the address of the int.
This code:

Expand|Select|Wrap|Line Numbers
  1. printf("\n%p",(char*)p-(char*)q); 
tells the compiler to subtract two pointers that are char*. The answer is 4 because the difference between the two addresses is 4. You see, that cast is a lie. These are pointers to int. When you incremented p, you incremented by the sizeof(Int), which is 4 bytes. Had you told the truth and subtracted p-q as pointers to int you would have got the1 instead. That is, the addresses are separated by the sizeof(int).
Apr 11 '10 #6
I made a mistake in asking the question

#include <stdio.h>
int main(void)

{
int a=1,*p=&a,*q=p;
printf("%p %p",p,q);
p++;
printf("\n%p",p-q);
}

Why does this prints 1 but i expected it to print 4.
If p initially points to 1000 and q also to 1000 after p++ p will ;point to 1004
and when i take the difference it sud print 4 but why is it printing 1.
This is my question.
Apr 11 '10 #7
newb16
687 512MB
It's pointer arithmetic. When you increment it ( p++ ) it goes forward four bytes, the sizeof(int) on your platforn. The result of subtraction for two pointers is not a number of raw bytes between them, but number of the items to which they point ( that is, one 'int' )
Apr 11 '10 #8
You mean the difference gives the number of elements between two pointers
Apr 11 '10 #9
newb16
687 512MB
Yes, number of elements between them.
Apr 11 '10 #10

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

Similar topics

388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
5
by: apropo | last post by:
Hi, i understood what pointers are and all stuff, and now i'm at the part with pointers to pointers and my book doesn't really help on this issue. let be: int a,*p,**q; how to make use of **q,*q...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
12
by: arganx | last post by:
Before I had a good idea how to use pointers, I was wondering - first, how to use them. And second, of what value would they be. Now that I have at least a pretty good idea of how to use them, I...
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
4
by: jagguy | last post by:
can you declare variables as pointers without using new or make them point at another variable. this works I thought it shouldn't char *p ; p="xat"; cout << p <<endl;
7
by: Eli Luong | last post by:
Hi, Sorry if I accidentally posted a blank post before. But, I was wondering, I'm practicing with pointers right now, and wanted to know if I am using it correctly in the following code. By...
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.