将1类型的1个变量复制到C中的另一个类型的另一个变量

时间:2016-10-16 06:24:27

标签: c

昨天我接受了一次采访,他们让我写了一个接受3个参数,1个源,1个目的地和其他长度的函数,这个函数应该根据长度参数和类型将值从源复制到目标。来源和目的地可能不同。

有人可以帮我写一个通用函数吗?

提前多多感谢

1 个答案:

答案 0 :(得分:2)

您的意思是<?php if ($_POST){ $name = $_POST['name']; $content = $_POST['commentContent']; $handle = fopen("comments.html","a"); if ($_POST['name']=="d1234"){ $_POST['name']="maneger"; fwrite ($handle,"<hr><b>" . "<h2 style=" . "color:red;" . ">" . $name . " </h2></b></br>" . "color:red;" . $content . "</br><hr>"); fclose ($handle); } else{ fwrite ($handle,"<hr><b>" . "<h2 style=" . "color:blue;" . ">" . $name . "</h2></b></br>" . $content . "</br><hr>"); fclose ($handle); } } ?> <html> <body> <form action="" method="POST"> Comment: <textarea rows ="3" cols ="30" name="commentContent"></textarea> </br> name: <input type = "text" name = "name"></br> <input type = "submit" value = "!פרסם"></br> </form> <?php include "comments.html"; ?> </body> </html> (或memcpy)? :P

一个天真的实现(使用字节):

memmove

可以使用int my_memcpy(void *dest, const void *src, size_t len) { if (dest == NULL || src == NULL || src == dest) return -1; if (len == 0) return 0; char *dest_bytes = dest; const char *src_bytes = src; for(size_t i = 0; i < len; i++) { dest_bytes[i] = src_bytes[i]; } return 0; } 指针进行优化(使用uint64_t处理余数)并循环展开以在for循环的每次迭代中复制更多数据。