使用分配的内存将整数复制到整数

标签 c memory-management copy integer strncpy

下面的代码有问题。

...
int anInteger;
...
//anInteger gets a value
...

int *anotherInteger;
label = (int *)malloc(sizeof(int));
strncpy(anotherInteger, anInteger, 40);

基本上我想做的是将值从一个整数复制到我为其分配内存的另一个整数。这可以在整数之间使用 strncpy 还是我需要另一个函数?

最佳答案

只是取消引用 anotherInteger

*anotherInteger = anInteger;

strncopy 用于字符串。

关于使用分配的内存将整数复制到整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25489260/

相关文章:

java - 使用多线程时如何深度复制 HashMap

java - 字符串匹配和整数匹配的时间复杂度

c - 如何在 C 中正确 fscanf txt 文件

c - 尝试理解我通过 TCP 连接发送/接收的 UART 数据包

c - 为灵活性和处理分配内存的更好方法

c++ - std::unordered_map 不释放内存

ubuntu 上的 Java 内存不足,但存在大量缓存内存

javascript - 如何将div内容复制到另一个页面

c++ - 点的复制构造函数

c++ - 从 float 转换为 QByteArray