Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 分配多维数组和不兼容类型_C_Pointers_Multidimensional Array_Perlin Noise - Fatal编程技术网

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

C 分配多维数组和不兼容类型,c,pointers,multidimensional-array,perlin-noise,C,Pointers,Multidimensional Array,Perlin Noise,我一直在尝试编写一个函数,该函数将输出0到1之间的值,这将反过来用于生成柏林噪声。每当我试图编译代码时,我都会得到这样的结果:错误:当从类型“float*”分配给类型“float[(sizetype)(width)][(sizetype)(height)]”时,类型不兼容。 我一直在尝试将多维函数的指针传递给函数,以便可以用值填充它。我已经读到,在传递数组之前,必须首先分配数组的内存。我还读到,您可以在函数声明中使用双指针 不过,我不知道怎么了。顺便说一句,我正在使用命令gcc-Wall fil

我一直在尝试编写一个函数,该函数将输出0到1之间的值,这将反过来用于生成柏林噪声。每当我试图编译代码时,我都会得到这样的结果:
错误:当从类型“float*”分配给类型“float[(sizetype)(width)][(sizetype)(height)]”时,类型不兼容。

我一直在尝试将多维函数的指针传递给函数,以便可以用值填充它。我已经读到,在传递数组之前,必须首先分配数组的内存。我还读到,您可以在函数声明中使用双指针

不过,我不知道怎么了。顺便说一句,我正在使用命令
gcc-Wall file.c-o file
来编译它

编辑:很抱歉没有说清楚。我试图将二维数组“smoothNoise”中的值分配给三维数组“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;
 }
#包括
#包括
/*这是一个用于确定数组大小的宏*/
#定义数组大小(x)(sizeof(x)/sizeof(x[0]))
/*现在来看一些简单的线性插值*/
浮点插值(浮点x0、浮点x1、浮点alpha)
{
返回x0*(1-α)+α*x1;
}
/*
*产生柏林噪声的函数*/
void-GeneratePerlinNoise(浮点**perlinNoise,int-width,int-height,int-octave){
/*初始化数组*/
浮动噪音[宽度][高度];
int i,j;
对于(i=0;i
据我所知,这项任务是罪魁祸首

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];
}

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];
}
smoothNoise
是一个
width×height
数组,
smoothNoise[i]
是一个
float[height]
(然后转换为指向其初始元素的指针),因此存在维度不匹配,我不知道您打算在这里做什么,因此我无法建议修复。

\35;
#include<stdlib.h>
您必须包含它,它将删除其他警告 关于阵列,我不知道是否可以从3D阵列复制到2D阵列
所以我无能为力。

您在分配
leNoise
时混淆了大小,应该是
float**leNoise=malloc(500*sizeof(float*))-或更好的
500*sizeof*leNoise
,以及
leNoise[i]=malloc(500*sizeof(float))-或更好,
leNoise[i]=malloc(500*sizeof*leNoise[i])。我将尝试
float**leNoise=malloc(500*sizeof(float*))选项。感谢您的帮助,VoidPointer。我正在尝试将“smoothNoise”中的所有值复制到“leNoise”。因为“smoothNoise”是一个三维数组,所以我在复制数据时遇到了问题。我看看这是否有效。
float smoothNoise[width][height];
#include<stdlib.h>