Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Adding The Values of Pointers (http://www.programmingforums.org/showthread.php?t=13435)

Darkhack Jun 27th, 2007 8:40 PM

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;
}


Dameon Jun 27th, 2007 9:17 PM

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.

Darkhack Jun 27th, 2007 10:13 PM

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;
}


Dameon Jun 27th, 2007 10:24 PM

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.

Fall Back Son Jun 27th, 2007 11:01 PM

Quote:

Originally Posted by Darkhack (Post 129747)
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.

What is your reasoning behind typecasting an integer value to a pointer anyway? This seems like a pointless operation. You're treating the integer 3 as a "pointer to an integer". In any case, assigning integer expressions to pointer variables is an illegal operation. Had you tried to say

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.

Fall Back Son Jun 27th, 2007 11:20 PM

"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);

Darkhack Jun 28th, 2007 12:05 AM

Quote:

Originally Posted by Fall Back Son (Post 129749)
In any case, assigning integer expressions to pointer variables is an illegal operation.

Ahh okay. My whole reason for asking this was trying to understand pointers. I'm not using it for a real application. Why is it okay to do the same thing with a character pointer but not an integer? I understand that in this example the string is an array of characters and that "s" by itself is a pointer to where the string starts, but why wouldn't "i" be the memory address of where 10 was created?

:

char *s = "Hello World!";  // legal
int *i = 10; // illegal, but why?


v0id Jun 28th, 2007 12:48 AM

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;
}

Of course we don't know what's on the addresses 0x03 and 0x04, so it could be anything - and we can't prevent an error.

Darkhack Jun 28th, 2007 1:27 AM

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?";

Fall Back Son Jun 28th, 2007 2:01 AM

Quote:

Originally Posted by Darkhack (Post 129751)
Ahh okay. My whole reason for asking this was trying to understand pointers. I'm not using it for a real application. Why is it okay to do the same thing with a character pointer but not an integer? I understand that in this example the string is an array of characters and that "s" by itself is a pointer to where the string starts, but why wouldn't "i" be the memory address of where 10 was created?

:

char *s = "Hello World!";  // legal
int *i = 10; // illegal, but why?


Neither one of those is an acceptable use of pointers. The compiler might accept it, but C doesn't always protect against everything. It seems like the compiler is converting "Hello World" to some sort of memory address, and then assigning that address to "s". You'll have to wait for one of the other, smarter guys to confirm that though. int *i = 10 is NOT doing what you may think it is doing. Integer variables and pointer variables are different...

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