473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

c question

#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";

Sep 5 '06 #1
18 2204

gi**********@gmail.com wrote:
#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.

Sep 5 '06 #2
On 4 Sep 2006 22:35:15 -0700, gi**********@gmail.com wrote:
>#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)

--
jay
Sep 5 '06 #3
gi**********@gmail.com wrote:
#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
Sep 5 '06 #4
jaysome wrote:
On 4 Sep 2006 22:35:15 -0700, gi**********@gmail.com wrote:
#include<stdio.h>

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

<SNIP>
Warning 415: Likely access of out-of-bounds pointer
That's a clever compiler. Which one is it ?

Sep 5 '06 #5
jaysome <ja*****@spamcop.netwrote:
On 4 Sep 2006 22:35:15 -0700, gi**********@gmail.com 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)...
Warning 415: Likely access of out-of-bounds pointer
....but this one is right.

Richard
Sep 5 '06 #6
"shaanxxx" <sh******@yahoo.comwrote:
gi**********@gmail.com wrote:
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.
Please quote C&V in the Standard where this is guaranteed.

Richard
Sep 5 '06 #7
Richard Bos wrote:
"shaanxxx" <sh******@yahoo.comwrote:
>gi**********@gmail.com wrote:
>>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.

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.
Sep 5 '06 #8

Richard Bos wrote:
"shaanxxx" <sh******@yahoo.comwrote:
gi**********@gmail.com wrote:
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.

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)

Sep 5 '06 #9
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?= <NO*@Utel.nowrote:
Richard Bos wrote:
"shaanxxx" <sh******@yahoo.comwrote:
gi**********@gmail.com wrote:
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.
Please quote C&V in the Standard where this is guaranteed.

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
Sep 5 '06 #10
shaanxxx wrote:
Richard Bos wrote:
>"shaanxxx" <sh******@yahoo.comwrote:
>>gi**********@gmail.com wrote:
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.
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)
"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.
Sep 5 '06 #11
Richard Bos wrote:
jaysome <ja*****@spamcop.netwrote:
On 4 Sep 2006 22:35:15 -0700, gi**********@gmail.com 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.

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
Sep 5 '06 #12
Richard Bos wrote:
gi**********@gmail.com wrote:
<snip>
>int i="hai";

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.
--
Flash Gordon
Sep 5 '06 #13

shaanxxx wrote:
gi**********@gmail.com wrote:
#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.
Sep 5 '06 #14

venkatesh wrote:
shaanxxx wrote:
gi**********@gmail.com wrote:
#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.

Sep 6 '06 #15
"Default User" <de***********@yahoo.comwrote:
Richard Bos wrote:
jaysome <ja*****@spamcop.netwrote:
On 4 Sep 2006 22:35:15 -0700, gi**********@gmail.com 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
Sep 6 '06 #16
Richard Bos wrote:
"Default User" <de***********@yahoo.comwrote:
Richard Bos wrote:
jaysome <ja*****@spamcop.netwrote:
>
On 4 Sep 2006 22:35:15 -0700, gi**********@gmail.com 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
Sep 6 '06 #17
"Default User" <de***********@yahoo.comwrote:
Richard Bos wrote:
"Default User" <de***********@yahoo.comwrote:
Richard Bos wrote:
>
jaysome <ja*****@spamcop.netwrote:

On 4 Sep 2006 22:35:15 -0700, gi**********@gmail.com 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
Sep 8 '06 #18
gi**********@gmail.com wrote:
>
#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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Sep 8 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.