我有:
typedef struct emm_packet_t // original struct 1
{
uchar emm[258];
int16_t emmlen;
uchar caid[2];
uchar provid[4];
uchar hexserial[8];
uchar type;
} EMM_PACKET;
考虑到结构体的所有成员都具有动态值,每当从代码中的其他位置调用 do_emm 时,它都会保持更改...
copy_function(EMM_PACKET *ep)
{
// here I wanna copy EMM_PACKET *ep to EMM_PACKET_s *ep_s
// if I do simple assignment like
EMM_PACKET_s *ep_s;
ep = ep_s; // I get warning: assignment from incompatible pointer type
// if I use memcpy the whole program crashes ...
EMM_PACKET_s = EMM_PACKET; // this also not working
}
static int do_emm(EMM_PACKET *ep)
{
copy_function(ep); // calling copy function with passing ep ...
}
我应该逐个复制成员吗?
我确信我的做法是错误的。请为我指明正确的方向 - 如何将结构复制到另一个结构中,或者如何创建第二个实例,或者只是将 *ep 复制到另一个结构中,例如 *ep_s 以便永久使用。
如果我将 *ep 分配给 ep_s 两者变得相等,但由于 ep 值不断变化,每当 ep 值更改 ep_s 也更改时,它也会影响 ep_s ,我希望 ep_s 中有静态值,只有当我调用复制函数时 ep_s 值才必须更改否则它必须保持不变,直到我再次调用复制函数。
简单地说,我想将 *ep 复制/存储到另一个说 *ep_s 中,如果我只是通过函数传递它,它会生成 Stuct 的副本,但 ep 值仍然会发生变化,并且它也会影响 ep_s 所以我想存储 ep_s永久地留在内存中可能是..我是菜鸟,抱歉大部分评论都在上面,哈哈..谢谢
================================================== ===================================
static EMM_PACKET *ep_s; // Globally Declared
typedef struct emm_packet_t // original struct
{
uchar emm[258];
int16_t emmlen;
uchar caid[2];
uchar provid[4];
uchar hexserial[8];
uchar type;
} EMM_PACKET;
copy_function(EMM_PACKET *ep)
{
ep_s = ep; // This works but values of EMM_PACKET ep members are keep changing , and it also effect ep_s value.
// when i call function first time from do_emm it bring the ep in here
and points it correctly to ep_s if i print the vlaues of member its comes equal/correct
but do_emm(EMM_PACKET *ep) values keep changes and it keep points to ep_s ,
i want to store the value of ep into ep_s when i call copy function only, this the reason
i wana deep copy of store the value of ep_s so it remain constant untill i again
call copy function.
}
static int do_emm(EMM_PACKET *ep) // its being called rapidly from other parts of the program
{
if(ep->type == SHARED && ep->emm[0] == 0x83) // i call copy function only in certain condition
{
copy_function(ep); // calling copy function with passing required ep to be copied to global EMM_PACKET ep_s ...
}
}
- 已再次编辑以获得更多说明...谢谢大家!
最佳答案
这不是结构的工作原理。
首先当两个结构体的所有成员都完全相同时,则有
没有必要创建新的结构,您只需拥有另一个实例(或变量)
相同的结构并继续。
就您的问题而言,请考虑以下示例:
#include<stdio.h>
struct one
{
int member1;
int member2;
char member3;
};
int main()
{
struct one var_one, var_two;
/* Assigning values to the members of instance "var_one" */
var_one.member1 = 1;
var_one.member2 = 2;
var_one.member3 = 'a';
/* to copy the values of one instance into another just use assignment operator */
var_two = var_one;
/* Now trying to access the values using "var_two" */
printf("member1 = %d, member2 = %d, member3 = %c\n", var_two.member1,var_two.member2
var_two.member3);
return 0;
}
您也可以在我使用简单实例的地方使用指针,但是您必须
传递对这些指针的引用以访问其他函数中的值。
进入你的代码:
P : stands for problem.
S : for solution.
更正:
ep = ep_s;
P1 :as per the requirement this assignment is wrong.
P2 :Even after correcting the assignment you will still get the warning.
S1:
由于您想将 EMM_PACKET *ep 复制到 EMM_PACKET_s *ep_s,因此您应该
赋值左侧有 EMM_PACKET_s *ep_s 类型的实例,并且
EMM_PACKET *ep 类型的实例位于右侧,因为赋值运算符有效
从右到左,即右侧的值分配给左侧的变量
边。另外,左侧必须有一个变量,但右侧可以
有一个变量或一个常量。
S2:
此赋值会向您发出警告,因为赋值运算符要求两侧的操作数必须具有相同的数据类型或兼容的数据类型。
我们需要兼容的数据类型将一种类型的实例(右值)转换为另一种类型
(左值)。
右值和左值指的是赋值语句右侧和左侧的值
运算符。
在操作符的左侧有一个指向 EMM_PACKET 类型实例的指针
右侧有一个指向 EMM_PACKET_s 类型实例的指针。
为了消除警告,您需要在两个服务器上拥有相同类型的实例 双方。
P3 :EMM_PACKET_s = EMM_PACKET; // this also not working
S3:
这不起作用,因为这不应该起作用。
不能在赋值运算符的两侧使用两种数据类型。
“=”是赋值“运算符”,根据定义,运算符对“操作数”进行运算,并且
不是他们的数据类型。
解决您的问题的更好方法是:
您不需要创建单独的方法来复制结构(不适用于相同的结构
至少)。
static int irdeto_do_emm(EMM_PACKET *ep, EMM_PACKET *ep_s)
{
/* I am assuming that "ep" and "ep_s" have already been malloc'd */
ep_s = ep; // now the pointer ep_s starts to point to the address inside "ep"
}
关于c - 如何将(深层)整个结构及其所有成员复制到另一个结构中? (经过编辑以使目标更容易实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824351/