473,797 Members | 3,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 D Array Prob Please Help

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
Feb 20 '08 #1
8 1260
sa*********@gma il.com 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;
}

Feb 20 '08 #2
sa*********@gma il.com 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
Feb 20 '08 #3
santosh wrote:
sa*********@gma il.com 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

Feb 20 '08 #4
Richard Heathfield wrote:
sa*********@gma il.com 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
Feb 20 '08 #5
Bartc said:
Richard Heathfield wrote:
>sa*********@gma il.com 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
Feb 20 '08 #6

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:yu******** *************** *******@bt.com. ..
Bartc said:
>Richard Heathfield wrote:
>>sa*********@gma il.com 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
Feb 20 '08 #7
Bartc said:
>
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:yu******** *************** *******@bt.com. ..
>Bartc said:
>>Richard Heathfield wrote:
sa*********@gma il.com 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
Feb 20 '08 #8
sa*********@gma il.com 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;
}

Feb 20 '08 #9

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

Similar topics

204
13134
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
18
3171
by: zehanwang | last post by:
Compiler: Visual C++ 6.0 My code: //============================== # include "stdio.h" # include "stdlib.h" int main() { int v; long i; for (i=1; i<=10000000; i++) v =i;
2
326
by: jophrthomas | last post by:
hi...i am getting a error here...saying : **quote** The ConnectionString property has not been initialized. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.
5
7351
by: B | last post by:
Hello, I am trying to migrate a vb6 app to vb.net. In one piece of the vb6 app I have a label where the label backcolor changes every 5 seconds on a timer. So I created an array of colors as follows and then run the array in the timer event in vb6. Not working in vb.net. How would I do this in vb.net? vb6 code:
4
1530
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those who experience. (For some reason I have to implement Array not ArrayLists) Below are the simple following code: Dim Array() As String Dim intCounter As Integer
20
3532
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int *)a2; // now a1=a2, a1=a2, etc.
2
1519
by: mnacw | last post by:
Can anybody help me to resolve this prob. i have installed Visual Studio 2005 Professional edition. I am working in VB.Net. When I tried to connect to database it is connected but when i make some changes in table and try to update it does not work. It does not give any error. It seems like all fine. but when I retrieve the data via application menas data grid control on form. shows only previous data. no updates. i am using acess...
8
1880
by: isaac2004 | last post by:
hello, i posted with a topic like this but got no real feedback(prob cuz of lapse in my explanation) so i am trying it again. i am trying to set up a function that brings in a txt file and adds the file into a 2d array. I have this to get the file. #include <iostream> #include <string> #include <fstream> using namespace std;
1
1386
by: shariquehabib | last post by:
Hi all, I am defining array of structure like that : STRUCT_NAME array. I am fetching some records from EMP table and copying into that array one by one using while loop. But it will work only 1024 records if records will be greater than 1024 then it will not work. If I increase the size as 1030 then it will work for 1030 records. But If I increase the size as 1031 or 1100 then it will show “application error”.
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9536
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10245
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.