Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 2 D Array Prob Please Help

Reply
Thread Tools

2 D Array Prob Please Help

 
 
sachinv1821@gmail.com
Guest
Posts: n/a
 
      02-20-2008
hi all i have simple Problem please tell me the Solution if u know??
main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
printf("%u %u %u",a[0],a[1],a[2]);
}
when i execute this program i am getting a Fixed address Value(multi
digit value)
can any body explain me ahy this is giving the same value for
different index

Thanks In Advance
-Sachin
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      02-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> hi all i have simple Problem please tell me the Solution if u know??
> main()
> {
> int a[3][3]={1,2,3,4,5,6,7,8,9};
> printf("%u %u %u",a[0],a[1],a[2]);
> }
> when i execute this program i am getting a Fixed address Value(multi
> digit value)
> can any body explain me ahy this is giving the same value for
> different index


You probably want:

#include <stdio.h>

int main(void) {
int a[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
printf("%d, %d, %d\n", a[0][0], a[1][0], a[2][0]);
return 0;
}

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      02-20-2008
(E-Mail Removed) said:

> hi all i have simple Problem please tell me the Solution if u know??
> main()
> {
> int a[3][3]={1,2,3,4,5,6,7,8,9};
> printf("%u %u %u",a[0],a[1],a[2]);
> }


You forgot to #include <stdio.h> - you'll need to fix that if you want to
use printf in your program, because its behaviour if you don't is allowed
to be arbitrary. You'll also want to use int main(void) rather than just
main(), and add a return statement to your function, e.g. return 0;

The problem with your program is that you are expecting printf to do more
than it can in fact do. It knows about chars, and unsigned chars, and
short ints, and unsigned short ints, and ints, and unsigned ints, and long
ints, and unsigned long ints, and doubles, and long doubles. It even knows
about pointers to void. But that's all it knows about, in the way of data
types.

a[0] is equivalent to *(a + 0), i.e. *a. (C doesn't let you use array
values, so in a value context like this, the value you actually get is a
pointer to the array's first element, i.e. a pointer to an array of three
int.) Dereferencing gives the array itself, i.e. an int[3], which decays
to an int *. But printf doesn't understand about pointers-to-int (except
in the pathological case of %n, which doesn't do what you want). And even
if it did, it wouldn't understand them in the context of %u, which is used
for printing unsigned ints, not pointers-to-int.

The proper course is to use %p as the format specifier and (void *)a[0] as
the argument.

Unfortunately, converting a pointer to some type (or an array of some
type!) into a void * is a bit like converting a place into GPS
co-ordinates. Contextual information is lost. Consider a house, a room, a
wall, a brick, and a "brick atom". All could have the same GPS co-ords,
even though they are very different things.

The right way to think about this is to work out exactly *why* you need to
know the information you're trying to display. The chances are that you
don't actually need it, and are merely curious - in which case, the proper
answer is that it doesn't /matter/ what glyphs are scribbled on your
screen when you print an address, provided only that you understand the C
type system. If you genuinely *do* need the information, it will either be
for a spurious reason (e.g. you're a student, and your teacher is stupid
enough to require you to find out this information) or for a real
technical reason. If the latter, you will be sufficiently experienced to
realise that you're trying to step outside the bounds of behaviour that C
can guarantee, and into the realms of architecture-specific details.

--
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
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      02-20-2008
santosh wrote:
> (E-Mail Removed) wrote:
>
>> hi all i have simple Problem please tell me the Solution if u know??
>> main()
>> {
>> int a[3][3]={1,2,3,4,5,6,7,8,9};
>> printf("%u %u %u",a[0],a[1],a[2]);
>> }
>> when i execute this program i am getting a Fixed address Value(multi
>> digit value)
>> can any body explain me ahy this is giving the same value for
>> different index

>
> You probably want:
>
> #include <stdio.h>
>
> int main(void) {
> int a[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
> printf("%d, %d, %d\n", a[0][0], a[1][0], a[2][0]);
> return 0;
> }


Or more likely:

printf("%d %d %d", a[0][0], a[0][1], a[0][2]);

Which gives the output 1 2 3

--
Bart



 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      02-20-2008
Richard Heathfield wrote:
> (E-Mail Removed) said:
>
>> hi all i have simple Problem please tell me the Solution if u know??
>> main()
>> {
>> int a[3][3]={1,2,3,4,5,6,7,8,9};
>> printf("%u %u %u",a[0],a[1],a[2]);
>> }

>

<snip>

You haven't explained why a linear list of 9 values is acceptable to
initialise a 3x3 array. Even if (yet another) quirk of the language allows
this, it must make more sense to write:

int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};


--
Bart


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-20-2008
Bartc said:

> Richard Heathfield wrote:
>> (E-Mail Removed) said:
>>
>>> hi all i have simple Problem please tell me the Solution if u know??
>>> main()
>>> {
>>> int a[3][3]={1,2,3,4,5,6,7,8,9};
>>> printf("%u %u %u",a[0],a[1],a[2]);
>>> }

>>

> <snip>
>
> You haven't explained why a linear list of 9 values is acceptable to
> initialise a 3x3 array. Even if (yet another) quirk of the language
> allows this, it must make more sense to write:
>
> int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};


Yes, it does make more sense to write it like that.

Alas, none of us has infinite time, care, or patience. Whilst it would be
wonderful to think that every article we post here explains absolutely
everything the OP needs to know (whether or not they realise the need), in
practice life doesn't work like that. Very few replies are as complete as
we would like them to be.

Perhaps you yourself would care to explain to the OP why such a linear
initialiser list is, or is not, acceptable.

--
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
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      02-20-2008

"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bartc said:
>
>> Richard Heathfield wrote:
>>> (E-Mail Removed) said:
>>>
>>>> hi all i have simple Problem please tell me the Solution if u know??
>>>> main()
>>>> {
>>>> int a[3][3]={1,2,3,4,5,6,7,8,9};
>>>> printf("%u %u %u",a[0],a[1],a[2]);
>>>> }
>>>

>> <snip>
>>
>> You haven't explained why a linear list of 9 values is acceptable to
>> initialise a 3x3 array. Even if (yet another) quirk of the language
>> allows this, it must make more sense to write:
>>
>> int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};

>
> Yes, it does make more sense to write it like that.


> Perhaps you yourself would care to explain to the OP why such a linear
> initialiser list is, or is not, acceptable.


Actually, I thought you might explain it to /me/..

But never mind, it's clearly a hangover from the early days of C, and must
now be common and accepted practice if it's use elicits no comments.

--
Bart


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-20-2008
Bartc said:

>
> "Richard Heathfield" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Bartc said:
>>
>>> Richard Heathfield wrote:
>>>> (E-Mail Removed) said:
>>>>
>>>>> hi all i have simple Problem please tell me the Solution if u know??
>>>>> main()
>>>>> {
>>>>> int a[3][3]={1,2,3,4,5,6,7,8,9};
>>>>> printf("%u %u %u",a[0],a[1],a[2]);
>>>>> }
>>>>
>>> <snip>
>>>
>>> You haven't explained why a linear list of 9 values is acceptable to
>>> initialise a 3x3 array. Even if (yet another) quirk of the language
>>> allows this, it must make more sense to write:
>>>
>>> int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};

>>
>> Yes, it does make more sense to write it like that.

>
>> Perhaps you yourself would care to explain to the OP why such a linear
>> initialiser list is, or is not, acceptable.

>
> Actually, I thought you might explain it to /me/..
>
> But never mind, it's clearly a hangover from the early days of C, and
> must now be common and accepted practice if it's use elicits no comments.


It's perfectly legal. Read the Initialization section of the Standard, and
you'll even see an example:

*** example from 3.5.7 of C89 (draft) ***

float y[4][3] = {
{ 1, 3, 5 },
{ 2, 4, 6 },
{ 3, 5, 7 },
};

is a definition with a fully bracketed initialization: 1, 3, and 5
initialize the first row of the array object y[0] , namely y[0][0] ,
y[0][1] , and y[0][2] . Likewise the next two lines initialize y[1]
and y[2] . The initializer ends early, so y[3] is initialized with
zeros. Precisely the same effect could have been achieved by

float y[4][3] = {
1, 3, 5, 2, 4, 6, 3, 5, 7
};

The initializer for y[0] does not begin with a left brace, so three
items from the list are used. Likewise the next three are taken
successively for y[1] and y[2] .

*** end of example ***

Examples are not normative, of course, but I think the intent of the
normative text is better expressed here than in the normative text itself!

--
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
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-20-2008
(E-Mail Removed) wrote:
> hi all i have simple Problem please tell me the Solution if u know??
> main()
> {
> int a[3][3]={1,2,3,4,5,6,7,8,9};
> printf("%u %u %u",a[0],a[1],a[2]);
> }
> when i execute this program i am getting a Fixed address Value(multi
> digit value)
> can any body explain me ahy this is giving the same value for
> different index


I doubt that that's true, but on the off-chance that it is, try this
_legal_ C program and see what you get:

#include <stdio.h>

int main(void)
{
int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
printf("%p %p %p\n", (void *) a[0], (void *) a[1], (void *) a[2]);
return 0;
}

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-20-2008
Martin Ambuhl <(E-Mail Removed)> writes:
> (E-Mail Removed) wrote:
>> hi all i have simple Problem please tell me the Solution if u know??
>> main()
>> {
>> int a[3][3]={1,2,3,4,5,6,7,8,9};
>> printf("%u %u %u",a[0],a[1],a[2]);
>> }
>> when i execute this program i am getting a Fixed address Value(multi
>> digit value)
>> can any body explain me ahy this is giving the same value for
>> different index

>
> I doubt that that's true, but on the off-chance that it is, try this
> _legal_ C program and see what you get:
>
> #include <stdio.h>
>
> int main(void)
> {
> int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
> printf("%p %p %p\n", (void *) a[0], (void *) a[1], (void *) a[2]);
> return 0;
> }


Getting the same output three times in the OP's program isn't entirely
implausible. If, for example, unsigned int is 32 bits and pointers
are 64 bits, the "%u" format might be causing printf to print just the
"high-order" 32 bits of each pointer value, which is very likely to be
the same for all three. (I put the phrase "high-order" in quotation
marks because C doesn't define any such concept for pointers.)

On the other hand, the byte ordering of the x86 systems that are all
the rage these days is such that you'd probably get the "low-order"
bits of the pointer values, which would almost certainly differ. But
it could also depend on the vagaries of the parameter passing
mechanisms used by the particular compiler.

Of course, the real problems are that (a) the program exhibits
undefined behavior, and the output may not be entirely meaningless,
but it's entirely system-specific and the C standard says nothing
about what the program should do, and (b) the original poster didn't
show us his actual output (perhaps he didn't really get the same
result each time). In principle, *any* output is possible, including
what the OP said he saw.

I'd use "%p\n%p\n%p\n" rather than "%p %p %p\n"; it makes it easier to
compare the successive outputs visually.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"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
Audio prob that's more than a sound card prob LineOut Computer Support 2 04-19-2008 02:30 PM
Dial up box prob , help needed please! gary Computer Support 0 08-04-2005 11:30 PM
Help with scanner prob please!!!! Computer Support 2 08-19-2004 11:22 AM
Printer prob and also networking prob Gareth not NLL or anybody else. Computer Support 2 01-02-2004 07:48 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments