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

Fjo*_*dor 2 c integer memory-management copy strncpy

我对下面的代码有问题。

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

int *anotherInteger;
label = (int *)malloc(sizeof(int));
strncpy(anotherInteger, anInteger, 40);
Run Code Online (Sandbox Code Playgroud)

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

小智 6

只是取消引用anotherInteger

*anotherInteger = anInteger;
Run Code Online (Sandbox Code Playgroud)

strncopy 用于字符串。