数据中嵌入多种类型
是否可以在一个数据变量中存储多种数据类型,例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,malloc 怪癖。当我在我的机器上尝试这个时,我得到了和你一样的结果。但奇怪的是,当我将转换移到指针增量之外时,我得到了 SecondInteger (56) 的正确值,但 floatValue 的值仍然是 0.00。不太确定如何解释这种行为,但我敢打赌这与字节对齐有关。我需要再考虑一下。
像 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.
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 :)
是的,但是使用结构要容易得多。
你的例子似乎在我的编译器上运行,但我懒得校对它是否符合标准(未定义的行为等),这是你应该使用 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
:).由于它被标记为 C++,所以我建议您了解类。以下是开始将代码放入
类
中的一种方法:查看它们如何运行!
Since this is tagged C++, I suggest you learn about classes. Here is one way to start putting your code in a
class
:See how they run!