数据中嵌入多种类型

发布于 2024-12-16 15:07:34 字数 1394 浏览 0 评论 0原文

是否可以在一个数据变量中存储多种数据类型,例如 char *?

拿这个例子来说,它打印 val1(整数)和 val3 到 val5(字符),但为第二个整数打印 0,为浮点数打印 0.00。

有关如何执行此操作的任何线索?

任何帮助表示赞赏。

#include <iostream>

static void printData(char *what) {

    int val1, val2, counter = 0;
    char val3, val4, val5;
    float val6;

    val1 = *((int *)what+counter);
    counter += sizeof(int);
    val2 = *((int *)what+counter);
    counter += sizeof(int);
    val3 = *((char *)what+counter);
    counter += sizeof(char);
    val4 = *((char *)what+counter);
    counter += sizeof(char);
    val5 = *((char *)what+counter);
    counter += sizeof(char);
    val6 = *((float *)what+counter);

    printf("val1 = %d, val2 = %d, val3-5 = %c%c%c, val6 = %.2f", val1, val2, val3, val4, val5, val6);

}

int main (int argc, const char *argv[]) {

    char *data = (char *)malloc((sizeof(int) * 2) + (sizeof(char) * 3) + sizeof(float));

    int integer = 4, secondInteger = 56;
    char test[3] = { 't', 'e', 's' };
    float floatValue = 3.14f;

    int counter = 0;

    *(data) = integer;
    counter += sizeof(int);
    *(data + counter) = secondInteger;
    counter += sizeof(int);
    *(data + counter) = test[0];
    counter += 1;
    *(data + counter) = test[1];
    counter += 1;
    *(data + counter) = test[2];
    counter += 1;
    *(data + counter) = floatValue;

    printData(data);

    return 0;
}

Is it possible to store multiple data types in a data variable, for instance char *?

Take this example, it prints val1 (an integer), and val3 to val5 (chars), but prints 0 for the second integer, and 0.00 for the float.

Any clues as how to do this?

Any help appreciated.

#include <iostream>

static void printData(char *what) {

    int val1, val2, counter = 0;
    char val3, val4, val5;
    float val6;

    val1 = *((int *)what+counter);
    counter += sizeof(int);
    val2 = *((int *)what+counter);
    counter += sizeof(int);
    val3 = *((char *)what+counter);
    counter += sizeof(char);
    val4 = *((char *)what+counter);
    counter += sizeof(char);
    val5 = *((char *)what+counter);
    counter += sizeof(char);
    val6 = *((float *)what+counter);

    printf("val1 = %d, val2 = %d, val3-5 = %c%c%c, val6 = %.2f", val1, val2, val3, val4, val5, val6);

}

int main (int argc, const char *argv[]) {

    char *data = (char *)malloc((sizeof(int) * 2) + (sizeof(char) * 3) + sizeof(float));

    int integer = 4, secondInteger = 56;
    char test[3] = { 't', 'e', 's' };
    float floatValue = 3.14f;

    int counter = 0;

    *(data) = integer;
    counter += sizeof(int);
    *(data + counter) = secondInteger;
    counter += sizeof(int);
    *(data + counter) = test[0];
    counter += 1;
    *(data + counter) = test[1];
    counter += 1;
    *(data + counter) = test[2];
    counter += 1;
    *(data + counter) = floatValue;

    printData(data);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱倦旧时光 2024-12-23 15:07:34

嗯,malloc 怪癖。当我在我的机器上尝试这个时,我得到了和你一样的结果。但奇怪的是,当我将转换移到指针增量之外时,我得到了 SecondInteger (56) 的正确值,但 floatValue 的值仍然是 0.00。不太确定如何解释这种行为,但我敢打赌这与字节对齐有关。我需要再考虑一下。

#include <iostream>

static void printData(char *what) {

    int val1, val2, counter = 0;
    char val3, val4, val5;
    float val6;

    val1 = *(int*)(what+counter);
    counter += sizeof(int);
    val2 = *(int*)(what+counter);
    counter += sizeof(int);
    val3 = *(char*)(what+counter);
    counter += sizeof(char);
    val4 = *(char*)(what+counter);
    counter += sizeof(char);
    val5 = *(char*)(what+counter);
    counter += sizeof(char);
    val6 = *(float*)(what+counter);

    printf("val1 = %d, val2 = %d, val3-5 = %c%c%c, val6 = %.2f", val1, val2, val3, val4, val5, val6);

}

像 aib 一样,我会说使用结构,但由于您使用的是 malloc,您可能已经知道使用结构,并且只是问这个以了解它为何表现出这种行为:)

Hmmm malloc quirks. I get the same thing as you when I try this on my machine. Oddly though, when I move the casting outside the pointer increment, I get the correct value for secondInteger (56) but still 0.00 for floatValue. Not really sure what explains this behavior, but I bet it has something to do with byte alignment. I'll need to think about it more.

#include <iostream>

static void printData(char *what) {

    int val1, val2, counter = 0;
    char val3, val4, val5;
    float val6;

    val1 = *(int*)(what+counter);
    counter += sizeof(int);
    val2 = *(int*)(what+counter);
    counter += sizeof(int);
    val3 = *(char*)(what+counter);
    counter += sizeof(char);
    val4 = *(char*)(what+counter);
    counter += sizeof(char);
    val5 = *(char*)(what+counter);
    counter += sizeof(char);
    val6 = *(float*)(what+counter);

    printf("val1 = %d, val2 = %d, val3-5 = %c%c%c, val6 = %.2f", val1, val2, val3, val4, val5, val6);

}

Like aib, I'd say to use structs, but since you're using malloc, you probably know already to use structs and are just asking this to see why it exhibits this behavior :)

ゞ花落谁相伴 2024-12-23 15:07:34

是的,但是使用结构要容易得多。

你的例子似乎在我的编译器上运行,但我懒得校对它是否符合标准(未定义的行为等),这是你应该使用 struct 的另一个原因:)。

Yeah, but it's much easier to use a struct.

And your example seems to be working on my compiler, but I'm too lazy to proofread it for standards compliance (undefined behavior, etc.) That's another reason why you should use a struct :).

尝蛊 2024-12-23 15:07:34

由于它被标记为 C++,所以我建议您了解类。以下是开始将代码放入中的一种方法:

#include <iostream>
#include <iomanip>
#include <string>

class data
{
private:
    int first_int_;
    int second_int_;
    std::string chars_;
    float float_value_;

public:
    data(int first_int, int second_int, const std::string& chars, float float_value)
        :first_int_(first_int)
        ,second_int_(second_int)
        ,chars_(chars)
        ,float_value_(float_value)
    {
    }

    void print(std::ostream& os) const
    {
        os << "val1 = "
            << first_int_
            << ", val2 = "
            << second_int_
            << ", val3-5 = "
            << chars_
            << ", val6 = "
            << std::setprecision(3)
            << float_value_
            << std::endl;
    }
};

int main ()
{
    data d(4, 56, "tes", 3.14f);
    d.print(std::cout);
    return 0;
}

查看它们如何运行!

Since this is tagged C++, I suggest you learn about classes. Here is one way to start putting your code in a class:

#include <iostream>
#include <iomanip>
#include <string>

class data
{
private:
    int first_int_;
    int second_int_;
    std::string chars_;
    float float_value_;

public:
    data(int first_int, int second_int, const std::string& chars, float float_value)
        :first_int_(first_int)
        ,second_int_(second_int)
        ,chars_(chars)
        ,float_value_(float_value)
    {
    }

    void print(std::ostream& os) const
    {
        os << "val1 = "
            << first_int_
            << ", val2 = "
            << second_int_
            << ", val3-5 = "
            << chars_
            << ", val6 = "
            << std::setprecision(3)
            << float_value_
            << std::endl;
    }
};

int main ()
{
    data d(4, 56, "tes", 3.14f);
    d.print(std::cout);
    return 0;
}

See how they run!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文