簡體   English   中英

雙數組僅存儲整數

[英]Double array only stores ints

我在這里有此代碼,我認為應該可以,而且確實可以! 除了以下事實外,盡管我已經聲明了一個指向double類型數組的指針,但它始終存儲int,而且我不知道為什么。

首先,我有這個結構,它的定義如下:

struct thing {
    int part1;
    double *part2;
};

然后我通過說struct thing *abc = malloc (sizeof(struct thing))初始化物,而part1 = 0part2 = malloc(sizeof(double))

然后,我嘗試在double數組的特定位置設置特定值。 這對於整數很有效,但是當我嘗試0.5時,它將值設置為0。當我嘗試2.9時,它將值設置為2。我真的不知道為什么這樣做。 setValue的代碼如下所示:

struct thing *setValue (struct thing *t, int pos, double set){
    if (t->part1 < pos){ // check to see if array is large enough
        t->part2 = realloc (t->part2, (pos+1) * sizeof(double));
        for (int a = t->part1 + 1; a < pos + 1; a++)
            t->part2[a] = 0;
    t->part1 = pos;
    }
    t->part2[pos] = set; // ALWAYS stores an integer, don't know why
    return t;
}

-編輯:因此,這部分沒有任何實質性的惡意; 但是這只是我的代碼其余部分:

在我的struct上運行的相關功能

#include "thing.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

struct thing *makeThing(){ // GOOD
    struct thing *t = (struct thing *) malloc (sizeof(struct thing));
    t->part1 = 0;
    t->part2 = malloc (sizeof(double));
    t->part2[0] = 0;
    return t;
}

struct thing *setValue (struct thing *t, int pos, double set){
    if (t->part1 < pos){ // check to see if array is large enough
        t->part2 = realloc (t->part2, (pos+1) * sizeof(double));
        for (int a = t->part1 + 1; a < pos + 1; a++)
            t->part2[a] = 0;
    t->part1 = pos;
    }
    t->part2[pos] = set; // ALWAYS stores an integer, don't know why
    return t;
}

double getValue (struct thing *t, int pos){
    if (pos <= t->part1){
        return t->part2[pos];
    }
    return 0;
}

頭文件:

#ifndef THING_H
#define THING_H

struct thing {
    int part1;
    double *part2;
};

struct thing *makeThing();
struct thing *setValue (struct thing *t, int pos, double set);
double getValue (struct thing *t, int pos);

#endif

主文件:

#include <stdio.h>
#include "thing.h"

int main (void)
{
    struct thing *t = makeThing();
    setValue (t, 1, -1);
    setValue (t, 1, -2);
    setValue (t, 10, 1);
    setValue (t, 3, 1.5);

    printf ("%g\n", getValue (t, 0));
    printf ("%g\n", getValue (t, 1));
    printf ("%g\n", getValue (t, 2));
    printf ("%g\n", getValue (t, 3));
    printf ("%g\n", getValue (t, 4));
    printf ("%g\n", getValue (t, 5));
    printf ("%g\n", getValue (t, 6));
    printf ("%g\n", getValue (t, 7));
    printf ("%g\n", getValue (t, 8));
    printf ("%g\n", getValue (t, 9));
    printf ("%g\n", getValue (t, 10));

    return 0;
}

在我的計算機上,此打印輸出:0 -2 0 1 0 0 0 0 0 0 1

編輯:原來,當我通過代碼塊編譯它時,它可以工作...

最終,我感到困惑。

在C中將double轉換為int嗎?

不,不是。 當您為一個類型為double的對象分配一個double值時,則不會進行任何轉換。

您的問題不在顯示的代碼中; 它在其他地方(您的打印方式,一些愚蠢的#define以及其他東西)。

哦! 而且,您確實應該確保realloc()正常工作。 否則,用戶可能會得到一個稍微錯誤的值,而不是一個錯誤...

正如我在評論中所說, 您的代碼按ideone的預期運行

我認為您可能在打印中弄亂了格式說明符。 對於正確的數據類型,請確保具有正確的說明符。

我知道有時在我更改Objective-C代碼中的數據類型時會發生這種情況。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM