通过函数将指针传递给数组

时间:2021-06-21 21:33:30

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.

在代码中有一个指向数组的指针,即NameList。我希望指针(NameList)中每个数组的内容逐个打印。以下代码无法完成任务。 PLS。救命。

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.

我在Turbo C中完成了一个pacman游戏。我正在抛光一些图形例程,以便它可以轻松地重复使用。这只是为了帮助和理解这个概念而创建的一个小样本。代码中的所有数据实际上都是精灵的char数组。现在我只想调用一个传递指针的函数,以便指针中的每个数组都被绘制到屏幕上。如何修改此代码来处理此问题?我实际上被困在这里。

6 个解决方案

#1


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:

哦,只是为了解决这个问题:

  • Arrays, such as int a[4] allocate memory space for their data.
  • 诸如int a [4]之类的数组为其数据分配内存空间。

  • Pointers, such as int * p allocate just enouch memory space for a pointer to another spot in memory.
  • 指针(例如int * p)仅为内存中的另一个点指定内存空间。

  • That's why we can use sizeof on arrays and get the full memory footprint, but not on pointers.
  • 这就是为什么我们可以在数组上使用sizeof并获得完整的内存占用,而不是指针。

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[]).)

除了那个小小的区别之外,int []和int *之间确实没有太大的区别。 (考虑有多少人声明* 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:

int Data1[] = {10,11};
int Data2[] = {20,22};
int Data3[] = {30,33};

We now have 3 blocks of memory. Say:

我们现在有3块内存。说:

0xffff0000-0xffff0003  with a value of (int)(10)
0xffff0004-0xffff0007  with a value of (int)(11)

0xffff0008-0xffff000b  with a value of (int)(20)
0xffff000c-0xffff000f  with a value of (int)(22)

0xffff0010-0xffff0013  with a value of (int)(30)
0xffff0014-0xffff0017  with a value of (int)(33)

Where:

Data1 == & Data1 [0] == 0xffff0000
Data2 == & Data2 [0] == 0xffff0008
Data3 == & Data3 [0] == 0xffff0010

NO, I'm not going to get into big-endian vs little-endian byte ordering here!

不,我不会在这里进入big-endian vs little-endian字节排序!

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.

是的,在这种情况下,Data1 [2] == Data2 [0]。但你不能依赖你的编译器在内存中放置内容,就像我在这里放置它们一样。

Next:

int *NameList[] = {Data1, Data2, Data3};

So we now have another block of memory. Say:

所以我们现在有另一块内存。说:

0xffff0018-0xffff001b  with a value of (int*)(0xffff0000)
0xffff001c-0xffff001f  with a value of (int*)(0xffff0008)
0xffff0020-0xffff0023  with a value of (int*)(0xffff0010)

Where:

NameList == & NameList [0] == 0xffff0018

Note that NameList is of int ** type, and NOT int* type!

请注意,NameList是int **类型,而NOT int *类型!

We can then write:

然后我们可以写:

void Function(int **ArrayPointer)
{
  for ( int i=0; i < 3;  i++ )
    for ( int j=0; j < 2; j++)
      printf("Name: %d\n",  ArrayPointer[i][j] );
}

int main() {  Function(NameList); }

ArrayPointer resolves to (int**)0xffff0018.

ArrayPointer解析为(int **)0xffff0018。

ArrayPointer[0] == *( (int**) 0xffff0018 ) == (int*)(0xffff0000) == Data1.

ArrayPointer [0] == *((int **)0xffff0018)==(int *)(0xffff0000)== Data1。

ArrayPointer[0][1] == *( ( * (int**) 0xffff0018 ) + 1 ) == (int) * ( (int*)0xffff0000 + 1 ) == (int) * (int*) 0xffff0004 == Data1[1].

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 )

您可能想要查看指针算术:array [N] == *(array + N)

#2


main has to return a type. You forget to put "int" as a return type (implicit int in C++ is banned).

main必须返回一个类型。您忘记将“int”作为返回类型(禁止在C ++中使用隐式int)。

Having said that, i'm not sure what you mean by

话虽如此,我不确定你的意思

// It does not print the data
printf("\nName: %s",  ArrayPointer[index++]);

ArrayPointer[index++] would, as it is defined in the parameter list, return an int. How is that supposed to store a name ? It will store an integer!

ArrayPointer [index ++]将在参数列表中定义,返回一个int。怎么会存储一个名字?它会存储一个整数!

Once again, that said, you can't call that Function (pun intended) with that particular argument. Let's view your types:

再说一次,你说不能用那个特定的参数调用那个函数(双关语)。让我们来看看你的类型:

int Data1[] = {10,10};
int Data2[] = {20,20};
int Data3[] = {30,30};

int *NameList[] = {Data1, Data2, Data3};

Data1      Data2      Data3      NameList
int[2]     int[2]     int[2]     int*[3]

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:

与您所说的相反,NameList不是指向数组数组的指针。我觉得我需要告诉你那将是什么:

int (*NameList)[N][M] = Some3DimensionalArray;

That wouldn't make sense at all. So what do you have?

这根本没有意义。那你有什么?

Data1 = array of 2 int
Data2 = array of 2 int
Data3 = array of 2 int
NameList = array of poiners to int

That is what you got. And you pass NameList to a Function that wants a pointer to an int. It must fail already at the time you call Function in main! I've got no idea what you mean by name 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):

这就是你得到的。然后将NameList传递给想要指向int的指针的Function。当你在main中调用Function时它必须已经失败!我不知道你在Function的那一行中的名字是什么意思。但是如果你想打印存储在指向的数组中的整数(通过指向它们的第一个元素的指针),你可以这样做(保持你的代码尽可能多):

// don't forget the return type, mate. Also see below
void 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. It is not a string, 
             // but an int!
             printf("\nName: %d\n",  ArrayPointer[i][index++]);
          } 
        index=0;         //Counter reset to 0
        // no need to increment the pointer. that's what the indexing by i 
        // above does
        // ArrayPointer++;  
      }
  }

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[] and int*. The first is an incomplete array type, while the second is a complete type (pointer to int):

我一直在讲道人们问问指针和数组之间的区别。编写正确的代码至关重要。我希望我能提供帮助。最后,只是关于int []和int *之间的区别。第一个是不完整的数组类型,而第二个是完整类型(指向int的指针):

typedef int Single[]; // array of indeterminate size.
typedef int *Pointer; // pointer to int

Single s1 = { 1, 2, 3, 4 }; // works!
Pointer s2 = { 1, 2, 3, 4 }; // no no, doesn't work. s2 wants an address

s2's type now has a type different from int[], because you initialized the array which would have incomplete type, the array s1 become complete after defined. It has type of int[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:

s2的类型现在具有与int []不同的类型,因为您初始化了具有不完整类型的数组,数组s1在定义后变为完整。它的类型为int [4]。但是,在参数列表中,存在一个特殊规则,它将导致任何数组类型(甚至完整的数组!)等同于指向其第一个参数的指针。从而:

void f(int *a) <=> void f(int a[]) <=> void f(int a[42]);
void f(int (*a)[42]) <=> void f(int a[][42]) <=> void f(int a[13][42])
// ...

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:

那是因为你不能按值传递数组。编译器滥用它来使数组类型等同于指向其第一个元素的指针。同样处理功能:

void f(int a()) <=> void f(int (*a)())

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.

因为你不能通过值传递函数(呵呵,对我来说根本没有意义),编译器滥用它来使参数列表中的函数类型等效于指向该函数类型的指针。

#3


int Data1[]

has the type of

有类型的

int *

and

int *NameList[]

has the type of

有类型的

int **

You have a two-dimensional array there. Most likely you meant:

那里有一个二维数组。你很可能意味着:

Function(int **ArrayPointer)

On that note, ANSI C/C89/C99 functions have an explicit return type (or void), e.g.

在这方面,ANSI C / C89 / C99函数具有明确的返回类型(或无效),例如,

int main() { ... }
void Function() { ... }

The value pointed to by ArrayPointer is an int, not a string. Thus

ArrayPointer指向的值是int,而不是字符串。从而

printf("\nName: %s",  ArrayPointer[index++]);

Should be written as something else.

应该写成别的东西。

A third thing: index == j in your code. Thus index can be removed in favor of j.

第三件事:代码中的index == j。因此可以删除索引以支持j。

i and j are probably not good variable names here because ArrayPointer is not name describing a list of something.

i和j在这里可能不是很好的变量名,因为ArrayPointer不是描述某事物列表的名称。

If you need more help, please post what you're looking to do, because you code has several bugs (and oddities) in it.

如果您需要更多帮助,请发布您要查找的内容,因为您的代码中存在多个错误(和奇怪的内容)。

#4


Judging by your "answer" (you really should have edited your original post and added this information), you probably want something like this:

根据你的“答案”判断(你真的应该编辑你的原始帖子并添加这些信息),你可能想要这样的东西:

void draw_all_sprites(Sprite *sprites, size_t num_sprites)
{
    Sprite *cur_sprite;
    size_t i;

    for(i = 0; i < num_sprites; ++i)
    {
        draw_sprite(cur_sprite);
        ++cur_sprite;
    }
}

A fairly simple for loop to iterate through elements in an array.

一个相当简单的for循环来迭代数组中的元素。

#5


Paramod,

is there a function you need to call that takes something along the lines of

是否有一个你需要调用的函数,它需要一些东西

void drawSprite(int *data, int rows, int cols)

?

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.

然后,您需要将所有数据放在一块内存中。在您的示例中,编译器为每一行分配一个单独的块,然后为另一个块分配三个指向行的指针。因此,阵列保持在4个不同的位置。

What you need is a multidimensional array rather than array of arrays. Initialize your data like this:

你需要的是一个多维数组而不是数组数组。像这样初始化您的数据:

int data[3,2] = {{10,10},{20,20},{30,30}};

Then you can call your function like this:

然后你可以像这样调用你的函数:

drawSprite(&data[0,0], 3, 2);

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.

使用多维数组将所有元素放在一个内存块中。优点是,您可以将piointer传递给第一个元素,并且知道所有其他元素的位置。缺点 - 所有行都分配了相同的大小。

#6


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.

我在你的程序中做了一些更正,请找到它们并进行比较。我知道答案太迟了。但我今天看到了这一点。

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 < 5; j++)
          {
             //It does not print the data
             printf("\nName: %d\n", *((int *)ArrayPointer[i] + index));
         index++;
          } 
        index=0;         //Counter reset to 0
      }
  }

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

说明:当您将NameList作为指向整数的指针传递给Function时,传递的是数组的第一个元素的地址,恰好是Data1(数组的地址)。但由于此地址以int数组保存,因此它将被视为整数。要使它的行为类似于数组的地址(或者,对于指向int的指针),您需要将其强制转换为(int *)。这就是我所做的:printf(“\ nName:%d \ n”,*((int *)ArrayPointer [i] + index));

#1


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:

哦,只是为了解决这个问题:

  • Arrays, such as int a[4] allocate memory space for their data.
  • 诸如int a [4]之类的数组为其数据分配内存空间。

  • Pointers, such as int * p allocate just enouch memory space for a pointer to another spot in memory.
  • 指针(例如int * p)仅为内存中的另一个点指定内存空间。

  • That's why we can use sizeof on arrays and get the full memory footprint, but not on pointers.
  • 这就是为什么我们可以在数组上使用sizeof并获得完整的内存占用,而不是指针。

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[]).)

除了那个小小的区别之外,int []和int *之间确实没有太大的区别。 (考虑有多少人声明* 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:

int Data1[] = {10,11};
int Data2[] = {20,22};
int Data3[] = {30,33};

We now have 3 blocks of memory. Say:

我们现在有3块内存。说:

0xffff0000-0xffff0003  with a value of (int)(10)
0xffff0004-0xffff0007  with a value of (int)(11)

0xffff0008-0xffff000b  with a value of (int)(20)
0xffff000c-0xffff000f  with a value of (int)(22)

0xffff0010-0xffff0013  with a value of (int)(30)
0xffff0014-0xffff0017  with a value of (int)(33)

Where:

Data1 == & Data1 [0] == 0xffff0000
Data2 == & Data2 [0] == 0xffff0008
Data3 == & Data3 [0] == 0xffff0010

NO, I'm not going to get into big-endian vs little-endian byte ordering here!

不,我不会在这里进入big-endian vs little-endian字节排序!

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.

是的,在这种情况下,Data1 [2] == Data2 [0]。但你不能依赖你的编译器在内存中放置内容,就像我在这里放置它们一样。

Next:

int *NameList[] = {Data1, Data2, Data3};

So we now have another block of memory. Say:

所以我们现在有另一块内存。说:

0xffff0018-0xffff001b  with a value of (int*)(0xffff0000)
0xffff001c-0xffff001f  with a value of (int*)(0xffff0008)
0xffff0020-0xffff0023  with a value of (int*)(0xffff0010)

Where:

NameList == & NameList [0] == 0xffff0018

Note that NameList is of int ** type, and NOT int* type!

请注意,NameList是int **类型,而NOT int *类型!

We can then write:

然后我们可以写:

void Function(int **ArrayPointer)
{
  for ( int i=0; i < 3;  i++ )
    for ( int j=0; j < 2; j++)
      printf("Name: %d\n",  ArrayPointer[i][j] );
}

int main() {  Function(NameList); }

ArrayPointer resolves to (int**)0xffff0018.

ArrayPointer解析为(int **)0xffff0018。

ArrayPointer[0] == *( (int**) 0xffff0018 ) == (int*)(0xffff0000) == Data1.

ArrayPointer [0] == *((int **)0xffff0018)==(int *)(0xffff0000)== Data1。

ArrayPointer[0][1] == *( ( * (int**) 0xffff0018 ) + 1 ) == (int) * ( (int*)0xffff0000 + 1 ) == (int) * (int*) 0xffff0004 == Data1[1].

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 )

您可能想要查看指针算术:array [N] == *(array + N)

#2


main has to return a type. You forget to put "int" as a return type (implicit int in C++ is banned).

main必须返回一个类型。您忘记将“int”作为返回类型(禁止在C ++中使用隐式int)。

Having said that, i'm not sure what you mean by

话虽如此,我不确定你的意思

// It does not print the data
printf("\nName: %s",  ArrayPointer[index++]);

ArrayPointer[index++] would, as it is defined in the parameter list, return an int. How is that supposed to store a name ? It will store an integer!

ArrayPointer [index ++]将在参数列表中定义,返回一个int。怎么会存储一个名字?它会存储一个整数!

Once again, that said, you can't call that Function (pun intended) with that particular argument. Let's view your types:

再说一次,你说不能用那个特定的参数调用那个函数(双关语)。让我们来看看你的类型:

int Data1[] = {10,10};
int Data2[] = {20,20};
int Data3[] = {30,30};

int *NameList[] = {Data1, Data2, Data3};

Data1      Data2      Data3      NameList
int[2]     int[2]     int[2]     int*[3]

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:

与您所说的相反,NameList不是指向数组数组的指针。我觉得我需要告诉你那将是什么:

int (*NameList)[N][M] = Some3DimensionalArray;

That wouldn't make sense at all. So what do you have?

这根本没有意义。那你有什么?

Data1 = array of 2 int
Data2 = array of 2 int
Data3 = array of 2 int
NameList = array of poiners to int

That is what you got. And you pass NameList to a Function that wants a pointer to an int. It must fail already at the time you call Function in main! I've got no idea what you mean by name 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):

这就是你得到的。然后将NameList传递给想要指向int的指针的Function。当你在main中调用Function时它必须已经失败!我不知道你在Function的那一行中的名字是什么意思。但是如果你想打印存储在指向的数组中的整数(通过指向它们的第一个元素的指针),你可以这样做(保持你的代码尽可能多):

// don't forget the return type, mate. Also see below
void 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. It is not a string, 
             // but an int!
             printf("\nName: %d\n",  ArrayPointer[i][index++]);
          } 
        index=0;         //Counter reset to 0
        // no need to increment the pointer. that's what the indexing by i 
        // above does
        // ArrayPointer++;  
      }
  }

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[] and int*. The first is an incomplete array type, while the second is a complete type (pointer to int):

我一直在讲道人们问问指针和数组之间的区别。编写正确的代码至关重要。我希望我能提供帮助。最后,只是关于int []和int *之间的区别。第一个是不完整的数组类型,而第二个是完整类型(指向int的指针):

typedef int Single[]; // array of indeterminate size.
typedef int *Pointer; // pointer to int

Single s1 = { 1, 2, 3, 4 }; // works!
Pointer s2 = { 1, 2, 3, 4 }; // no no, doesn't work. s2 wants an address

s2's type now has a type different from int[], because you initialized the array which would have incomplete type, the array s1 become complete after defined. It has type of int[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:

s2的类型现在具有与int []不同的类型,因为您初始化了具有不完整类型的数组,数组s1在定义后变为完整。它的类型为int [4]。但是,在参数列表中,存在一个特殊规则,它将导致任何数组类型(甚至完整的数组!)等同于指向其第一个参数的指针。从而:

void f(int *a) <=> void f(int a[]) <=> void f(int a[42]);
void f(int (*a)[42]) <=> void f(int a[][42]) <=> void f(int a[13][42])
// ...

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:

那是因为你不能按值传递数组。编译器滥用它来使数组类型等同于指向其第一个元素的指针。同样处理功能:

void f(int a()) <=> void f(int (*a)())

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.

因为你不能通过值传递函数(呵呵,对我来说根本没有意义),编译器滥用它来使参数列表中的函数类型等效于指向该函数类型的指针。

#3


int Data1[]

has the type of

有类型的

int *

and

int *NameList[]

has the type of

有类型的

int **

You have a two-dimensional array there. Most likely you meant:

那里有一个二维数组。你很可能意味着:

Function(int **ArrayPointer)

On that note, ANSI C/C89/C99 functions have an explicit return type (or void), e.g.

在这方面,ANSI C / C89 / C99函数具有明确的返回类型(或无效),例如,

int main() { ... }
void Function() { ... }

The value pointed to by ArrayPointer is an int, not a string. Thus

ArrayPointer指向的值是int,而不是字符串。从而

printf("\nName: %s",  ArrayPointer[index++]);

Should be written as something else.

应该写成别的东西。

A third thing: index == j in your code. Thus index can be removed in favor of j.

第三件事:代码中的index == j。因此可以删除索引以支持j。

i and j are probably not good variable names here because ArrayPointer is not name describing a list of something.

i和j在这里可能不是很好的变量名,因为ArrayPointer不是描述某事物列表的名称。

If you need more help, please post what you're looking to do, because you code has several bugs (and oddities) in it.

如果您需要更多帮助,请发布您要查找的内容,因为您的代码中存在多个错误(和奇怪的内容)。

#4


Judging by your "answer" (you really should have edited your original post and added this information), you probably want something like this:

根据你的“答案”判断(你真的应该编辑你的原始帖子并添加这些信息),你可能想要这样的东西:

void draw_all_sprites(Sprite *sprites, size_t num_sprites)
{
    Sprite *cur_sprite;
    size_t i;

    for(i = 0; i < num_sprites; ++i)
    {
        draw_sprite(cur_sprite);
        ++cur_sprite;
    }
}

A fairly simple for loop to iterate through elements in an array.

一个相当简单的for循环来迭代数组中的元素。

#5


Paramod,

is there a function you need to call that takes something along the lines of

是否有一个你需要调用的函数,它需要一些东西

void drawSprite(int *data, int rows, int cols)

?

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.

然后,您需要将所有数据放在一块内存中。在您的示例中,编译器为每一行分配一个单独的块,然后为另一个块分配三个指向行的指针。因此,阵列保持在4个不同的位置。

What you need is a multidimensional array rather than array of arrays. Initialize your data like this:

你需要的是一个多维数组而不是数组数组。像这样初始化您的数据:

int data[3,2] = {{10,10},{20,20},{30,30}};

Then you can call your function like this:

然后你可以像这样调用你的函数:

drawSprite(&data[0,0], 3, 2);

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.

使用多维数组将所有元素放在一个内存块中。优点是,您可以将piointer传递给第一个元素,并且知道所有其他元素的位置。缺点 - 所有行都分配了相同的大小。

#6


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.

我在你的程序中做了一些更正,请找到它们并进行比较。我知道答案太迟了。但我今天看到了这一点。

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 < 5; j++)
          {
             //It does not print the data
             printf("\nName: %d\n", *((int *)ArrayPointer[i] + index));
         index++;
          } 
        index=0;         //Counter reset to 0
      }
  }

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

说明:当您将NameList作为指向整数的指针传递给Function时,传递的是数组的第一个元素的地址,恰好是Data1(数组的地址)。但由于此地址以int数组保存,因此它将被视为整数。要使它的行为类似于数组的地址(或者,对于指向int的指针),您需要将其强制转换为(int *)。这就是我所做的:printf(“\ nName:%d \ n”,*((int *)ArrayPointer [i] + index));