Copying integer to integer with allocated memory

Fjodor picture Fjodor · Aug 25, 2014 · Viewed 11.7k times · Source

I have a problem with the code below.

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

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

Essentially what I want to do is to copy the value from an integer to my other integer which I have allocated memory for. Can this work with strncpy between integers or do I need another function?

Answer

Neal Miller picture Neal Miller · Aug 25, 2014

Just dereference anotherInteger.

*anotherInteger = anInteger;

strncopy is for strings.