I need to find if a char array starts with “ADD”. I know to use strcmp(), but I don’t know how to get the first three characters. I really hate working with c-strings. How can I take a slice of a char array like char buffer[1024]?
I need to find if a char array starts with ADD. I know to
Share
Use
strncmp("ADD", buffer, 3).I am not sure what you mean by “slice” but any pointer inside
buffercould be considered a slice. For example ifbufferis a string that starts with"ADD"thenchar *slice = buffer + 3is the same string with"ADD"removed. Note thatsliceis then a part ofbufferand modifying the content of theslicewill modify the content of thebuffer. And the other way round.If by “slice” you mean an independant copy then you have to allocate a new memory block and copy the interesting parts from
bufferto your memory. The library functionsstrdupandstrndupare handy for this.