FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


A C program in Linux





anooptdas
Sorry I dont know if this is the right place to put this question
but i could not find a place for C

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
Georgeboy
Hmmm, maybe you could place your output and your expected output?
You made a 3D array, but you only defines a 2D array. At this time it seems you have made a big mistake about memory. You placed some elements in an array, but you haven't allocated enough memory to do this. You should be happy that you get an output and you don't get some rubbish... Always keep in mind when working with C, you are responsible for the memory allocation. It isn't like Java that your compiler will tell you that you are going out of bound...

Before closing, maybe I can tell you some tips. Use your debugger and you will see what the problem could be and maybe you can write a function to print your array or write it into some for-lus... It will make your code a lot easier to read...Wink

Greetz
anooptdas
Code:
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");
}


I know the code is very difficult to read, sorry.
i was trying to print the addresses, location name and elements of the array
but i shall giv the output:

15



0xbff40f40 a[0][0][0] 15
0xbff40f44 a[0][0][1] 2
0xbff40f48 a[0][1][0] 15
0xbff40f4c a[0][1][1] 4
0xbff40f50 a[1][0][0] 5
0xbff40f54 a[1][0][1] 6
0xbff40f58 a[1][1][0] 7
0xbff40f5c a[1][1][1] 8

but i expected the output to be

15



0xbff40f40 a[0][0][0] 15
0xbff40f44 a[0][0][1] 2
0xbff40f48 a[0][1][0] 3
0xbff40f4c a[0][1][1] 4
0xbff40f50 a[1][0][0] 5
0xbff40f54 a[1][0][1] 6
0xbff40f58 a[1][1][0] 7
0xbff40f5c a[1][1][1] 8

As a [2][2][2] 3D array can hold 8 elements , here i gave 8 elements, but in a different way. fortuantely that section is working
but the value that i am giving to q is overwriting the value in a[0][1][0]
yes i did not allocate any memory for q to point to
but y is it taking the third memory itself
i think the outputs will help u to explain

thankyou
Georgeboy
ok, I have rewritten your code. There where a lot of mistakes in this little program... Confused Your code is replaced by comment tags...

Code:

#include <stdio.h>
main()
{
   /*int a[2][2][2]={{15,2,3,4},{5,6,7,8}};*/
   int a[2][4]={{15,2,3,4},{5,6,7,8}};
   int *p,*q;
        /* *q=***a;*/
   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("%p a[0][0] %2d\n%p a[0][1] %2d\n%p a[0][2] %2d\n%p a[0][3] %2d\n%p a[1][0] %2d\n%p a[1][1] %2d\n%p a[1][2] %2d\n%p a[1][3] %2d\n",&a[0][0],a[0][0],&a[0][1],a[0][1],&a[0][2],a[0][2],&a[0][3],a[0][3],&a[1][0],a[1][0],&a[1][1],a[1][1],&a[1][2],a[1][2],&a[1][3],a[1][3]);
   printf("\n\n\n");
}


Copy this to your editor and you will see the output you wanted...Wink

I didn't know if you wanted to make a 2D or 3D array. You defined a 3D but you declared a 2D !!! If you want that q points to your first element, you only need to ask for the address of a and not the pointer of a pointer of a pointer... Make sure you really understand the baseconcepts of C. Otherwise you will stuck again in one of your next programs...

Greetz
anooptdas
Sorry, but i hav to disagree
A 3D array is also stored sequentially in memory, and if we give 8 elements(for this 2x2x2 array) it will be stored in its corresponding locations, no matter how we give those elements.

The name of a 3D array is similar to a pointer to a pointer to a pointer; so what is done is to take the first element and store it in the memory pointed to by q.Yes i know it is not fare to store something in an unknown or un allocated space. Better we could have assigned the address of array to an integer pointer, which was easier and better. but the particular code is taken from a tricky qn to find the output and i only tried to print the whole array, which when executed in Linux gave the unexpected result.
schizofrenikh
I wonder what does mean:
Code:
***a

I have always tought that a 3D array isn't an array of arrays of arrays of elements, but It's just an array with adresses calculated from indexes in different way. In this case if we declare:
Code:
a[2][2][2]

"a" is actually(but not in the sense of compilers typyfication, becouse of differences in treating when for example adding) a pointer to the first element of an array.
Code:
a == &a[0][0][0] == a[0][0] == a[0]
anooptdas
a == &a[0][0][0] == a[0][0] == a[0]
Yes all hav the same values in it. but they mean different things to the compiler
Also
a == &a[0][0][0] == a[0][0] == a[0] == *a[0][0][0] == *a[0][0]
==**a[0][0][0]

But some are pointer to pointers, some pointer to (pointer to pointers) ...

So if we increment each they will point to different locations
schizofrenikh
I am not sure whether &a[0][0][0] == *a[0][0][0]
I think that a[0][0][0] is int, so *a[0][0][0] will be a reference to an unallocated space in memory(address 0x000000ff)
Am I right?
hexkid
anooptdas wrote:
Code:
main()
{
int a[2][2][2]={{15,2,3,4},{5,6,7,8}};
int *p,*q;
*q=***a;
printf("\n%d\n",*q);
/* ... rest of code removed ... */


STOP RIGHT HERE! You're invoking Undefined Behaviour by using a variadic function without a prototype.

Your "program" can do whatever it wants now; and that includes printing what it did, or printing what you want, or print "next time you run this program there will be nasal demons oozing out of your left nostril."
varun_dodla
You are violating a memory rule by directly initialising a value to a pointer variable. Try as below and it will work perfectly.



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");
}



I only changed a single statement (italisized) to initialize q to address of ***a.
anooptdas
Sorry hexkid and varun
this code does some illegall or undefined things, i agree
and as i hav said it is not my code, i gotit from a tricky problem and i only tried to see the contents of the array. also i undestood what varun said.
bur hexkid, can u please help me understand what is variadic function without a prototype.

thankyou
hexkid
anooptdas wrote:
can u please help me understand what is variadic function without a prototype.


A variadic function is a function which accepts a indeterminated number of parameters (at least 1 in C; can be zero in C++), for example printf().

All variadic function in all versions of C need to have a prototype for them in scope. In old versions of C, if a function didn't have a prototype in scope it wouldn't be an error if, among other things, the function was not variadic. To get the right prototype for printf() include <stdio.h> in your program.

For example
Code:
#include <stdio.h> /* printf() */

int otherfun(char); /* prototype */

int main(void) {
  printf("%d\n", 42); /* ok, prototype in scope */
  printf("%d\n", somefun(42)); /* no prototype for somefun()
                                * older compilers can accept this
                                * if somefun() accepts an int and returns an int
                                * but it's best to always provide prototypes */
  printf("%d\n", otherfun(42)); /* ok, prototype in scope */
}
anooptdas
thankyou hexkid
Related topics
C++ TUTORIAL : INTRODUCTION TO C++
What's wrong with this C code???
simple webserver on cable line.
A C program in Linux
C program with 3D array, 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 -> Computers -> Operating Systems

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