There is a pointer-to-an-Array of Arrays i.e. NameList in the code. I want the contents of each of the Arrays in the Pointer(NameList) to get printed one by one. The below code is not able do the task. Pls. help.
int Data1[] = {10,10};
int Data2[] = {20,20};
int Data3[] = {30,30};
int *NameList[] = {Data1, Data2, Data3};
main()
{ Function(NameList); }
Function(int *ArrayPointer)
{
int i, j, index=0;
for (i=0; i < 3; i++)
{
for (j=0; j < 2; j++)
{
//It does not print the data
printf("\nName: %s", ArrayPointer[index++]);
}
index=0; //Counter reset to 0
ArrayPointer++; //Pointer is incremented by one to pick next array in the pointer
}
}
print("code sample");
Another note from the original poster of the question:
I have completed a pacman game in Turbo C. I was polishing some graphics routines so that it can be reused again easily. This is only a small sample created for the purpose of help and understanding the concept. All data in the code actually are char arrays for sprites. Now i simply want to call a function passing the pointer so that each arrays in the pointer are drawn to the screen. How can this code be modified to handle this? Im actually stuck up here.
has the type of
and
has the type of
You have a two-dimensional array there. Most likely you meant:
On that note, ANSI C/C89/C99 functions have an explicit return type (or
void
), e.g.The value pointed to by
ArrayPointer
is anint
, not a string. ThusShould be written as something else.
A third thing:
index
==j
in your code. Thusindex
can be removed in favor ofj
.i
andj
are probably not good variable names here becauseArrayPointer
is not name describing a list of something.If you need more help, please post what you're looking to do, because you code has several bugs (and oddities) in it.
I made some corrections in your program please find them and compare. I know its too late for the response. But I saw this today itself.
Explanation:
When you pass NameList to Function as a pointer to an integer, what gets passed is the address of the first element of the array, which happens to be Data1 (an address to an array). But since this address is held in an array of ints, it will be considered as an integer. To make it behave like an address to an array(or, for that matter pointer to an int) you need to cast it to (int *). Thats what I did in :
printf("\nName: %d\n", *((int *)ArrayPointer[i] + index));
main
has to return a type. You forget to put "int" as a return type (implicit int in C++ is banned).Having said that, i'm not sure what you mean by
ArrayPointer[index++]
would, as it is defined in the parameter list, return anint
. How is that supposed to store a name ? It will store an integer!Once again, that said, you can't call that Function (pun intended) with that particular argument. Let's view your types:
Contrary to what you said, NameList is not a pointer to an array of arrays. I feel i need to show you what that would be:
That wouldn't make sense at all. So what do you have?
That is what you got. And you pass
NameList
to a Function that wants a pointer to anint
. It must fail already at the time you callFunction
inmain
! I've got no idea what you mean byname
in that line in Function. But if you want to print out the integers that are stored in the arrays pointed to (by pointers to their first element), you can do it like this (keeping your code as much as i can):I keep preaching people asking questions the difference between a pointer and an array. It's crucial to write correct code. I hope i could help. At the end, just a little though about the difference between
int[]
andint*
. The first is an incomplete array type, while the second is a complete type (pointer to int):s2
's type now has a type different from int[], because you initialized the array which would have incomplete type, the arrays1
become complete after defined. It has type ofint[4]
. In parameter lists, however, there exist a special rule, which will cause any array type (even complete ones!) to be equivalent to a pointer to their first argument. Thus:That's because you can't pass arrays by value. The compiler abuses that to make array types equivalent to pointers to their first element. Same deal with functions:
Because you can't pass functions by value (huh, doesn't even make sense at all to me), the compiler abuses it to make a function type in a parameter list equivalent to a pointer to that function type.
Paramod,
is there a function you need to call that takes something along the lines of
?
Then you need to have all data in a single chunk of memory. In your example, the compiler allocates a separate chunk for every row, and then another chunk to hold the three pointers to rows. Thus, the array is kept in 4 different places.
What you need is a multidimensional array rather than array of arrays. Initialize your data like this:
Then you can call your function like this:
Using multidimensional array places all elements in one block of memory. The advantage is, you can pass the piointer to first element, and you know where all other elements are. The disadvantage - all rows are allocated the same size.
Darn it litb, once again you beat me to the punch by mere minutes. (If only I didn't have kids who keep waking up...)
Ahh, what the hell. Perhaps this will still be useful to somebody.
Oh, and just to nail this thing down:
Other than that little distinction, there really isn't a big difference between int[] and int*. (Consider how many folks declare *main(int argc, char **argv) vs main(int argc, char * argv[]).)
ATTENTION: All memory addresses here are fictional. I'm just making them up to illustrate a point.
Given:
We now have 3 blocks of memory. Say:
Where:
NO, I'm not going to get into big-endian vs little-endian byte ordering here!
Yes, in this case, Data1[2] == Data2[0]. But you can't rely on your compiler laying things out in memory the same way I've laid them out here.
Next:
So we now have another block of memory. Say:
Where:
Note that NameList is of int ** type, and NOT int* type!
We can then write:
ArrayPointer resolves to (int**)0xffff0018.
ArrayPointer[0] == *( (int**) 0xffff0018 ) == (int*)(0xffff0000) == Data1.
ArrayPointer[0][1] == *( ( * (int**) 0xffff0018 ) + 1 ) == (int) * ( (int*)0xffff0000 + 1 ) == (int) * (int*) 0xffff0004 == Data1[1].
You may want to review pointer arithmetic: array[N] == *( array + N )
Judging by your "answer" (you really should have edited your original post and added this information), you probably want something like this:
A fairly simple
for
loop to iterate through elements in an array.