Reputation: 25
What i am trying to accomplish here is taking an cString and replacing a certain character with another which place i find via using the strchr() function.
what i can't figure out is how you can replace the character all my attempts (below commented out) all produce either an unedited string or crash the program. i believe i am going in the right direction with replacing the character (take the starting address of char *c and add n (the number of bytes forward the character i want to replace is) and then write to that new address.), but i can't seem to get it to function correctly.
any help is appreciated.
int main()
{
char *c, *sch;
int n;
c = "this is a test\n";
sch = strchr(c, 'a');
if(sch != NULL)
{
n = sch-c+1;
printf("%d\n", (int)sch);
printf("%d\n\n", (int)c);
printf("'a' found at: %d", n);
}
/////////////////////
//sch = &c;
//*(sch + n) = 'z';
/////////////////////
//*(c + n) = 'z';
/////////////////////
//c[n] = 'z';
/////////////////////
printf("\n\n%s", c);
getchar();
return 0;
}
Upvotes: 1
Views: 638
Reputation: 329
#include <stdio.h>
#include <string.h>
int main()
{
char c[80], *sch;
int n;
scanf("%s", c);printf("string %s \n", c);
//c = "this is a test\n";
sch = strchr(c, 'a');
if(sch != NULL)
{
n = sch-c;
printf("%d\n", (int)sch);
printf("%d\n\n", (int)c);
printf("'a' found at: %d", n);
}
/////////////////////
sch = &c;
*(sch + n) = 'z';
/////////////////////
*(c + n) = 'z';
/////////////////////
c[n] = 'z';
/////////////////////
printf("\n\n%s", c);
getchar();
return 0;
run above snippet with the string "this-is-a-test"
Problem with your earlier code is that string literals end up in read-only section and you can't modify them. So, writing it as char c[] ="this is a string" also won't help.
Upvotes: 0
Reputation: 126957
Edit: first of all, c
should be an array and not a pointer to a string literal, since string literals are stored in read-only memory, and trying to modify them will usually result in a crash.
So, first of all,
char c[]= "this is a test\n"
This initializes a modifiable string to that value, that you can edit without problems.
In general, you shouldn't directly assign string literals to char *
, because you can incur in this kind of problems; instead, assign them only to const char *
, that way any modification attempt will result in a compilation error.
Then, strchr
already returns a pointer to the matching character (as stated in the documentation), you can simply change it directly, no pointer arithmetic is involved:
*sch = 'z';
Still, if you want to experiment with pointer arithmetic, there's an error in the definition for n
:
n = sch-c+1;
If then you use it as an index of the string, you should remove that 1, because arrays (and strings) are zero-based. In other words: if you assign to n
that value, the following code:
*(c + n) = 'z';
(which is equivalent to
c[n] = 'z';
)
Will mean
*(c + sch - c + 1)
that is,
*(sch+1)
i.e. the character following the one found by strchr
. Removing the +1
will do the trick (although it's just a convoluted way to simply say *sch='z'
).
The other try is wrong because in your code &c
will yield a char **
, i.e. a pointer to a pointer to char, which is not what you want.
Upvotes: 2
Reputation: 45087
The problem here is not in your pointer arithmetic (although you are making it more complicated than it needs to be, see Matteo Italia's answer).
Your problem is that the following code doesn't do what you think it does:
char *c;
c = "this is a test\n";
This code creates a pointer and assigns it the address of a static string. Even with Matteo's suggestion, you won't be able to modify the static string.
To fix this, you can do one of two things. You can declare c
as an array and initialize it with the string. You can also let c
remain a pointer, then use malloc
to allocate a buffer (storing the buffer's address in c
) and then use strcpy
to copy the static string into the buffer. At that point, you should be able to manipulate the string as you are trying to do.
Upvotes: 0
Reputation: 2857
In your code, c
points to a constant string (a string literal). Modifying it is undefined behavior. You should copy it into an array of char in stead (e.g.:
char c[] = "This is a test"
Your first attempt that's commented out takes the address of c
and puts it into sch
- that yields a char**
, which is not what you want. The compiler should have picked that up, btw - perhaps turn some more warnings on.
Your second attempt should be OK, and the third one is basically the same thing.
BTW: n should be sch - c
- no need for the + 1
, and you should initialize n
to something in case you can't find the character you're looking for..
Upvotes: 1
Reputation: 57076
In C, a quoted string is a char *
, but that doesn't mean you can modify it. Try using c
as an array (char c [] = "this is a test\n";
) or even allocate memory and copy the text in.
Upvotes: 1