Velocity Reviews > can you tell that y the code is work like that

# can you tell that y the code is work like that

venkatesh
Guest
Posts: n/a

 10-21-2005
int *p=20;
printf("%d",*p);
printf("%d",p);

the above code prints the output as
somegarbage value //for *p(may be address
20 //for p
why can you explain
thanks for that

Christopher Benson-Manica
Guest
Posts: n/a

 10-21-2005
venkatesh <(E-Mail Removed)> wrote:

> int *p=20;

Broken. What makes you suspect that 20 is an appropriate value for a
pointer to an integer?

> printf("%d",*p);

Broken. What exactly do you think a pointer with the value 20 might
point to?

> printf("%d",p);

Broken. The size of a pointer to an integer need not be equal to
sizeof(int).

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

> why can you explain

Pure random chance, as far as the C standard is concerned.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Pierre Maurette
Guest
Posts: n/a

 10-21-2005
venkatesh, le 21/10/2005, a écrit :
> int *p=20;
> printf("%d",*p);
> printf("%d",p);
>
>
>
> the above code prints the output as
> somegarbage value //for *p(may be address
> 20 //for p
> why can you explain
> thanks for that

int dummy = 20;
int* p = &dummy;
printf("%d\n",*p);
printf("%p\n",p);

or

int* p = malloc(sizeof int);
*p = 20;
printf("%d\n",*p);
printf("%p\n",p);

--
Pierre Maurette

Emmanuel Delahaye
Guest
Posts: n/a

 10-21-2005
venkatesh a écrit :
> int *p=20;

This is kinda nonsense. What to you think it meant ?

The portable way of initializing a pointer to an object are

- NULL
- The address of an object of the same type
- the value returned by malloc()
- the value returned by fopen() if the type is void* or FILE*
and similar cases.

But initialising a pointer with a plain integer is undefined by the
language. Actually, it may work on a specific platform. It's called an
implementation-dependent behaviour.

--
C is a sharp tool

vire
Guest
Posts: n/a

 10-21-2005
yes,
exactly what you wrote means:
int* p;
p = 20;

Default User
Guest
Posts: n/a

 10-21-2005
vire wrote:

> yes,
> exactly what you wrote means:
> int* p;
> p = 20;

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded

Keith Thompson
Guest
Posts: n/a

 10-21-2005
Christopher Benson-Manica <(E-Mail Removed)> writes:
> venkatesh <(E-Mail Removed)> wrote:
>> int *p=20;

>
> Broken. What makes you suspect that 20 is an appropriate value for a
> pointer to an integer?

What's surprising is that his compiler thought it was appropriate.

A compiler might allow assigning an integer value to a pointer as an
extension, but if the code is compiled in strict mode (might be called
"ansi" or "iso", depending on the compiler), the compiler is required
to issue a diagnostic.

[...]

>> printf("%d",p);

>
> Broken. The size of a pointer to an integer need not be equal to
> sizeof(int).

Size matters not. Using "%d" to print a pointer value invokes
undefined behavior (though it will often do what you expect anyway).
It can fail even if int and int* happen to have the same size.

The correct way to print an int* value is:

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

Also, the original code, if it doesn't blow up, will probably print
the values adjacent to each other; you won't be able to tell where one
ends and the other begins. Rather than

int *p=20;
printf("%d",*p);
printf("%d",p);

you might try this:

int *p = (int*)20;
printf("*p = %d\n", *p);
printf("p = %p\n", (int*)p);

Of course this could still invoke undefined behavior, since (int*)20
is unlikely to be a valid address.

Here's a program that actually works:

#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int *p = malloc(sizeof *p);
if (p == NULL) {
printf("malloc failed\n");
}
else {
*p = 20;
printf("*p = %d\n", *p);
printf("p = %p\n", (void*)p);
}
return 0;
}

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Christopher Benson-Manica
Guest
Posts: n/a

 10-21-2005
Keith Thompson <(E-Mail Removed)> wrote:

> The correct way to print an int* value is:

> printf("%p", (void*)p);

(which I did mention, FWIW)

> Of course this could still invoke undefined behavior, since (int*)20
> is unlikely to be a valid address.

Is it not UB regardless, since 20 is not a value obtained from
malloc()?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Tim Rentsch
Guest
Posts: n/a

 10-21-2005
Emmanuel Delahaye <(E-Mail Removed)> writes:

> venkatesh a écrit :
> > int *p=20;

>
> This is kinda nonsense. What to you think it meant ?
>
> The portable way of initializing a pointer to an object are
>
> - NULL
> - The address of an object of the same type
> - the value returned by malloc()
> - the value returned by fopen() if the type is void* or FILE*
> and similar cases.

And 0 (or any 0-valued constant expression).

> But initialising a pointer with a plain integer is undefined by the
> language.

Unless the integer is 0.

Keith Thompson
Guest
Posts: n/a

 10-21-2005
Christopher Benson-Manica <(E-Mail Removed)> writes:
> Keith Thompson <(E-Mail Removed)> wrote:
>> The correct way to print an int* value is:

>
>> printf("%p", (void*)p);

>
> (which I did mention, FWIW)

Ok.

>> Of course this could still invoke undefined behavior, since (int*)20
>> is unlikely to be a valid address.

>
> Is it not UB regardless, since 20 is not a value obtained from
> malloc()?

Valid pointers don't have to be obtained from malloc().

The conversion itself yields an implementation-defined result:

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.

with a footnote:

The mapping functions for converting a pointer to an integer or an
integer to a pointer are intended to be consistent with the
addressing structure of the execution environment.

If the result happens to be an invalid pointer, dereferencing it
invokes undefined behavior. If the implementation happens to
guarantee that 20 is a valid address for an int, the behavior of
dereferencing (int*)20 is merely implementation-defined.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.