二维数组指针表示法适用于一个版本而不是其他的......我想 [英] Two dimensional array pointer notation works for one version but not the other...I think

查看:203
本文介绍了二维数组指针表示法适用于一个版本而不是其他的......我想的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用下面的函数定义为一个32位版本的一切工作得很好:

When I used the following function definition for a 32 bit version everything worked fine:

void intensifyPixel(int x, int y, double distance, unsigned long *grid[framebufferheight][framebufferwidth], unsigned long color){
    unsigned long intensity; 
    //assign intensity returned from filter function 
    intensity = filter ((floor(fabs (distance)) + .5), color);
    //write intensity value to x,y coordinates of framebuffer memory    
    *( *(grid + y) + x) = intensity;
}

现在我想用同样的code除了无符号短,而不是unsigned long类型数组

Now I am trying to implement a 16 bit version using the same code except unsigned short instead of unsigned long for the array

void intensifyPixel(int x, int y, double distance, unsigned short *grid[framebufferheight][framebufferwidth], unsigned short color){
    unsigned short intensity; 
    //assign intensity returned from filter function 
    intensity = filter ((floor(fabs (distance)) + .5), color);
    //write intensity value to x,y coordinates of framebuffer memory    
    *( *(grid + y) + x) = intensity;
}

我已经相应改变过滤函数的返回类型。

I've changed the return type of the filter function accordingly.

下面是对code,工程(32位版本)的链接:的http:// codepad.org / GsK00pd5

Here is a link to the code that works (32 bit version): http://codepad.org/GsK00pd5

下面是对code不正常工作的链接(16位版本:的http:/ /$c$cpad.org/6eAeTful

Here is a link to the code that does not work properly (16 bit version: http://codepad.org/6eAeTful

有很多在那里。这真的很难,我来缩小这个问题了,我为此道歉。

There is a lot there. It's really hard for me to narrow this problem down, and I apologize for that.

我希望你可以看到什么,我不能。

I hope one of you can see what I cannot.

有可能是值得一提的是,这里的要点是将指针传递到一个二维阵列的intensifyPixel功能

It may be worth mentioning that the point here is to pass a pointer to a two dimensional array to the intensifyPixel function.

感谢您的帮助,您可以提供。

Thank you for any help you can provide.

推荐答案

定义为一个参数无符号长*电网[framebufferheight] [framebufferwidth] 电网

framebufferheight framebufferwidth 指针数组的数组无符号长

an array of framebufferheight arrays of framebufferwidth pointers to unsigned long, IT IS

指针 framebufferwidth 指针数组无符号长

这是因为,在C,数组从未如以其整体参数传递。 ,而不是传递一个数组,指针,它的第一个元素传递,尽管语法,什么不是。在我说的电网是,是不是再看看。这就是本质。

That is because, in C, arrays are never passed as parameters in their entirety. Instead of an array being passed, the pointer to its first element is passed, despite the syntax and what not. Look again at what I said grid is and is not. That's the essence.

现在, *(*(网格+ Y)+ X)或等价地,电网[Y] [X] 解析为一个指向无符号长

Now, *( *(grid + y) + x) or, equivalently, grid[y][x] resolves to a pointer to unsigned long.

指定的指针整数不太可能你想要什么。

Assigning an integer to a pointer is unlikely what you want.

您可能要重新定义电网参数是这样的:

You probably want to redefine the grid parameter this way:

unsigned long grid[framebufferheight][framebufferwidth]

或等价是这样的:

unsigned long (*grid)[framebufferwidth]

所以电网[Y] [X] 将解析为无符号长,而不是一个指针之一。

so grid[y][x] would resolve to unsigned long and not a pointer to one.

函数之外的帧缓冲器可以被分配或静态

The frame buffer outside of the function can be allocated either statically:

// 2-d array of unsigned longs
unsigned long grid[framebufferheight][framebufferwidth];

或动态:

// pointer to an array of framebufferwidth unsigned longs
unsigned long (*grid)[framebufferwidth] = malloc(sizeof(unsigned long) * framebufferheight * framebufferwidth);

您把它当作是 intensifyPixel() intensifyPixel(...,电网,...)并以同样的方式访问:电网[Y] [X] =东西;

You pass it as is to intensifyPixel(): intensifyPixel(..., grid, ...) and access in the same manner: grid[y][x] = something;

更新:如果我不顾一切试图解释它,你仍然没有得到它的权利或被遗忘的东西,看到一个更完整的例子:

UPDATE: If despite all my attempts to explain it you still don't get it right or are forgetting something, see a more complete example:

#include <stdio.h>
#include <stdlib.h>

#define framebufferheight 20
#define framebufferwidth  64

void visualize(unsigned long (*grid)[framebufferwidth])
{
  int y, x;
  for (y = 0; y < framebufferheight; y++)
  {
    for (x = 0; x < framebufferwidth; x++)
      printf("%c", (char)grid[y][x]);
    printf("\n");
  }
}

void drawBox(unsigned long (*grid)[framebufferwidth],
             int x1, int y1, int x2, int y2, char c)
{
  int y, x;
  for (y = y1; y <= y2 ; y++)
    for (x = x1; x <= x2; x++)
      grid[y][x] = c;
}

void clear(unsigned long (*grid)[framebufferwidth], char c)
{
  drawBox(grid, 0, 0, framebufferwidth - 1, framebufferheight - 1, c);
}

int main(void)
{
  {
    unsigned long (*grid)[framebufferwidth] = malloc(sizeof(unsigned long) * framebufferheight * framebufferwidth);
    clear(grid, '.');
    drawBox(grid, 1, 1, 10, 5, '1');
    drawBox(grid, 40, 10, 55, 18, '2');
    grid[framebufferheight / 2][framebufferwidth / 2] = '+';
    visualize(grid);
  }

  {
    unsigned long grid[framebufferheight][framebufferwidth];
    clear(grid, ',');
    drawBox(grid, 1, 1, 10, 5, 'a');
    drawBox(grid, 40, 10, 55, 18, 'b');
    grid[framebufferheight / 2][framebufferwidth / 2] = '*';
    visualize(grid);
  }

  return 0;
}

输出( ideone ):

................................................................
.1111111111.....................................................
.1111111111.....................................................
.1111111111.....................................................
.1111111111.....................................................
.1111111111.....................................................
................................................................
................................................................
................................................................
................................................................
................................+.......2222222222222222........
........................................2222222222222222........
........................................2222222222222222........
........................................2222222222222222........
........................................2222222222222222........
........................................2222222222222222........
........................................2222222222222222........
........................................2222222222222222........
........................................2222222222222222........
................................................................
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,aaaaaaaaaa,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,aaaaaaaaaa,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,aaaaaaaaaa,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,aaaaaaaaaa,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,aaaaaaaaaa,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*,,,,,,,bbbbbbbbbbbbbbbb,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,bbbbbbbbbbbbbbbb,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,bbbbbbbbbbbbbbbb,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,bbbbbbbbbbbbbbbb,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,bbbbbbbbbbbbbbbb,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,bbbbbbbbbbbbbbbb,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,bbbbbbbbbbbbbbbb,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,bbbbbbbbbbbbbbbb,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,bbbbbbbbbbbbbbbb,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

这篇关于二维数组指针表示法适用于一个版本而不是其他的......我想的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆