I have a variable buf stored as char *buf this variable comes out to be an ID that looks something like /3B494538-9120-46E0-95D4-51A4CF5712A1. I want to remove the first element of the char *buf so that /3B494538-9120-46E0-95D4-51A4CF5712A1 becomes 3B494538-9120-46E0-95D4-51A4CF5712A1. How do I do this?
Add a comment
|
1 Answer
You can create an array that reuses buf's memory:
char *nonCopyBuf = buf + 1;
or allocate new memory storage:
char *copyBuf = malloc(strlen(buf));
memcpy(copyBuf, buf + 1, strlen(buf));
//...
free(copyBuf);
2 Comments
Coldsteel48
(strlen(buf) -1) ? maybe ? since new array should be 1 char less
toma
@ColdSteel it is also needed to copy a
0-value byte that terminates sting and is not included in strlen() result.