FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


C program with 3D array, in Linux





anooptdas
I tried the following program in RHEL 4 and got some unexpected results

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

When i print the array the value pointed by q gets printed as the third element.
Can any one please help
BruceTheDauber
Quote:

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


Can I help by suggesting that in future you write code in a way that's not such a pain to read?

What were you doing? Testing the compiler?

Oh, and unless you're doing embedded stuff, switch to C++.[/quote]
muggle
If you replace the line
anooptdas wrote:
*q=***a;

with `q=**a;` it will work in the manner you probably expected.
Why? I think it will be interesting for you to figure out by yourself.
anooptdas
First thanks bruce and muggle for ur reply

bruce, this is not my original code, i got it from a test paper
i only tried to print the contents of the array
and got the wonderful result

muggle, thanks for the suggestion. it is working fine
but why does the original program affect the third element itself, every time
please try it out

thankyou
BruceTheDauber
anooptdas wrote:
First thanks bruce and muggle for ur reply

bruce, this is not my original code, i got it from a test paper
i only tried to print the contents of the array
and got the wonderful result

muggle, thanks for the suggestion. it is working fine
but why does the original program affect the third element itself, every time
please try it out

thankyou


If you look at the line I quoted, where the array is initialized, it's declared as a 3-d array a[2][2][2], but initialized as a 2-d array ([2][4]). Therefore, a[0][0] makes sense, but a[0][0][0] does not. By implicitly converting an integer into a pointer, you are pointing at an area of memory whose value is undefined.
Sappho
I rarely reply to C/C++ topics but i will make an exception. Your array declaration has some brackets missing and therefor its not properly initialized as BruceTheDauber already mentioned.

So just to fix it you need declaration like this:
anooptdas wrote:
int a[2][2][2]={{{15,2},{3,4}},{{5,6},{7,8}}};
BruceTheDauber
If you ran the code through a C++ compiler, instead of a C compiler, it would spot the mistake.
anooptdas
sorry but i hope it worked b'coz in C , a multidimensional array is also
stored sequentially as 1D array of 1D arrays
so it needs only 8 elements. am i wrong. please point out

and my actual doubt was regarding the output
why did the value given to *q replace the particular element every time

thankng u for ur responses
BruceTheDauber
anooptdas wrote:
sorry but i hope it worked b'coz in C , a multidimensional array is also
stored sequentially as 1D array of 1D arrays
so it needs only 8 elements. am i wrong.


Yes, you're wrong. A 2d array in C (and in C++) is represented as an array of pointers to 1d arrays.
muggle
Here:
anooptdas wrote:
int *p,*q;

you define variable `p` as pointer to an int. It means that `p` is a place in the memory where the address of an int variable can be stored.
In the beginning the content of `p` is undefined, you didn’t put anything in it and pointers are not initialized automatically. Undefined doesn’t mean that there is no value stored in this variable, there is something; we just don’t know what it is.
It seems that, happily, your `p` variable contains reference to the third element of your array.
By issuing this command:
anooptdas wrote:
*p=***a;

you assign the value of a[0][0][0] i.e. the first element of your array to the place pointed by variable `p` as I already said the third element of the array. That’s how you’re getting the `unexpected` behaviour.
As you can imagine `p` could point to any place of the memory and by issuing the above command you’ll overwrite it and the damages could be much bigger than simply getting the wrong output from the printf command.
The initialization of the array is fine, C ignores the {} pairs and stores the values sequentially.
Sappho
muggle wrote:
The initialization of the array is fine, C ignores the {} pairs and stores the values sequentially.


This depends on a compiler you are using. Wink

It should work however but its not a good thing to refrain from using valid syntax especially when you will come to more complex structures. Wink

Anyway nice catch with the pointers i missed it.
anooptdas
I was lot more wrong than that i expected
Thanks sapho, muggle and bruce for providing these informations
Using pointers in incorrect way, and getting atleast the output is a luck right!

Thanks I will modify the Qn and come with the next doubt Smile
Related topics
C++ TUTORIAL : INTRODUCTION TO C++
Want to start making games?
C/C++
A C program in Linux
A C program in Linux
CLOSE PLEASE-Need a C++ program - upto 200frih$
[C] Splitting names program
Deploying a C# program
changing system time using a c program under cygwin
C++ Software
C++ Program Basics
C++ program yazmaya baţlayalým artýk :=)
Writing your first C++ Program
My Small C# Program
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.