Can anyone please help me? I need to remove the first character from a const char *
in C.
For example, const char * contents
contains a 'x'
character as the first character in the array. I need to detect and eliminate this character, modifying the original variable after its been "sanitized".
Can anyone suggest how to achieve it? I'm completely new to C (though I know Java), and just can't seem to figure it out.
Note:
I already referred to these, and still not able to figure out:
How to remove first character from C-string? - this tell how to remove when the input is char * contents
AND
Difference between char* and const char*?
it mentions that const char* is a mutable pointer but points to immutable character/string
What I tried
below it works, but why it works?(it should not able to modify the immutable char array contents value "xup")
#include <stdio.h>
int main()
{
char input[4] = "xup";
removeLeadingX(input);
return 0;
}
int removeLeadingX(const char * contents)
{
//this if condition is something I have to add
//can't modify other code (ex: the method receives const char * only)
if(contents[0] == 'x'){
contents ;
}
printf(contents);//up - why immutable string "xup" updated to "up"?
return 0;
}
CodePudding user response:
You can't remove anything from the const char object.
- You need to make a copy of the string and then modify and use the copy
const char *original = "Hello world!";
char *copy = strdup(original);
copy[strlen(copy) - 1] = 0; //removing `!`
- You can modify the pointer to reference other element of the string. But you lose the original pointer and actually do not remove anything from the string.
const char *original = "Hello world!";
original = strchr(original, 'w');
printf("%s\n", original);
What I tried below it works, but why it works?(it should not able to modify the immutable char array contents)
In your example you modify the pointer not the object referenced by the pointer. It is OK.
If you declare this function as
void removeLeadingX(const char * const contents)
the pointer will also be const
and your code will not compile.
CodePudding user response:
Do you need to remove the leading character, or simply ignore it.
Here's an example of what the function is doing (simply changing the pointer)
int main() {
char input[4] = "xup";
char *p = input;
printf( "advance\n" );
p = p 1;
printf( "input: '%s'\n", input );
printf( "p: '%s'\n", p );
printf( "retreat\n" );
p = p - 1;
printf( "input: '%s'\n", input );
printf( "p: '%s'\n", p );
printf( "nothing has been removed or shifted except the pointer 'p'\n" );
return 0;
}
Output:
advance
input: 'xup'
p: 'up'
retreat
input: 'xup'
p: 'xup'
nothing has been removed or shifted except the pointer 'p'
CodePudding user response:
The function declaration int removeLeadingX(const char * contents)
doesn't make any sense. Either the function should modify the passed string "in place", in which case the function should be:
void removeLeadingX (char* contents)
Or the function should store a modified copy of the original (immutable) string somewhere, in which case the function could be:
void removeLeadingX (char* new_str, const char* old_str)
These could be implemented as:
void removeLeadingX (char* contents)
{
if(contents[0]) == 'x')
memmove(&contents[0], &contents[1], 1);
}
Or:
void removeLeadingX (char* new_str, const char* old_str)
{
if(old_str[0] == 'x')
{
// assuming new_str points at a caller-allocated array:
strcpy(new_str, &old_str[1]);
}
}
CodePudding user response:
Considering that the original object in main
is not const
, you can modify the array using a dirty trick to avoid warnings:
#include <stdio.h>
#include <string.h>
int removeLeadingX(const char *contents)
{
if (contents[0] == 'x')
{
union {const char *constant; char *not_constant;} dummy = {contents};
memmove(dummy.not_constant, contents 1, strlen(contents));
}
printf("%s\n", contents);
return 0;
}
int main(void)
{
char input[4] = "xup";
removeLeadingX(input);
return 0;
}