c question

G

girish.dinne

#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);
printf("%d", x[""]);
}

The above program wont give you any error or warning but the output is
not predictable.
If you guys understand how it happens, kindly let me know.

what value will be assigned for the following statement.

int i="hai";
 
S

shaanxxx

#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);
printf("%d", x[""]);
}
I write equivalent statements for above code
int main()
{
char arr1[] = "123456";
char arr2[] = "";
int x = 34;
printf("%d\n", arr1[x]);
printf("%d", arr2[x]);
}
this is memory over-run.

The above program wont give you any error or warning but the output is
not predictable.
If you guys understand how it happens, kindly let me know.

what value will be assigned for the following statement.

int i="hai";

"Hai" will return an address which will coverted to int and assigned to
i.
 
R

Richard Bos

#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);
printf("%d", x[""]);
}

The above program wont give you any error or warning

This is not necessarily true. The error is detectable at compile time,
so it's possible that some compilers do give a diagnostic when the
warning level is screwed up far enough.
but the output is not predictable.

This, too, is not necessarily true. For example, it could predictably
output "0\n0"; or it could predictably crash. It all depends on which
implementation you use, on which system.
If you guys understand how it happens, kindly let me know.

You invoke undefined behaviour. Anything may happen. Don't do that.
what value will be assigned for the following statement.

int i="hai";

Nothing; that's a constraint violation, and it _must_ give an error.

Richard
 
J

jaysome

#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);

Warning 409: Expecting a pointer or array
Warning 415: Likely access of out-of-bounds pointer
printf("%d", x[""]);

Warning 409: Expecting a pointer or array
Warning 415: Likely access of out-of-bounds pointer
The above program wont give you any error or warning but the output is
not predictable.

It gave me a warning or two.
what value will be assigned for the following statement.

int i="hai";

One compiler warns:

warning C4047: 'initializing' : 'int ' differs in levels of
indirection from 'char [4]'

And another "compiler" warns:

Error 64: Type mismatch (initialization) (int = pointer)
 
R

Richard Bos

jaysome said:
#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);

Warning 409: Expecting a pointer or array

That warning is wrong (it would be apropos if there were _two_ pointers
or two scalars, but as written this code is correct)...
Warning 415: Likely access of out-of-bounds pointer

....but this one is right.

Richard
 
R

Richard Bos

shaanxxx said:
"Hai" will return an address which will coverted to int and assigned to
i.

Please quote C&V in the Standard where this is guaranteed.

Richard
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Richard said:
Please quote C&V in the Standard where this is guaranteed.

perhaps 6.3.2.3:6 in C99. The result is implementation defined
or undefined behaviour though.
 
S

shaanxxx

Richard said:
Please quote C&V in the Standard where this is guaranteed.

Richard

C & V ? , you meant "context and verse" ?
I am little new to sf (short forms)
 
R

Richard Bos

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?= said:
perhaps 6.3.2.3:6 in C99.

That only allows pointers to be converted to integers; it does not say
that the conversion occurs automatically, without a cast.
The result is implementation defined or undefined behaviour though.

Quite; so the result may not ever be assigned to i. UB means that it is
also allowed to simply crash.

Richard
 
C

Clever Monkey

shaanxxx said:
C & V ? , you meant "context and verse" ?
I am little new to sf (short forms)
"Chapter and Verse."

The connotation is that The Standard or one of Its Supporting Books is
nearly Biblical.

But that would be an ecumenical matter.
 
D

Default User

Richard said:
jaysome said:
#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);

Warning 409: Expecting a pointer or array

That warning is wrong (it would be apropos if there were two pointers
or two scalars, but as written this code is correct)...

I wouldn't say it's wrong. The compiler was expecting a pointer or
array with the [] operator. That doesn't mean that it was wrong that
one didn't appear, just that the compiler thinks it looks fishy.

Compilers often warn about things that aren't wrong.

Example:

int num;
if (num = 3)
{
}

Is perfectly correct, but many compilers will issue a warning anyway.




Brian
 
F

Flash Gordon

Richard said:
(e-mail address removed) wrote:


Nothing; that's a constraint violation, and it _must_ give an error.

No, it must give a *diagnostic*. There are compilers which will only
generate a warning not an error for this and that is perfectly
acceptable as far as the standard goes.
 
V

venkatesh

shaanxxx said:
#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);
printf("%d", x[""]);
}
I write equivalent statements for above code
int main()
{
char arr1[] = "123456";
char arr2[] = "";
int x = 34;
printf("%d\n", arr1[x]);
printf("%d", arr2[x]);
}
this is memory over-run.

The above program wont give you any error or warning but the output is
not predictable.
If you guys understand how it happens, kindly let me know.

what value will be assigned for the following statement.

int i="hai";

"Hai" will return an address which will coverted to int and assigned to
i.
====

do you mean that if the value of x is say 3, then the 1st printf will print arr1[3] ie., 4.
 
S

shaanxxx

venkatesh said:
shaanxxx said:
#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);
printf("%d", x[""]);
}
I write equivalent statements for above code
int main()
{
char arr1[] = "123456";
char arr2[] = "";
int x = 34;
printf("%d\n", arr1[x]);
printf("%d", arr2[x]);
}
this is memory over-run.

The above program wont give you any error or warning but the output is
not predictable.
If you guys understand how it happens, kindly let me know.

what value will be assigned for the following statement.

int i="hai";

"Hai" will return an address which will coverted to int and assigned to
i.
====

do you mean that if the value of x is say 3, then the 1st printf will print arr1[3] ie., 4.

no , It will print 52 (THE ASCII VALUE:http://www.lookuptables.com/).

when we say
char c = 'a' ;
variable c will have one byte. And it will store ASCII character
corresponding to 'a';

If you want to print '4' there , replace %d by %c in first printf.
 
R

Richard Bos

Default User said:
Richard said:
jaysome said:
On 4 Sep 2006 22:35:15 -0700, (e-mail address removed) wrote:

#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);

Warning 409: Expecting a pointer or array

That warning is wrong (it would be apropos if there were two pointers
or two scalars, but as written this code is correct)...

I wouldn't say it's wrong. The compiler was expecting a pointer or
array with the [] operator. That doesn't mean that it was wrong that
one didn't appear, just that the compiler thinks it looks fishy.

Hmyes. I'd say that if it had said "Array and index in unusual order",
it would've been right; but as written, it seems to indicate that there
is _no_ pointer or array, and that's just not the problem.

Richard
 
D

Default User

Richard said:
Default User said:
Richard said:
On 4 Sep 2006 22:35:15 -0700, (e-mail address removed) wrote:

#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);

Warning 409: Expecting a pointer or array

That warning is wrong (it would be apropos if there were two
pointers or two scalars, but as written this code is correct)...

I wouldn't say it's wrong. The compiler was expecting a pointer or
array with the [] operator. That doesn't mean that it was wrong that
one didn't appear, just that the compiler thinks it looks fishy.

Hmyes. I'd say that if it had said "Array and index in unusual order",
it would've been right; but as written, it seems to indicate that
there is no pointer or array, and that's just not the problem.

What? Diagnostics that are somewhat baffling? Perish the thought.

Yours would naturally be better, but that doesn't mean the original was
"wrong", just non-optimally worded. It failed to say where it expected
a pointer or array. A better one might have been:

So you really think you're clever when you use that obfuscated crap,
huh? Well you're not, so cut it out.



Brian
 
R

Richard Bos

Default User said:
Richard said:
Default User said:
Richard Bos wrote:


On 4 Sep 2006 22:35:15 -0700, (e-mail address removed) wrote:

#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);

Warning 409: Expecting a pointer or array

That warning is wrong (it would be apropos if there were two
pointers or two scalars, but as written this code is correct)...

I wouldn't say it's wrong. The compiler was expecting a pointer or
array with the [] operator. That doesn't mean that it was wrong that
one didn't appear, just that the compiler thinks it looks fishy.

Hmyes. I'd say that if it had said "Array and index in unusual order",
it would've been right; but as written, it seems to indicate that
there is no pointer or array, and that's just not the problem.

What? Diagnostics that are somewhat baffling? Perish the thought.

Oh, I have no problems with diagnostics that confuse the already
confused confuser-programmer, but when they give the impression that the
compiler writer himself was confused, I start getting second thoughts.

Richard
 
C

CBFalconer

#include<stdio.h>

int main()
{
int x = 34;
printf("%d\n", x["123456"]);
printf("%d", x[""]);
}

The above program wont give you any error or warning but the
output is not predictable.
If you guys understand how it happens, kindly let me know.

Of course. Buffer overflow causes undefined behaviour. If you
make it a legal program, as in:

#include <stdio.h>

int main(void) {
int x = 4;

printf("%d\n", x["123456"]);
return 0;
}

there will be no difficulty, and it will print 53 (for ascii char
coding). "123456" is an array of seven members, so the maximum
legal x is 6.
 

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,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top