2 D Array Prob Please Help

S

sachinv1821

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
 
S

santosh

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;
}
 
R

Richard Heathfield

(e-mail address 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.
 
B

Bartc

santosh 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]);
}
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
 
B

Bartc

Richard said:
(e-mail address 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}};
 
R

Richard Heathfield

Bartc said:
Richard said:
(e-mail address 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.
 
B

Bartc

Richard Heathfield said:
Bartc said:
Richard said:
(e-mail address 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.
 
R

Richard Heathfield

Bartc said:
Richard Heathfield said:
Bartc said:
Richard Heathfield wrote:
(e-mail address 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!
 
M

Martin Ambuhl

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;
}
 
K

Keith Thompson

Martin Ambuhl 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]);
}
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.
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top