传递指针,通过功能数组的数组

问题描述:

有一个在code数组即名称列表指针到一个数组。我希望每个在指针(NameList中)在阵列中的内容得到印一个接一个。下面code是无法做到的任务。 PLS。帮助。

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

这是问题的楼主另注:

我已经完成了在Turbo C的吃豆子游戏,我抛光一些图形程序,因此它可以被再次轻易重用。这仅是为了帮助的目的和理解概念创建一个小样本。在code所有的数据实际上是精灵字符数组。现在我只是想调用传递指针,以便在指针阵列各被吸引到屏幕的功能。如何解决这个code进行修改,来处理呢?我其实卡住了这里。

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.

该死的 litb 的,再次你打我被仅仅几分钟一拳。 (如果我没有孩子谁一直醒来......)

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:


  • 阵列,比如 int类型的[4] 的分配内存空间,他们的数据。

  • 指针,如为int * P 的分配仅enouch内存空间​​,一个指向内存中的另一个位置。

  • 这就是为什么我们可以使用的的sizeof 的对数组并获得全面的内存占用,但不能在三分球。

  • Arrays, such as int a[4] allocate memory space for their data.
  • Pointers, such as int * p allocate just enouch memory space for a pointer to another spot in memory.
  • That's why we can use sizeof on arrays and get the full memory footprint, but not on pointers.

除此之外区别不大,是不是真的有INT []和INT之间有很大的区别*。 (想想有多少人声明*主(INT ARGC,字符** argv的)VS的主(INT ARGC,CHAR *的argv [])的。)

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 Data1[] = {10,11};
int Data2[] = {20,22};
int Data3[] = {30,33};

我们现在拥有的记忆3个街区。你说:

We now have 3 blocks of memory. Say:

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)

其中:

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

后,我的不可以打算进入大端VS这里little-endian字节顺序!

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

是,在这种情况下,数据1 [2] == Data2的[0]。但是你不能依靠你的编译器铺设的事情了记忆我已经奠定了他们在这里以同样的方式。

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.

下一页:

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)

其中:

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

注意NameList中的 INT ** 的类型,和不是int * 键入!

我们可以接着写:

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解析,与(int **)0xffff0018。

ArrayPointer resolves to (int**)0xffff0018.

ArrayPointer [0] == *((INT **)0xffff0018)==(INT *)(为0xffff0000)==数据1。

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

ArrayPointer [0] [1] == *((*(INT **)0xffff0018)+1)==(INT)*(为(int *)为0xffff0000 + 1)==(INT)*(中间体* )0xffff0004 ==数据1 [1]。

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


您可能需要检查指针算法:数组[N] == *(阵列+ N)

You may want to review pointer arithmetic: array[N] == *( array + N )