![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Adding The Values of Pointers
Why is the following program legal, but not the one beneath it? I'm adding the values of two pointers which works if I point them to an existing variable but it does not work if I initilize the pointer to a value.
LEGAL
int main()
{
int i = 3;
int j = 5;
int *x = &i;
int *y = &j;
int z = (int)*x+*y;
printf("%d\n", z);
return 0;
}ILLEGAL - SEGMANTATION FAULT
int main()
{
int *x = (int *)3;
int *y = (int *)5;
int z = (int)*x+*y;
printf("%d\n", z);
return 0;
} |
|
|
|
|
|
#2 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
If I'm not mistaken, which I very possibly am, when casting "3" and "5" to int pointers, you are not getting an int pointer that points to the number 3 or 5 in memory, but pointers to the addresses 3 and 5. When you dereference those addresses in the 3rd line, your error occurs. In the example that works, you are in fact getting int pointers that point to the numbers 3 and 5.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
I do not think that is quite right. My reasoning is because the following program will display the number three just fine. Compiler doesn't show any warnings and it executes perfectly and displays the number, even though I am initilizing it without pointing to an existing variable.
int main()
{
int *x = (int *)3;
printf("%d\n", x);
return 0;
} |
|
|
|
|
|
#4 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Well of course that will work. You aren't dereferencing x.
x is 3. *x is the integer at memory address 3, which isn't in your program's address space.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 203
Rep Power: 2
![]() |
Quote:
int *x = 3 ; you would (or should anyway), get a compiler warning. You can assign 0 to a pointer though, to indicate that it points nowhere. Btw, I think Dameon was right. |
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 203
Rep Power: 2
![]() |
"does not work if I initilize the pointer to a value."
http://www.csee.umbc.edu/201/spring0...ures/pointers/ Specifically, I have no idea what "int *x = (int *)3 ; does . It treats 3 as type int * which doesn't make any sense to me, other than the obvious. What it does in memory, I don't know. Dameon may have been correct in his first post, but I'm not sure either. also: int z = (int)*x+*y; would not work the way you want if y was type float. I'm pretty sure only *x is typecasted to an int. You should put the whole expression in parentheses, int z = (int)(*x+*y); |
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
|
Quote:
char *s = "Hello World!"; // legal int *i = 10; // illegal, but why? |
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 155
Rep Power: 3
![]() |
Dameon was right in his first post. When doing like you did, you're "giving" the pointer an address, and when you later tries to use the pointer, you of course are getting an error.
If we have a pointer int *ptr, then ptr is the address of the pointing address, &ptr is the pointers own address and *ptr is the value from the pointing address. So Dameon was correct, when saying that a cast, ptr = (int *)3 will set the address of which ptr is pointing to, and not as a value. This is an example that shows it: #include <stdio.h>
int main()
{
int *x = (int *)3;
int *y = (int *)4;
printf("x points to: %x\n", x);
printf("y points to: %x\n", y);
return 0;
}
__________________
-- v0id
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
Okay, that makes sense to me, but I don't understand why char pointers are able to work in that fashion. It seems to me that C isn't very consistant on this issue.
char *s = "Why can I do this?"; |
|
|
|
|
|
#10 | |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 203
Rep Power: 2
![]() |
Quote:
int i = 10 works for an interger variable, because "i" is a value. In the case of int *i = 10, it does not work, because "i" is not a value, it is a memory address. When you say int *i = 10, you are trying to say "make the address of 'i' 10." You can't do that. The compiler assigns memory addresses, not you. Last edited by Fall Back Son; Jun 28th, 2007 at 2:17 AM. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding a 0x Prefix to Hex Values? | vinit11 | Software Design and Algorithms | 21 | Apr 7th, 2007 1:38 PM |
| adding printing deleting values from a min heap | cwl157 | C++ | 15 | Mar 23rd, 2006 11:52 PM |
| Pointers to return multiple values | Clotters | C++ | 8 | Jul 20th, 2005 9:54 PM |
| Pointers in C (Part II) | Stack Overflow | C | 2 | Apr 29th, 2005 10:39 AM |
| Pointers in C (Part I) | Stack Overflow | C | 4 | Apr 28th, 2005 7:03 PM |