简体   繁体   English

无法将 int 指针数组转换为 function 中的双指针数组

[英]Can't convert int pointer array to double pointer array in a function

I have a function that is supposed to convert an int pointer array to a double pointer array.我有一个 function 应该将 int 指针数组转换为双指针数组。 It looks like this:它看起来像这样:

double* intToDouble(int len , int*x){

    double* y;
    y =(double*)malloc(len*sizeof(double));
    for (int i=0; i<len ; i++){
        y[i]=(double)x[i];
    }
    return y;
}

However, when trying to use it, I get an array full of float 0's.但是,当尝试使用它时,我得到一个充满浮点 0 的数组。 I will also leave here my print array function in case it is relevant.我还将在这里留下我的打印数组 function 以防万一。

void printDoubleSignal(int len, double* x) {
    printf("%d: [", len);
    if (len > 0) {
        printf("%lf", x[0]);
        for (int i = 1; i < len; i++) printf(",%lf", x[i]);
    }
    printf("]\n");
}

This is the result that I get after trying to convert from int to float the array 10: [1,2,3,4,5,0,0,0,0,0]这是我尝试从 int 转换为浮点数组 10 后得到的结果:[1,2,3,4,5,0,0,0,0,0]

10: [0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000]

EDIT As requested, here is the rest of the code:编辑根据要求,这里是代码的 rest:

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

int* readSignal(int* len) {
    int* x;
    char c;
    scanf("%d:", len);
    x = calloc(*len, sizeof(int));
    do c = getchar(); while (c != '[');
    if (*len > 0) {
        scanf("%d", &x[0]);
        for (int i = 1; i < *len; i++) scanf(",%d", &x[i]);
    }
    do c = getchar(); while (c != ']');
    return x;
}

void printSignal(int len, int* x) {
    printf("%d: [", len);
    if (len > 0) {
        printf("%d", x[0]);
        for (int i = 1; i < len; i++) printf(",%d", x[i]);
    }
    printf("]\n");
}

void printDoubleSignal(int len, double* x) {
    printf("%d: [", len);
    if (len > 0) {
        printf("%lf", x[0]);
        for (int i = 1; i < len; i++) printf(",%lf", x[i]);
    }
    printf("]\n");
}

int* zeroPad(int len, int* x, int n){

    int i;
    x = (int*)realloc(x, sizeof(int) * n);
    for (i=len; i<n ; i++){
        x[i]=0;
    }
    return x;
}

double* intToDouble(int len , int* x){

    double *y;
    y =(double*)malloc(len*sizeof(double));
    for (int i=0; i<len ; i++){
        printf("x[%d]=%lf\n",i, x[i]);
        y[i]=x[i];
        printf("y[%d]=%lf\n",i, y[i]);
    }
    return y;
}

int main()
{
    int *x , len=5;
    double *y;
    x = readSignal(&len);
    printSignal(len,x);
    x = zeroPad(&len,x,10);
    printSignal(len,x);
    y = intToDouble(len,x);
    printDoubleSignal(len ,y);

    return 0;
}

EDIT 2 This is the exact text of the input/output (first line is input, the rest is output):编辑 2这是输入/输出的确切文本(第一行是输入,rest 是输出):

5:[1,2,3,4,5]
5: [1,2,3,4,5]
10: [1,2,3,4,5,0,0,0,0,0]
x[0]=-0.000000
y[0]=-0.000000
x[1]=-0.000000
y[1]=-0.000000
x[2]=-0.000000
y[2]=-0.000000
x[3]=-0.000000
y[3]=-0.000000
x[4]=-0.000000
y[4]=-0.000000
x[5]=-0.000000
y[5]=-0.000000
x[6]=-0.000000
y[6]=-0.000000
x[7]=-0.000000
y[7]=-0.000000
x[8]=-0.000000
y[8]=-0.000000
x[9]=-0.000000
y[9]=-0.000000
10: [0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000]

You forgot to receive the result of zeroPad .您忘记接收zeroPad的结果。 The function uses realloc() , so the passed pointer may be invalidated. function 使用realloc() ,因此传递的指针可能无效。 The function returns the new pointer, so you have to assign that to x . function 返回新指针,因此您必须将其分配给x

This means that the line in the main() function这意味着main()中的行 function

    zeroPad(5,x,10);

should be应该

    x = zeroPad(5,x,10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM