Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 27th, 2007, 8:40 PM   #1
Darkhack
Hobbyist Programmer
 
Darkhack's Avatar
 
Join Date: Dec 2005
Location: Kansas City
Posts: 105
Rep Power: 3 Darkhack is on a distinguished road
Send a message via AIM to Darkhack
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;
}
Darkhack is offline   Reply With Quote
Old Jun 27th, 2007, 9:17 PM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Jun 27th, 2007, 10:13 PM   #3
Darkhack
Hobbyist Programmer
 
Darkhack's Avatar
 
Join Date: Dec 2005
Location: Kansas City
Posts: 105
Rep Power: 3 Darkhack is on a distinguished road
Send a message via AIM to Darkhack
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;
}
Darkhack is offline   Reply With Quote
Old Jun 27th, 2007, 10:24 PM   #4
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Jun 27th, 2007, 11:01 PM   #5
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 263
Rep Power: 2 Fall Back Son is on a distinguished road
Quote:
Originally Posted by Darkhack View Post
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 is offline   Reply With Quote
Old Jun 27th, 2007, 11:20 PM   #6
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 263
Rep Power: 2 Fall Back Son is on a distinguished road
"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);
Fall Back Son is offline   Reply With Quote
Old Jun 28th, 2007, 12:05 AM   #7
Darkhack
Hobbyist Programmer
 
Darkhack's Avatar
 
Join Date: Dec 2005
Location: Kansas City
Posts: 105
Rep Power: 3 Darkhack is on a distinguished road
Send a message via AIM to Darkhack
Quote:
Originally Posted by Fall Back Son View Post
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?
Darkhack is offline   Reply With Quote
Old Jun 28th, 2007, 12:48 AM   #8
v0id
Hobbyist Programmer
 
Join Date: Apr 2006
Posts: 155
Rep Power: 3 v0id is on a distinguished road
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.
__________________
-- v0id
v0id is offline   Reply With Quote
Old Jun 28th, 2007, 1:27 AM   #9
Darkhack
Hobbyist Programmer
 
Darkhack's Avatar
 
Join Date: Dec 2005
Location: Kansas City
Posts: 105
Rep Power: 3 Darkhack is on a distinguished road
Send a message via AIM to Darkhack
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?";
Darkhack is offline   Reply With Quote
Old Jun 28th, 2007, 2:01 AM   #10
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 263
Rep Power: 2 Fall Back Son is on a distinguished road
Quote:
Originally Posted by Darkhack View Post
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.

Last edited by Fall Back Son; Jun 28th, 2007 at 2:17 AM.
Fall Back Son is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC