1

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?

0

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

(strlen(buf) -1) ? maybe ? since new array should be 1 char less
@ColdSteel it is also needed to copy a 0-value byte that terminates sting and is not included in strlen() result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.