简体   繁体   English

C 语言:将数据从一个 typedef 结构复制到另一个并返回一个值

[英]C language: copy data from one typedef struct to another and return a value

I am new in C & I am currently working with structures.我是 C 的新手,我目前正在处理结构。 I am stuck & I would be very grateful if somebody came to my rescue.我被困住了,如果有人来救我,我将不胜感激。 So I have a function to write input data from the main module to the sub-module.所以我有一个 function 将输入数据从主模块写入子模块。 It should return 0 if it writes successfully, & 1 if it doesn't succeed.如果写入成功,它应该返回 0,如果不成功,它应该返回 1。 I have been reading widely but I am still unsure whether to use memcpy or not.我一直在广泛阅读,但我仍然不确定是否使用 memcpy。 Then, on the return value I have read that memcpy returns 0 if the operation was successful and 1 if it was not.然后,在返回值上,我读到 memcpy 如果操作成功则返回 0,否则返回 1。 Here is that part of my code:这是我的代码的那一部分:

typedef struct {
    UINT32 carnum_n;
    UINT32 carnum_k;
    UINT32 results[30];
    UINT32 feedback[30];  
    UINT32 errors[30];
} MAIN;

typedef struct {
   UINT32 carnum_n;
   UINT32 carnum_k;
   UINT32 feedback[30];
} SETTING;

/* function to copy the data from the main to the sub-module */    
UINT32 Write( MAIN *data, SETTING *info)
{
   sc_memcpy ( &info->carnum_k, &data->carnum_k, sizeof (SETTING));
   sc_memcpy ( &info->feedback, &data->feedback, sizeof (SETTING));  //copies the array
}
  1. Please tell me, is this correct?请告诉我,这是正确的吗? If not, what is the correct way to write it?如果不是,那么正确的写法是什么? Or what other alternatives do I have?或者我还有什么其他选择?
  2. As I have an array as well, how should I go about it?由于我也有一个数组,我应该如何处理它? What changes when I have to save say upto fb[10] from the main module to the sub-module?当我必须将 fb[10] 从主模块保存到子模块时会发生什么变化?
  3. How do I incoorporate the return condition, given that I should seemingly write ONE line of code to copy each type of data from the main module's table to the sub-module's input table?鉴于我似乎应该编写一行代码将每种类型的数据从主模块的表复制到子模块的输入表,我该如何合并返回条件?

I really want to get this right.我真的很想把这件事做好。 Thanks in Advance:)提前致谢:)

Question 1, your code is not correct.问题1,您的代码不正确。

When copy carnum_k , the sizeof(SETTING) is wrong.复制carnum_k时, sizeof(SETTING)错误。 sizeof (SETTING) means the size of all data in SETTING structure. sizeof (SETTING)表示SETTING结构中所有数据的大小。 But you just need copy carnum_k , so you need provide the size of carnum_k , which is sizeof (UINT32) , UINT32 is the type of carnum_k .但是您只需要复制carnum_k ,因此您需要提供carnum_k的大小,即sizeof (UINT32)UINT32carnum_k的类型。

memcpy ( &info->carnum_k, &data->carnum_k, sizeof (UINT32));

You can also just write an assignment info->carnum_k = data->carnum_k .你也可以只写一个赋值info->carnum_k = data->carnum_k

The copy of feedback has two problem.反馈的副本有两个问题。

  1. sizeof (SETTING) is wrong just like when you copy carnum_k . sizeof (SETTING)是错误的,就像您复制carnum_k时一样。 You can use sizeof(UINT32 [30]) , UINT32 [30] is the type of feedback .您可以使用sizeof(UINT32 [30])UINT32 [30]feedback的类型。
  2. info->feedback is an array address, so no need &info->feedback , just info->feedback . info->feedback是一个数组地址,所以不需要&info->feedback ,只需info->feedback
memcpy(info->feedback, data->feedback, sizeof(UINT32 [30]));

Question 2. I think you mean you may want just copy part of feedback .问题 2。我认为您的意思是您可能只想复制部分feedback

When you copy memory you need figure out src memory address, dest memory address, and the size of memory you want to copy. When you copy memory you need figure out src memory address, dest memory address, and the size of memory you want to copy.

so if you want copy from feedback[2] to feedback[10] .所以如果你想从feedback[2]复制到feedback[10]

  • source address is &(data->feedback[2])源地址是&(data->feedback[2])
  • dest address is &(info->feedback[2]) . dest 地址是&(info->feedback[2])
  • From 2 to 10, there is 9 value you want to copy, so the total size is 9 * sizeof(UINT32)从 2 到 10,有 9 个值要复制,所以总大小为9 * sizeof(UINT32)
memcpy(&(info->feedback[2]), &(data->feedback[2]), 9*sizeof(UINT32));

Question 3.问题 3。

I don't known the answer.我不知道答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM