Originally Posted by
winsonlee
char *string = "Hello how are you doing > file";
char *string2 = "hello > file how are you doing";
I wanted to to store the information in string one as
char *stringx = "Hello how are you doing";
char *string2x = "hello how are you doing";
How can i archieve this ?? I just wanted to remove the ">" and the string after ">" only.
First of all, you should learn the difference between a pointer to a string literal (what you have written)
Code:
char *p = "string";
and an array of char initialized by a string, like
Code:
char p[] = "string";
In the first case, you have a pointer to char that holds the address of an array of char terminated by a 0. This address belongs to a segment of memory that is not guaranteed to be mutable. (Actually, on embedded systems, it belongs to a segment that is implemented in *ROM or flash memory)
This is why it is highly recommended to define such pointers like this:
Code:
char const *p = "string";
It helps to prevent from a common error that is to attempt to write to a non-writable segment of memory.
On the second case, you have an array of char. Can be a string or not. The value of the elements can be changed. Just keep in mind that its size is defined by its initialization. It can't be rezised dynamically.
Code:
{
/* an array of 10 uninitialized char */
char a[10];
/* an array of char initialized with {'h','e','l','l','o',0} (valid string) */
char a[] = "hello";
/* an array of 10 char initialized with {'h','e','l','l','o',0, 0, 0, 0, 0} (valid string) */
char a[10] = "hello";
/* an array of 5 char initialized with {'h','e','l','l','o'} (Not a valid string) */
char a[5] = "hello";
}
All this introduction to say that
Code:
char *string = "Hello how are you doing > file";
char *string2 = "hello > file how are you doing";
should have been defined
Code:
char const *string = "Hello how are you doing > file";
char const *string2 = "hello > file how are you doing";
and is not mutable.
If you want to change these strings, according to your wishes, you can start with that:
Code:
char string[] = "Hello how are you doing > file";
char string2[] = "hello > file how are you doing";
Then you have to define clearly what you want. I guess you want to suppress the " > file" substring.
- Search the position of the substring (strstr())
- Move the end of the string at this place (memmove())
It's now your turn to play.