Help in c pointers

J

Jordan Abel

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

Not quite - msg isn't a valid pointer at all, so the act itself of
passing it to printf invokes UB. It would equally invoke UB if it were a
void * and being passed to a printf %p format.

The value of msg is indeterminate since it is never initialized. Reading
an indeterminate value results in undefined behavior [since it may be a
trap representation]
 
H

Herbert Rosenau

Hi keith,

Can u recommend me soem of the advance C books.

I don't know who u is. Why does you ask us if u knows it? Ask
she/he/it
yourself! Maybe u will answer you, maybe not. I've never seen a
message from Mr./Ms./Mrs. u in usenet at all.

I have gone thru Expert
C programming. Some book on similiar line sbut with multi threading and
algorithms.

Mr. google is your friend on that question.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
K

Kenneth Brody

CBFalconer wrote:
[...]
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.

Can demons come out of a flying pig's nose?

--
+-------------------------+--------------------+-----------------------------+
| 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]>
 
P

patelchint

hum nice ?
yar i dont know why u got error
but i cant give eroor
*p=4
mins u asign a adress of value 4 to p there no address of 4
but in charector every char have a fix address.
so may be because of this u get error for int & not for charector.ok.
 
K

Keith Thompson

hum nice ?
yar i dont know why u got error
but i cant give eroor
*p=4
mins u asign a adress of value 4 to p there no address of 4
but in charector every char have a fix address.
so may be because of this u get error for int & not for charector.ok.

Please make some effort to write in standard English. Silly
abbreviations like "u" for "you" just make what you write more
difficult to read. We'll gladly make allowances for minor errors,
especially if English isn't your first language, but I can't even
figure out what you mean.

You also need to provide context when you post a followup. Read
<http://cfaj.freeshell.org/google/> to find out how to do this.

(And the original question has already been answered.)
 
P

Pedro Graca

Jordan said:
Not quite - msg isn't a valid pointer at all, so the act itself of
passing it to printf invokes UB. It would equally invoke UB if it were a
void * and being passed to a printf %p format.

I understand.

But another question gets raised in my head:
What is a valid pointer?


Examples:

char * p; /* p is, for now, an invalid pointer */

p = &p; /* and now??? */
p = (char*)&p; /* and now??? */
p = main;
p = (char*)main;
p = rand();
p = (char*)rand();
 
K

Keith Thompson

Pedro Graca said:
But another question gets raised in my head:
What is a valid pointer?


Examples:

char * p; /* p is, for now, an invalid pointer */

p = &p; /* and now??? */
p = (char*)&p; /* and now??? */

In the above two cases, p is a valid pointer; it points to the first
byte of p.
p = main;

Illegal. A conversion from a pointer-to-function type to a
pointer-to-object type requires a cast.
p = (char*)main;

Legal (I think), but undefined. The language does not define the
semantics of a conversion from a pointer-to-function type to a
pointer-to-object type.
p = rand();

Illegal. There is no implicit conversion from int to char*; a cast is
required.
p = (char*)rand();

This is legal, but the result of converting an int value to char* is
implementation-defined (except for the special case of a null pointer
constant). The value of p may or may not be valid.
 
C

Charles Richmond

Hi,
Thanx

But again why i am able to pass value to char* even if it is not
intialized.
Because what you have done is *wrong* and you get "undefined behavior".
Undefined behavior can range from nasal demons to actually working
occasionally. Undefined behavior means *anything* can happen.
 
P

Pedro Graca

[Jordan's comment on passing an invalid pointer to printf inserted]

Keith said:
Jordan Abel wrote [edited]:
the act itself of passing an invalid pointer to printf invokes UB.
What is a valid pointer? [snip examples]
p = (char*)rand();

This is legal, but the result of converting an int value to char* is
implementation-defined (except for the special case of a null pointer
constant). The value of p may or may not be valid.

So the following code does not invoke UB?

char * p;
p = (char*)rand();
printf("%c\n", p);

.... and removing the assignment does?

char * p;
printf("%c\n", p);
 
K

Keith Thompson

Pedro Graca said:
[Jordan's comment on passing an invalid pointer to printf inserted]

Keith said:
Jordan Abel wrote [edited]:
the act itself of passing an invalid pointer to printf invokes UB.
What is a valid pointer? [snip examples]
p = (char*)rand();

This is legal, but the result of converting an int value to char* is
implementation-defined (except for the special case of a null pointer
constant). The value of p may or may not be valid.

So the following code does not invoke UB?

char * p;
p = (char*)rand();
printf("%c\n", p);

You need "%p", not "%c". Actually, the "%p" format expects a void*;
you can almost certainly get away with passing a char* instead, but
IMHO it's better style to convert it:

printf("%p\n", (void*)p);

C99 6.3.2.3p5:

An integer may be converted to any pointer type. Except as
previously specified, the result is implementation-defined, might
not be correctly aligned, might not point to an entity of the
referenced type, and might be a trap representation.

The "previously specified" part refers to null pointer constants.
Alignment shouldn't be an issue for char*, but you could still get a
trap representation. If so, evaluating p, whether you then pass the
value to printf() or not, invokes undefined behavior.
... and removing the assignment does?

char * p;
printf("%c\n", p);

That certainly invokes UB, even with "%p".
 
M

manochavishal

hum nice ?
yar i dont know why u got error
but i cant give eroor
*p=4
mins u asign a adress of value 4 to p there no address of 4
but in charector every char have a fix address.
so may be because of this u get error for int & not for charector.ok.

Even i agree with Keith. Please use some proper English words as this
group is in public domain. Each one here is trying to figure out what
others write and provide a logical solution to it.

Hindi words like 'yar' should not be used here.

Hope you got the point.

Cheers
Vishal
 

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

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,498
Latest member
yelene6679

Latest Threads

Top