C 如何将指向int类型的指针复制到结构数组中的uint8\u t数组中?
我正在写一个程序,它将读取文件并将值保存在数组中。 这是我的档案:C 如何将指向int类型的指针复制到结构数组中的uint8\u t数组中?,c,C,我正在写一个程序,它将读取文件并将值保存在数组中。 这是我的档案: communication1 : b8:27:eb:cf:54:2c, b8:27:eb:75:85:e4, 2000000; communication2 : mm:27:eb:cf:54:2c, xx:27:eb:75:85:e4, 2200000; 这是我的代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #i
communication1 : b8:27:eb:cf:54:2c, b8:27:eb:75:85:e4, 2000000;
communication2 : mm:27:eb:cf:54:2c, xx:27:eb:75:85:e4, 2200000;
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
struct mac{
uint8_t address_bytes [6];
};
void main(){
int count = 2, i = 0;
char *Trash[4], *Time[4];
int k=0;
int mac1[6], mac2[6];
char tempbuff[100];
char trash[20], mac_s[20], mac_d[20], time[20];
struct mac Mac1[2], Mac2[2];
int j = 0;
FILE *fptr = fopen("config", "r");
fseek(fptr, 0, SEEK_SET);
while(!feof(fptr)){
if (fgets(tempbuff,100,fptr)) {
printf("\n%s", tempbuff);
sscanf(tempbuff, "%15s : %17[^;], %17[^;], %17[^;];", trash, mac_s, mac_d, time);
Trash[i] = strdup(trash);
Time[i] = strdup(time);
sscanf(mac_s, "%x:%x:%x:%x:%x:%x", &mac1[0], &mac1[1], &mac1[2], &mac1[3], &mac1[4], &mac1[5]);
sscanf(mac_d, "%x:%x:%x:%x:%x:%x", &mac2[0], &mac2[1], &mac2[2], &mac2[3], &mac2[4], &mac2[5]);
for(j = 0; j < 6; j++){
Mac1[i].address_bytes[j] = (uint8_t) mac1[j];
Mac2[i].address_bytes[j] = (uint8_t) mac2[j];
}
printf ("Mac1[%d] is %02x:%02x:%02x:%02x:%02x:%02x and Time is %s\n", i, Mac1[i].address_bytes[0], Mac1[i].address_bytes[1], Mac1[i].address_bytes[2], Mac1[i].address_bytes[3],
Mac1[i].address_bytes[4], Mac1[i].address_bytes[5], Time[i]);
printf ("Mac2[%d] is %02x:%02x:%02x:%02x:%02x:%02x \n", i, Mac2[i].address_bytes[0], Mac2[i].address_bytes[1], Mac2[i].address_bytes[2], Mac2[i].address_bytes[3],
Mac2[i].address_bytes[4], Mac2[i].address_bytes[5]);
}
i++;
}
printf(" \n time0 is %s time1 is %s \n", Time[0], Time[1]);
fclose(fptr);
}
问题是,我无法找到将指针复制到结构数组的
uint8\t
字段的方法。对于字符串,我已经得到了提示-strdup
,那么uint8\u t
呢?要复制像uint8\u t
这样的普通字节,请使用memcpy
:
int main() {
uint8_t s[6] = { 0x1, 0x2, 0x3,0x4,0x5,0x6 };
uint8_t *t;
t = malloc(6*sizeof(uint8_t));
memcpy(t,s,6*sizeof(uint8_t));
printf("%x %x %x %x %x %x\n", t[0],t[1],t[2],t[3],t[4],t[5]);
}
你的问题不清楚。您的代码似乎正确地读入值并将其打印出来。你到底想做什么,而你目前还没有做?@dbush请注意输出。当我试图从我的
Mac1[**1**]
中的communication2
行扫描mac地址时,我的值与Mac1[**0**]
中的communication1
raw中的值相同。我期望通过此代码,我将在Mac1[**1**]
中具有类似mm:27:eb:cf:54:2c
的值,这在您的解释中并不清楚mm
不是有效的十六进制字符串,xx
也不是有效的十六进制字符串,因此不会在mac1
和mac2
中读取任何内容,并且它们以前的值不变。@dbush您是对的!我想这是stackoverflow中最愚蠢的问题了。谢谢。关于:void main(){
函数有两个有效签名:main()
int main(void)
和int main(int argc,char*argv[])
注意,两个签名都有返回类型ifint
,而不是void
我想你的代码和我的代码有区别-我没有2个uint8\u t数组,我只有一个数组和int类型的指针。所以如果我像memcpy(Mac1[I]。address\u bytes[j],(uint8\u t)Mac1[j],6*sizeof(uint8\t))这样做的话;
我将进行分段fault@Max:你问到指针的复制,并自己引用了strdup(),Stephan向你展示了memcpy()。你知道strdup()是做什么的,是吗?@kesselhaus,从我的角度来看,strdup()正在尝试分配内存来保存字符串,该字符串就像指向该字符串的指针一样,不是吗?strdup
需要一个以\0
结尾的字符串(而不是任何“长度”参数),而memcpy
用于复制任意内存块(但需要以字节为单位指定长度).memcpy
不分配内存;如果您也必须分配内存,则修改答案以显示其工作原理。
int main() {
uint8_t s[6] = { 0x1, 0x2, 0x3,0x4,0x5,0x6 };
uint8_t *t;
t = malloc(6*sizeof(uint8_t));
memcpy(t,s,6*sizeof(uint8_t));
printf("%x %x %x %x %x %x\n", t[0],t[1],t[2],t[3],t[4],t[5]);
}