Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-21-2005
venkatesh <> 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.
 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
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
 
Reply With Quote
 
vire
Guest
Posts: n/a
 
      10-21-2005
yes,
exactly what you wrote means:
int* p;
p = 20;

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-21-2005
vire wrote:

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


Please read my .sig.


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
header.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-21-2005
Christopher Benson-Manica <> writes:
> venkatesh <> 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) kst- <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.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-21-2005
Keith Thompson <kst-> 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.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      10-21-2005
Emmanuel Delahaye <> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-21-2005
Christopher Benson-Manica <> writes:
> Keith Thompson <kst-> 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) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
don't like, so don't waste your time thinking and buy them. You know,if you don't like one thing, it means nothing for you.</p> fashion t shirts seller Cisco 0 06-13-2011 02:01 AM
RE;Kontki if you delete kontiki any program you loaded with it in it 'will not work I have tried it with three programs and none work anymore (if you se it just stop download) 1-Twitch Computer Support 5 04-23-2009 02:45 PM
if you have free time ,you can chose the pro of you like sara_love67 Computer Support 7 09-25-2007 08:29 PM
CAN any one tell it whats the code tell it yogesh C++ 1 03-14-2007 01:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57