[ back to toc ]

pointers

Date: 2002/01/23 16:14

Q:
Hi Peter

could you clarify this doubt for me....
int *j;
i=10;
j=&i;
printf("%d", *j);

iget the value of i as 10;

now i will say
in a second example

int *j;
j=(int *)10;
printf("%d", j);
i get the value of j as 10 but if i say printf("%d", *j) i get the bus
error and it dumps core ...

when we use an pointer to an integer we print the value of the variable by
saying *(pointer_name) then why do we have to say just say to get the
value of j...

Ques #2:

# include<string.h>
main()
{
void dis(char *);
char *p="temp";

printf("%s\n", p);
dis(p);
printf("%s\n", p);

}

void dis (char *p)
{

p="cool";
printf("%s\n", p);
}

The output of the baove program is :
temp
cool
temp

why has it not printed cool in the mai function after the assignment of
value "cool" to it...

could you clarify thses doubts of mine...

thanx and regards
*NAME-DELETED*

A:
j=(int *)10;
printf("%d", j);
i get the value of j as 10 but if i say printf("%d", *j) i get the bus
error and it dumps core ...

Of course. You want to access the memory location 0x00000A which is not
belonging to your process.

In your second example you alter the local (argument) variable p to point
to a different string. The global variable p still points tot he original
string.

regards,
Peter

[ back to toc ]