Help in c pointers

M

manochavishal

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

Help
 
Z

Zero

int *p;
*p = 4;

You have done the greates programming fault when using pointers.
Never use a pointer when it points somewhere!

Better try this:

int * p;

p = (int *) malloc (sizeof(int));

*p = 4;
 
M

manochavishal

hi,

Thanx for the quick reply.

In here i am concerned why while using char * i am able to pass value
to it. Even this points to somewhere right.
So while using int * which points somwhere i am not able to pass value
to it but while using the char * i am able to do it . Why???
Thanx in advance
 
A

A. Sinan Unur

You have done the greates programming fault when using pointers.
Never use a pointer when it points somewhere!

ITYM: Never use a pointer if it does *not* point to anywhere (that is,
never use an uninitialized pointer.
Better try this:

int * p;

p = (int *) malloc (sizeof(int));

*p = 4;

Better not. There is no need to cast the return value of malloc, and
doing so can hide errors. Always, yes *always*, check that malloc
succeeded:

int *p = malloc(sizeof *p);
if ( p ) {
*p = 4;
} else {
/* handle error */
}

Sinan
 
M

manochavishal

Hi,
Thanx

But again why i am able to pass value to char* even if it is not
intialized.

Cheers
 
K

Keith Thompson

int *p;
*p = 4;

p is uninitialized. Attempting to dereference it invokes undefined
behavior.
char * msg;
*msg = 'c';

msg is uninitialized. Attempting to dereference it invokes undefined
behavior.
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.

Both are possible consequences of undefined behavior.
 
P

pranav.choudhary

Hi,
Thanx

But again why i am able to pass value to char* even if it is not
intialized.

1. I think it is just a coincidence that you are not getting the error
for char *.
2. Maybe your program terminates on line 2 and u never know if there
was a SEG_FAULT in line 4
3. AFAIK chances for seg_fault is more for integers as their size is
larger.
 
M

manochavishal

Hi keith,

Can u recommend me soem of the advance C books. I have gone thru Expert
C programming. Some book on similiar line sbut with multi threading and
algorithms.

Thanx in advance

Cheers
Vishal
 
P

Pedro Graca

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

When you start your program imagine your memory looks like

0x65 0x39 0x82 0x87 0x46 0x53 0x72 0x99 0x27 0x34 ... <== garbage

You then say `p' is a pointer to int

0x65 0x39 0x82 0x87 0x46 0x53 0x72 0x99 0x27 0x34 ... <== garbage
`-------------------' <== p, pointing to garbage

and `msg' is a pointer to char

0x65 0x39 0x82 0x87 0x46 0x53 0x72 0x99 0x27 0x34 ... <== garbage
`-------------------' <== msg, pointing to garbage


Apparently the garbage `p' points to is not writable whereas the garbage
`msg' points to is.
 
K

Kenneth Brody

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.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
C

CBFalconer

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

You were unlucky with the char pointer.

Neither p nor msg points anywhere. Trying to dereference either of
them results in undefined behavior. Undefined behaviour includes
"working". It also includes launching flying pigs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
J

Jordan Abel

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?
 
P

Pedro Graca

Jordan said:
Incidentally, there's a third instance of undefined behavior here -
anyone know what it is?

a) `msg' isn't (*) a c-string (a sequence of chars ending in NUL) so
passing it to printf invokes UB.

b) printf doesn't end with a newline nor is followed by a
fflush(stdout); invoking implementation defined behaviour (???)


(*) probably it is -- there probably is a nul between msg and msg+<some
large integer value>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,497
Latest member
PilarLumpk

Latest Threads

Top