I’m defining a char pointer in this manner.
char *s=(char *)malloc(10);
After I fill in all possible values that can fit here, I want to clear whatever I wrote to s and without using malloc again want to write in s? How can I do this?
I need to update the contents but if in the last iteration not all values are updated, then I’ll be processing over old values which I do not want to do.
Be careful!
malloc(sizeof(2*5))is the same asmalloc(sizeof(int))and allocates just 4 bytes on a 32 bit system. If you want to allocate 10 bytes usemalloc(2 * 5).You can clear the memory allocated by
malloc()withmemset(s, 0, 10)ormemset(s, 0, sizeof(int)), just in case this was really what you intended.See man memset.
Another way to clear the memory is using
callocinstead of malloc. This allocates the memory as malloc does, but sets the memory to zero as well.