【问题标题】:Initialize multiple arrays in function C programming在函数 C 编程中初始化多个数组
【发布时间】:2015-07-27 16:14:45
【问题描述】:

我正在尝试用 C 编写一个函数来初始化多个具有不同大小的双精度类型数组。数组大小应在函数中给出,数组值通过先前定义的函数和值分配。 这些功能将被设置为无效,因为我以后应该在不同的地方多次使用它们。这是一个没有给出合理结果的代码版本。感谢您的帮助!

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

//interpolation, source code from internet

int findCrossOver(double arr[], int low, int high, double x)
{
  // Base cases
  if (arr[high] <= x) // x is greater than all    
    return high;
  if (arr[low] > x)  // x is smaller than all
    return low;

  // Find the middle point
  int mid = (low + high)/2;  /* low + (high - low)/2 */

  /* If x is same as middle element, then return mid */
  if (arr[mid] <= x && arr[mid+1] > x)
    return mid;

  /* If x is greater than arr[mid], then either arr[mid + 1]
    is ceiling of x or ceiling lies in arr[mid+1...high] */
  if(arr[mid] < x)
      return findCrossOver(arr, mid+1, high, x);

  return findCrossOver(arr, low, mid-1, x);
}

void interp1(double xp[], double yp[], int xyplen, double x[], int xlen,double* y)
{
int index;
int i;
for (i=0; i<xlen; i++)
{
    index = findCrossOver(xp, 0, xyplen-1, x[i]);
    if(x[i] > xp[xyplen-1])
    {
        index--;
    }
    y[i] = ((x[i]-xp[index])*(yp[index+1]-yp[index]))/(xp[index+1]-xp[index]) + yp[index];
}
}

void fpr(double step, double* oldVal, double* xp, double* yp, double* x)
{
   int i;
   int xyplen = 5; //array sizes through many calculations inside the function
   int xylen = 10;  

   for(i=0; i<xyplen; i++)
   {
        xp[i] = i+1;
        yp[i] = 2*xp[i];
        printf("%f\n", xp [i]);
   }
    for (i=0; i<xylen; i++)
    {
        x[i] = i+0.5;
    }
   interp1(xp, yp, xyplen, x, xylen,oldVal);
}


int main()
{
    double *someVal=malloc(sizeof(double));
    double *yp=malloc(sizeof(double));
    double *xp=malloc(sizeof(double));
    double *x=malloc(sizeof(double));
    fpr(1.1,someVal, xp, yp, x); 
    printf("%d\n", sizeof(xp)/sizeof(xp[0]));
    int i;
    printf("Input Data\n");
    for (i=0; i<5; i++)
    {
    printf("(%.1f, %.1f)\t", xp[i], yp[i]);
    }
    printf("Interpolated Data\n");
    for (i=0; i<10; i++)
    {
    printf("(%.1f, %.1f)\t",x[i],someVal[i]);
    }

    return 0;
}

这是输出(由于 printf 命令):

1.000000
2.000000
3.000000
4.000000
5.000000
0
Input Data
(-14.5, 0.5)    (-22.0, -7.0)   (90.5, -14.5)   (653.0, -22.0)  (89.5, 90.5)
Interpolated Data
(90.5, -3.5)    (653.0, -2.5)   (89.5, 0.5) (-9.1, -7.0)    (4.5, -14.5)    (5.5, -22.0)    (6.5, 90.5) (7.5, 653.0)    (8.5, 89.5) (9.5, -9.1) 

如您所见,sizeof 只是给出了第一个元素的地址,而 main 中的输出并不像预期的那样。

【问题讨论】:

  • 这个肯定有很多重复,但简短的回答是,当你将一个数组传递给一个函数时,它衰减到一个指针,所以当你这样做时sizeof在函数参数上,您获得指针的大小,而不是它指向的数组。并不是说你有数组,你只有指向 single value. 的指针

标签: c arrays function pointers initialization


【解决方案1】:

xp 和 yp 是代码中指向 double 的指针,而不是数组。

使用类似的东西:

double *xp = malloc(sizeof(double) * num_of_elements0);
double *yp = malloc(sizeof(double) * num_of_elements1);

并且总是检查空指针。

if(xp != NULL && yp != NULL)
{
    /* Your code here */
}

我不明白你想如何初始化你的数组。随机数?零?

【讨论】:

  • 感谢您的回答。我不能使用malloc(sizeof(double) * num_of_elements0),因为要在初始化函数中定义 num_elements(或大小)。我只需要一个函数来获取许多未知大小的数组并定义它们的大小和元素。我只是无法用数组弄清楚,所以我开始使用指针。
【解决方案2】:

初始化多个具有不同大小的双精度类型数组。

您显示的代码中没有数组,只有指向内存的指针。

sizeof(xp) 返回指针 xp 的大小,无论指针指向多少内存。

这一行

double *xp=malloc(sizeof(double));

xp 定义为指向double 的指针,并将其初始化为指向内存以准确存储one double。

someValypx 相同。

通过使用超出索引0 的内存索引,代码会调用未定义的行为。

【讨论】:

  • 我尝试了类似的方法:void fpr(double step, double* oldVal, double* xp, double* yp, double* x) { ..... x=malloc(sizeof(double) * xylen); // interp1(xp, yp, xyplen, x, xylen,oldVal); } ,主要定义了double *x;。虽然它在指针是全局的时有效
  • @MRM:关于如何在您喜欢阅读的函数中分配数组:stackoverflow.com/q/4179273/694576
  • 基本上我只想拥有本地数组(在 main 中),它们只用函数 initialize() 定义一次,然后在 main 和其他函数中使用。我希望我已经说清楚了。另一件事是我要返回多个数组。
  • @MDM:然后调用malloc(),正如您在上面的评论中所显示的那样,在main() 而不是fpr()。并为您需要的每个“数组”执行此操作,即为四个指针中的每一个分配尽可能多的内存。
  • 所以这里有一个混淆。这里的问题是数组大小首先在fpr() 中给出,然后通过该大小分配数组。这就是想法。我的问题是,我如何在不知道 main 大小的情况下执行malloc()
猜你喜欢
  • 2018-05-05
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 2015-04-07
  • 1970-01-01
相关资源
最近更新 更多