View Single Post
Old Jun 27th, 2007, 8:40 PM   #1
Darkhack
Hobbyist Programmer
 
Darkhack's Avatar
 
Join Date: Dec 2005
Location: Kansas City
Posts: 102
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