Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Code problem

Reply
Thread Tools

Code problem

 
 
rage
Guest
Posts: n/a
 
      06-08-2012
Can someone please help me out on this?

#include<stdio.h>
main()
{
int i;
int *j=10;
i=j+19;
printf("%d %d\n",j,i);
}

The output is a garbage value (86) on my pc and ideone. Should it not be 29?
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-08-2012
On 6/8/2012 10:52 AM, rage wrote:
> Can someone please help me out on this?
>
> #include<stdio.h>
> main()
> {
> int i;
> int *j=10;


What are you trying to accomplish with this line? Your compiler
should have given you a diagnostic message about it; what did the
message say, and what meaning did you think it had?

> i=j+19;


Should have received another diagnostic message for this line.

> printf("%d %d\n",j,i);


No diagnostic is *required* for this line, but some compilers
are smart and helpful enough to issue one.

> }
>
> The output is a garbage value (86) on my pc and ideone. Should it not be 29?


Your code's behavior is undefined, so there is no reason you
"should" expect any particular output, nor even any output at all.
What are you trying to do?

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      06-08-2012
rage <(E-Mail Removed)> writes:
>The output is a garbage value (86) on my pc and ideone. Should it not be 29?


Why did you write the asterisk?

 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      06-08-2012
On 2012-06-08, rage <(E-Mail Removed)> wrote:
> Can someone please help me out on this?


Your code is not a well-formed ISO C program. It contains type system
violations that require a diagnostic.

> #include<stdio.h>
> main()
> {
> int i;
> int *j=10;


Here, a variable of pointer type is being initialized with an integer value.

> i=j+19;


Here, an integer object is being assigned from a pointer expression.

> printf("%d %d\n",j,i);


Here, the %d conversion specifier, which requires a matching argument of type
int, is given an argument of type "int *". This is undefined behavior.

> }
>
> The output is a garbage value (86) on my pc and ideone. Should it not be 29?


Since the program requires diagnostics, the ISO C standard doesn't require the
implementation to translate and execute the program. If the program translates
and executes anyway, its behavior is not defined by ISO C. Of course, it has a
behavior, but implementation-specific reasoning is required to understand it.

For instance, whether or not you can print a pointer using %d will depend
on the calling conventions, and that can be sensitive to different versions
of even the same compiler. What if pointers are 64 bits wide but int is 32
bits? In that case it is conceivable that "%d %d" will just take two halves
of the 64 bit j value, and i is ignored.

This kind of program is usually not correct even according to the local
compiler and library documentation, never mind ISO C.
 
Reply With Quote
 
Angel
Guest
Posts: n/a
 
      06-08-2012
On 2012-06-08, rage <(E-Mail Removed)> wrote:
> Can someone please help me out on this?
>
> #include<stdio.h>
> main()
> {
> int i;
> int *j=10;


In this line, you're converting an integer value to a pointer. That
is an operation with undefined behavior. (Except if the integer is a
constant value of zero, if I recall right.)

Because of this undefined behavior, your program may not compile at
all. Even if it does, it may not run. Even if it runs, it may not give
any sensible output. Undefined behavior basically means "sorry mate,
you're on your own here".

Do you actually know what pointers are? If not, I'd recommend not using
the unary & and * operators until you do.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-08-2012
On 06/08/2012 10:52 AM, rage wrote:
> Can someone please help me out on this?
>
> #include<stdio.h>
> main()
> {
> int i;
> int *j=10;


You're trying to initializing j, which is a pointer to an int, with the
value of 10, which is an integer. That's a constraint violation
(6.5.16.1p1). A conforming implementation is required to issue a
diagnostic message - did yours? If so, what did it say?

After issuing the diagnostic, your compiler is free to do anything it
wants with your code. I suspect that what it is doing is converting 10
to a pointer value, and storing that pointer value in j.

You could force it to do that conversion explicitly by inserting an
(int*) before the 10, in which case the constraint violation would go
away. However, the resulting pointer value is implementation-defined. It
might contain a trap representation, in which case the behavior of your
program would still be undefined. Even if it is not a trap
representation, you have no idea where in memory it points at. It
probably does not point at a piece of memory reserved for use by your
program; if it does, there's a small chance it points at the same piece
of memory reserved to store the value of 'i'.

> i=j+19;


Since you don't know what location in memory j points at, you can't be
sure whether adding 19 to that pointer will have defined behavior; it
probably doesn't.

You're trying to store the result of that addition, which is a pointer
value, into an int object. This is once again a constraint violation,
with the same consequences as the first time.

> printf("%d %d\n",j,i);


You're using a %d format specifier, which should be used only for int
values, to print the value of j, which is a pointer. That's yet another
example of undefined behavior.

> }
>
> The output is a garbage value (86) on my pc and ideone. Should it not be 29?


The output could have been anything, including the text of the "Star
Spangled Banner" (or nothing at all). That's what "undefined behavior"
means.

Here's correct code that does something similar to what your original
code may have been meant to do:

int i;
int k;
int *j=&k;
*j = 10;
i = *j+19;
printf("%d %d\n", *j, i);
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      06-08-2012


"rage" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Can someone please help me out on this?
>
> #include<stdio.h>
> main()
> {
> int i;
> int *j=10;
> i=j+19;
> printf("%d %d\n",j,i);
> }
>
> The output is a garbage value (86) on my pc and ideone. Should it not be
> 29?


Assuming you did intend j to be a pointer, try it like this:

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int i;
int *j=malloc(sizeof(int));
*j=10;
i=*j+19;
printf("%d %d\n",*j,i);
}

(The code assumes the malloc() call is successful.)

--
Bartc
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      06-08-2012
In <(E-Mail Removed)> rage <(E-Mail Removed)> writes:

> Can someone please help me out on this?


> #include<stdio.h>
> main()
> {
> int i;
> int *j=10;
> i=j+19;
> printf("%d %d\n",j,i);
> }


> The output is a garbage value (86) on my pc and ideone. Should it not be 29?


You declared j as an integer pointer, and initialized it to point at memory
location 10, which very likely contains an unknown value.

As others have said, your compiler should have issued a warning about
this, which would have given you a clue as to what the problem was.

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Les Cargill
Guest
Posts: n/a
 
      06-08-2012
rage wrote:
> Can someone please help me out on this?
>
> #include<stdio.h>
> main()
> {
> int i;
> int *j=10;
> i=j+19;
> printf("%d %d\n",j,i);
> }
>
> The output is a garbage value (86) on my pc and ideone. Should it not be 29?
>



Your code is broken:

int i;
int k = 10;
int *j = &k;
i=(*j)+19;
printf("%d %d\n",*j,i);

might work as you expect. "int *j;" declares j as "a pointer
to an integer". "j = 10;" make it point to address 10.

you probably don't want that.

From that point on, you're not in Kansas any more.. you're exposing...
<puts on sunglasses>
UNDEFINED BEHAVIOR!

--
Les Cargill



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-08-2012
Angel <(E-Mail Removed)> writes:
> On 2012-06-08, rage <(E-Mail Removed)> wrote:
>> Can someone please help me out on this?
>>
>> #include<stdio.h>
>> main()
>> {
>> int i;
>> int *j=10;

>
> In this line, you're converting an integer value to a pointer. That
> is an operation with undefined behavior. (Except if the integer is a
> constant value of zero, if I recall right.)


What makes you think there's a conversion there?

The declaration violates a constraint. An initialization has
the same constraints as a simple assignment (N1570 6.7.9p11).
The rules for simple assignment say that if the left hand side has
pointer type, the right hand side must have pointer type (either
a compatible type or void*) or it can be a null pointer constant.

Since

int *j = 10;

doesn't satisfy these constraints, there is no definition of its
behavior. In particular, the standard does not say or imply that the
int value 10 should be converted to int*.

That was the typical behavior for pre-standard versions of C, and
most compilers that don't reject the declaration will generate code
equivalent to:

int *j = (int*)10;

But it's not required.

Note that the behavior of an integer-to-pointer conversion is not
(entirely) undefined. N1570 6.3.2.3 says:

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.

(But that's not relevant to this code because, as I said, no conversion
is implied.)

[...]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
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 with code - and code wariors thedarkman HTML 5 09-13-2010 11:40 PM
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Problem getting cookie from code-behind (worked in code-beside) Alan Silver ASP .Net 1 09-15-2005 05:23 PM
Problem with ejbdoclet--generated bean code is inheriting from "phantom" code... mwkohout@gmail.com Java 0 02-14-2005 06:06 PM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM



Advertisments