分配多维数组和不兼容的类型

时间:2013-05-08 20:48:40

标签: c pointers multidimensional-array perlin-noise

我一直在努力编写一个函数,它将输出0到1之间的值,而这些函数又将用于生成Perlin噪声。每当我尝试编译代码时,我都会得到:error: incompatible types when assigning to type ‘float[(sizetype)(width)][(sizetype)(height)]’ from type ‘float *’

我一直在尝试将多维指针传递给函数,以便可以使用值填充它。我已经读过你必须首先分配数组的内存然后再传递它。另外我读过你可以在函数声明中使用双指针。

但是,我不知道出了什么问题。顺便说一下,我正在使用命令gcc -Wall file.c -o file来编译它。

修改:抱歉没有那么清楚。我正在尝试将2-D数组“smoothNoise”中的值分配给3-D数组“leNoise”。这就是问题出现的地方。

#include <math.h>
#include <stdio.h> 

/* This is a macro for determining the size of an array */
#define ARRAY_SIZE(x)  (sizeof(x) / sizeof(x[0]))

/* Now for some simple linear interpolation */
float Interpolate(float x0, float x1, float alpha)
{
   return x0 * (1 - alpha) + alpha * x1;
}

/*
 * Functions for generating Perlin Noise. */

void GeneratePerlinNoise(float **perlinNoise, int width, int height, int octave) {

  /* Initialize the array */
    float noise[width][height];

    int i,j;

    for (i = 0; i < width; i++)
   {
    for (j = 0; j < height; j++)
       {
        /* Return a random number between 0 and 1.
     * This code may not work.
         */
    noise[i][j] = random();
    }
 }

 float smoothNoise[width][height];

 int samplePeriod = 1 << octave; /* calculates 2 ^ k */
 float sampleFrequency = 1.0f / samplePeriod;

 for (i = 0; i < width; i++)
{
  /* calculate the horizontal sampling indices */
  int sample_i0 = (i / samplePeriod) * samplePeriod;
  int sample_i1 = (sample_i0 + samplePeriod) % width; /* wrap around */
  float horizontal_blend = (i - sample_i0) * sampleFrequency;

  for (j = 0; j < height; j++)
  {
     /* calculate the vertical sampling indices */
     int sample_j0 = (j / samplePeriod) * samplePeriod;
     int sample_j1 = (sample_j0 + samplePeriod) % height; /* wrap around */
     float vertical_blend = (j - sample_j0) * sampleFrequency;

     /* blend the top two corners */
     float top = Interpolate(noise[sample_i0][sample_j0],
        noise[sample_i1][sample_j0], horizontal_blend);

     /*blend the bottom two corners */
     float bottom = Interpolate(noise[sample_i0][sample_j1],
        noise[sample_i1][sample_j1], horizontal_blend);

     /* final blend */
     smoothNoise[i][j] = Interpolate(top, bottom, vertical_blend);
   }
}

float leNoise[octave][width][height];

float persistance = 0.5f;

for (i = 0; i < octave; i++)
{
   /* Big problem here: moving data from a two-dimensional array
* to a three-dimensional array */
    leNoise[i] = smoothNoise[i];
}

 float amplitude = 1.0f;
 float totalAmplitude = 0.0f;

 /*blend noise together */
 int octaveCount;
 for (octaveCount = octave - 1; octaveCount >= 0; octave--)
 {
    amplitude *= persistance;
    totalAmplitude += amplitude;

    for (i = 0; i < width; i++)
    {
       for (j = 0; j < height; j++)
       {
          perlinNoise[i][j] += leNoise[octave][i][j] * amplitude;
       }
    }
  }

  /*normalisation */
  for (i = 0; i < width; i++)
 {
   for (j = 0; j < height; j++)
    {
      perlinNoise[i][j] /= totalAmplitude;
    }
  }
}

  int main()
{
/* These are some variables which will be used in the 
 * Perlin Noise functions.  These may not work.
 */
   /* Make some space in the RAM for our big array. */
   float **leNoise = malloc( 500 * sizeof( float ) );
   int i;

   for (i=0; i < 500; i++) {
 leNoise[i] = malloc(500 * sizeof(float *) );
   }

   GeneratePerlinNoise(leNoise, 500, 500, 7); 

   return 0;
 }

3 个答案:

答案 0 :(得分:1)

据我所知,这项任务是罪魁祸首。

for (i = 0; i < octave; i++)
{
   /* Big problem here: moving data from a two-dimensional array to a three-dimensional array */
   leNoise[i] = smoothNoise[i];
}

因为 C没有任何称为数组到数组的复制。您应该通过仔细循环数组手动将单个项目从源复制到目标。

Ex, leNoise[x][y][z] = smoothNoise[i][j]

答案 1 :(得分:1)

clang的错误信息更好:

error: array type 'float [width][height]' is not assignable

数组不可分配。你已宣布

float leNoise[octave][width][height];

然后尝试分配二维组件数组:

for (i = 0; i < octave; i++)
{
   /* Big problem here: moving data from a two-dimensional array
* to a three-dimensional array */
    leNoise[i] = smoothNoise[i];
}

由于数组不可分配,因此您必须在此处[直接或使用memcpy]逐个复制值。

但是,

float smoothNoise[width][height];

smoothNoisewidth×height数组,smoothNoise[i]float[height](然后转换为指向其初始元素的指针),因此您的维度不匹配,我不知道你打算在这里做什么,所以我无法提出修复建议。

答案 2 :(得分:0)

#include<stdlib.h>

您必须包含它,它将删除其他警告 关于数组,我不知道这样的副本是否可以从3D到2D阵列 所以无法帮助。