Search code examples
iphoneobjective-cpointerscharmemcpy

How to store int in char * in iphone


Can anyone help converting the Int to char array as i have buffer as

char *buffer = NULL;  
int lengthOfComponent = -1;   
char *obj;
buffer[index]= (char *)&lengthOfComponent; 

if i do this it is thorwing EXCESS BAD ACCESS after the execution how to store the value of the obj to buffer using memcpy


Solution

  • Of course you cannot write in buffer[index], it is not allocated!

    buffer = malloc(sizeof(char) * lengthOfBuffer);
    

    should do it. After that you can write the buffer with memcpy or with an assignation, like you are doing.