473,385 Members | 1,907 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,385 software developers and data experts.

Output of the printf(a,*a,**a) where a is of type int [][][]

#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}

I tried the above code and got the same value for a, *a , **a and &a.
Can anyone please tell me the reason behind such a behavior?
Feb 4 '08 #1
17 1773
In article <73**********************************@l32g2000hse. googlegroups.com>,
Nikhil Bokare <nb*****@gmail.comwrote:
>#include<stdio.h>
>int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}
>I tried the above code and got the same value for a, *a , **a and &a.
Can anyone please tell me the reason behind such a behavior?
You should not be printing out pointers with a %d format. A
pointer might (or might not) occupy more storage than an int,
so if you use a %d format you might be printing out only part
of the address of the pointer. It is not uncommon for pointers
to be longer than int; it is more common for pointers to -happen-
to be the same size as long, but that is not guaranteed.
You can use a %p format to write out a pointer to void
(so be sure to cast the pointer to (void *) in the argument list,
as pointers to different types may have different sizes.)

Also, you have specified that your main returns int and yet
you have not returned any value from main(). That will work in C99
but has undefined behaviour in C90.
--
"I will speculate that [...] applications [...] could actually see a
performance boost for most users by going dual-core [...] because it
is running the adware and spyware that [...] are otherwise slowing
down the single CPU that user has today" -- Herb Sutter
Feb 4 '08 #2
Nikhil Bokare said:
#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}

I tried the above code and got the same value for a, *a , **a and &a.
They can't have the same value, because they don't have the same type. a
has type int[3][3][3], *a has type int[3][3], **a has type int[3], and &a
has type (int *)[3][3][3]. None of these is type int, so %d is
inappropriate as a format specifier (and indeed the behaviour if you do
this is undefined). Since printf lacks format specifiers for the
above-mentioned types, a conversion is required, and the most obvious way
to do this is to cast to void *, which of course discards the type
information, so when you correct your program to use casts-to-void* and %p
instead of %d, you shouldn't necessarily expect to see different values.
Can anyone please tell me the reason behind such a behavior?
Mu.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 4 '08 #3
Nikhil Bokare <nb*****@gmail.comwrites:
#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}

I tried the above code and got the same value for a, *a , **a and &a.
Can anyone please tell me the reason behind such a behavior?
You're lucky (or unlucky) that the program worked at all. The "%d"
format requires an argument of type int. If you give it anything
else, you invoke undefined behavior. You're probably getting
meaningful results on your implementation, but there are no guarantees.

If you want to print a pointer value, use "%p" and convert the
argument to void*.

You should also terminate your output with a newline ("\n") and return
a value from main(); "return 0;" is usually the right thing.

Getting to what you're really asking about, you should read section 6
of the comp.lang.c FAQ, <http://www.c-faq.com/>. If you're still
confused after that, feel free to come back with more specific
questions.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 4 '08 #4
Thanks for pointing out my mistakes.
This is my edited program:

#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%p %p %p %p",a,*a,**a,&a);
return 0;
}

Its still printing the same value for all of them. How does it happen??
Feb 4 '08 #5
Nikhil Bokare <nb*****@gmail.comwrites:
int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
You should use %p to print pointers. To be really correct, you
should also cast all of these values to void *.
}

I tried the above code and got the same value for a, *a , **a and &a.
Can anyone please tell me the reason behind such a behavior?
All of those expressions point to the same byte. It's just in
their types that they have some differences.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Feb 4 '08 #6
Nikhil Bokare said:
Thanks for pointing out my mistakes.
This is my edited program:

#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%p %p %p %p",a,*a,**a,&a);
That line should be:

printf("%p %p %p %p",(void *)a,(void *)*a,(void *)**a,(void *)&a);
return 0;
}

Its still printing the same value for all of them. How does it happen??
They have different types, as I explained before, so they have different
values, as I explained before. The casts to void * are required, as I
explained before. Because the casts to void * discard type information,
the differences between the values are also being discarded.

Here's a real world parallel question:

"I used a GPS to find out the location of an oxygen atom, a water molecule,
and a water spillage on a kitchen table, and the GPS gave the same
location for all three. Why?"

Obviously, these are three very different things. Equally obviously,
there's no particular reason why they shouldn't be in precisely the same
place.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 4 '08 #7
On Feb 4, 1:46 pm, Nikhil Bokare <nbok...@gmail.comwrote:
Thanks for pointing out my mistakes.
This is my edited program:

#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%p %p %p %p",a,*a,**a,&a);
return 0;

}

Its still printing the same value for all of them. How does it happen??
Because all of those expressions evaluate to the same location. The
address of an array is the same as the address of the first element of
the array. The *types* of each expression are different (int (*)[3]
[3], int (*)[3], int *, and int (*)[3][3][3], respectively), but they
all refer to the same location.
Feb 4 '08 #8
Nikhil Bokare wrote:
>
Thanks for pointing out my mistakes.
This is my edited program:

#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%p %p %p %p",a,*a,**a,&a);
return 0;
}

Its still printing the same value for all of them. How does it happen??
Given the definition of "a":

int a[3][3][3];

What do the following actually mean:

a
*a
**a
&a

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Feb 4 '08 #9
Nikhil Bokare <nb*****@gmail.comwrites:
Thanks for pointing out my mistakes.
This is my edited program:

#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%p %p %p %p",a,*a,**a,&a);
return 0;
}

Its still printing the same value for all of them. How does it happen??
Read section 6 of the comp.lang.c FAQ.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 4 '08 #10
Nikhil Bokare wrote:
>
Thanks for pointing out my mistakes. This is my edited program:

#include <stdio.h>

int main() {
int a[3][3][3];
printf("%p %p %p %p",a,*a,**a,&a);
return 0;
}

Its still printing the same value for all of them. How does it
happen??
Because you are still lying to printf and getting undefined
behaviour. %p requires a void* parameter. I also fixed your
#include statement.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 4 '08 #11
Keith Thompson wrote:
>
.... snip ...
>
Yes, the program exhibits undefined behavior, and yes, that
absolutely should be fixed, but that doesn't actually explain
the behavior, and there is another explanation. There's
nothing wrong with pointing out the undefined behavior *and
then* answering the original question.
Undefined behaviour can do _anything_. No explanation is needed.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Feb 5 '08 #12
CBFalconer said:
Keith Thompson wrote:
>>
... snip ...
>>
Yes, the program exhibits undefined behavior, and yes, that
absolutely should be fixed, but that doesn't actually explain
the behavior, and there is another explanation. There's
nothing wrong with pointing out the undefined behavior *and
then* answering the original question.

Undefined behaviour can do _anything_.
Agreed, but there's nothing wrong with pointing out the undefined behaviour
*and then* answering the original question.
No explanation is needed.
The OP needed an explanation. The fact that his program was flawed does not
change this.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 5 '08 #13
"Nikhil Bokare" <nb*****@gmail.comwrote in message
news:73**********************************@l32g2000 hse.googlegroups.com...
#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}

I tried the above code and got the same value for a, *a , **a and &a.
Can anyone please tell me the reason behind such a behavior?
http://web.torek.net/torek/c/pa.html
Feb 5 '08 #14
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
... snip ...
>>
Yes, the program exhibits undefined behavior, and yes, that
absolutely should be fixed, but that doesn't actually explain
the behavior, and there is another explanation. There's
nothing wrong with pointing out the undefined behavior *and
then* answering the original question.

Undefined behaviour can do _anything_.
True.
No explanation is needed.
Nonsense. It was obvious from the beginning that the OP was
attempting to print the values of a, *a, **a, and &a, and wanted to
know why they all appeared to have the same value. He clearly did
require an explanation (i.e., he came to us for help), and digging
through the undefined behavior to get to what he was actually asking
about was easy enough that several of us managed to do it.

A C program can behave in arbitrarily obnoxious ways in the presence
of undefined behavior. We don't have to do likewise.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 5 '08 #15
Keith Thompson <ks***@mib.orgwrites:
CBFalconer <cb********@yahoo.comwrites:
>Keith Thompson wrote:
... snip ...
>>>
Yes, the program exhibits undefined behavior, and yes, that
absolutely should be fixed, but that doesn't actually explain
the behavior, and there is another explanation. There's
nothing wrong with pointing out the undefined behavior *and
then* answering the original question.

Undefined behaviour can do _anything_.

True.
> No explanation is needed.

Nonsense. It was obvious from the beginning that the OP was
attempting to print the values of a, *a, **a, and &a, and wanted to
know why they all appeared to have the same value. He clearly did
require an explanation (i.e., he came to us for help), and digging
through the undefined behavior to get to what he was actually asking
about was easy enough that several of us managed to do it.

A C program can behave in arbitrarily obnoxious ways in the presence
of undefined behavior. We don't have to do likewise.
Mr CBFalconer does not seem happy unless he is being curt and rude to a
new poster. I sometimes wonder why you can other more useful people even
bother replying to his obnoxious nonsense.
Feb 5 '08 #16

"Ravishankar S" <ra***********@in.bosch.comwrote in message
news:fo**********@news4.fe.internet.bosch.com...
"Nikhil Bokare" <nb*****@gmail.comwrote in message
news:73**********************************@l32g2000 hse.googlegroups.com...
>#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}

I tried the above code and got the same value for a, *a , **a and &a.
Can anyone please tell me the reason behind such a behavior?

http://web.torek.net/torek/c/pa.html
That's a pretty good link, especially
http://web.torek.net/torek/c/expr.html#therule a bit further on.

Text that tells you /about/ the language rather than try and teach you
programming.

[The 'rule'] "It falls out from a key fact: C does not have array values"

Finally someone has pointed out the elephant in the drawing room, or
whatever the saying is.

--
Bart
Feb 5 '08 #17
In article <87************@kvetch.smov.org>,
Keith Thompson <ks***@mib.orgwrote:
>CBFalconer <cb********@yahoo.comwrites:
>Keith Thompson wrote:
... snip ...
>>>
Yes, the program exhibits undefined behavior, and yes, that
absolutely should be fixed, but that doesn't actually explain
the behavior, and there is another explanation. There's
nothing wrong with pointing out the undefined behavior *and
then* answering the original question.

Undefined behaviour can do _anything_.

True.
> No explanation is needed.

Nonsense. It was obvious from the beginning that the OP was
attempting to print the values of a, *a, **a, and &a, and wanted to
know why they all appeared to have the same value. He clearly did
require an explanation (i.e., he came to us for help), and digging
through the undefined behavior to get to what he was actually asking
about was easy enough that several of us managed to do it.

A C program can behave in arbitrarily obnoxious ways in the presence
of undefined behavior. We don't have to do likewise.
Oh. The. Irony...

Feb 5 '08 #18

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

Similar topics

5
by: Abe Simpson | last post by:
Hello all, The application I am working on must never output numbers in a floating-point format, that is, something like 2e-002 is a big no-no. At the same time, it must output numbers in a...
2
by: Claudio | last post by:
     printf ("\n %u " , sizeof ( ub ) ) ;      printf ("\n %u " , sizeof ( sb ) ) ;      printf ("\n %u " , sizeof ( n ) ) ;...
3
by: Paul Watson | last post by:
It is clear that just using 'print' with variable names is relatively uncontrollable. However, I thought that using a format string would reign the problem in and give the desired output. Must...
13
by: Peter Ammon | last post by:
I invented a new (AFAIK) way of doing the O part of C's I/O that is safer than printf() without sacrificing much convenience. It looks a bit like C++'s formatted output, without the stupid gotchas...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
5
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned...
0
by: newbie | last post by:
i'm a newbie of c language. can anyone help me to implement the code so that I can get the ciphertext from the output. thanks. #ifndef _3DES_H #define _3DES_H #ifndef uint8 #define uint8 ...
18
by: rajpal_jatin | last post by:
int main() { int k; union jatin{ int i :5; char j :2; }; union jatin rajpal; k= sizeof(rajpal);
19
by: deepak | last post by:
How the following code is working. main() { int a = 38, b = 13; unsigned long long c; c = a * (1<<b) * 32000; printf("%llu", c);
9
ShawnRR
by: ShawnRR | last post by:
Hi all, This program is an easy "printing" Calculator... the problem i am having is that when I type in "0e" or "0E" for the program to exit it thinks i am typing in an exponent value and does not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.