Re: Help in c pointers
- From: Jordan Abel <random832@xxxxxxxxx>
- Date: 28 Feb 2006 17:03:28 GMT
On 2006-02-28, Kenneth Brody <kenbrody@xxxxxxxxxxx> wrote:
"manochavishal@xxxxxxxxx" wrote:
HI
int *p;
*p = 4;
char * msg;
*msg = 'c';
printf("msg is %s",msg);
when i try to pass a value 4 to place what integer pointer p points to
it says seg fault
but for the char pointer it works fine.
Why this behaviour
Neither pointer has been initialized, and both assignments invoke UB. In
your particular case, the address in p happened to be invalid for an int,
and the address in msg happened to be valid for a char, and didn't cause
any noticable effect by overwriting that memory.
Incidentally, there's a third instance of undefined behavior here -
anyone know what it is?
.
- Follow-Ups:
- Re: Help in c pointers
- From: Pedro Graca
- Re: Help in c pointers
- References:
- Help in c pointers
- From: manochavishal@xxxxxxxxx
- Re: Help in c pointers
- From: Kenneth Brody
- Help in c pointers
- Prev by Date: Re: need help getting the results of the system function into a variable
- Next by Date: Re: need help getting the results of the system function into a variable
- Previous by thread: Re: Help in c pointers
- Next by thread: Re: Help in c pointers
- Index(es):
Relevant Pages
|