![]() |
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:
ILLEGAL - SEGMANTATION FAULT |
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.
|
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() |
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. |
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. |
"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); |
Quote:
:
char *s = "Hello World!"; // legal |
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> |
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?"; |
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. |
| All times are GMT -5. The time now is 10:12 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC