开发者

how to replace a char in char *

开发者 https://www.devze.com 2023-01-28 23:07 出处:网络
total newbie here. i was trying to replace a character in char * but my program gives error #include开发者_开发问答 <stdio.h>

total newbie here. i was trying to replace a character in char * but my program gives error

#include开发者_开发问答 <stdio.h>

int main(int argc, char **argv)
{
    char *mystring ="love is alweys better yoe";
    int count = 1;

    for (count ;  count < 23; count++)
    {     
    if ((mystring[count] == 0x65 )) //&& ((mystring[count+1] > 0x41) && (mystring[count+1] < 0x7A))) 
    {
      mystring[count] = 0x45; //here occur the freezing
      printf ("%c\n", mystring[count]); 
      //break;
    };
    };

    printf("%s\n",mystring);
    return 0;
}


The

char *mystring ="love is alweys better yoe"

makes mystring read-only

you need to copy the string into a buffer before you can change it

e.g.

char mystring[128];
strcpy( mystring , "love is alweys better yoe" );


Stack allocation

char *mystring ="love is alweys better yoe";

This creates a string literal in read-only memory, and you cannot subsequently write to it to change characters.

You should initialise your string like this instead:

char mystring[] ="love is alweys better yoe";

This will allocate a character array of size 26 bytes - 1 byte per character, terminated with a null character \0.

Note that if you attempt to write past the end of the buffer (i.e. beyond the \0 character), you may be invading the memory allocated for other data in your program, and this can have undesirable consequences.

Heap allocation

The previous example allocates memory on the stack, and will be free'd at the end of the current level of scope (usually the function you are in). If you want the memory to persist beyond the end of the function call, you need to allocate it on the heap like so:

int bufferSize = 26;
char* mystring = malloc(bufferSize);
strncpy(mystring, "love is alweys better yoe", bufferSize);

And you will need to remember to free this memory when you are done with it:

free(mystring);

If freeing from the calling function, you will need to return the char* pointer back to the caller, so it knows which memory location to free. If you don't free this memory, your program will leak memory.

Increasing size of string

If you need to re-size the string after allocating memory for it, you can use realloc:

char* mybiggerString = realloc(mystring, bufferSize + 10);
strncpy(mystring, "I can fit more in this string now!", bufferSize);


"in the char*" is meaningless. You cannot change things that are "in" the pointer, because there is nothing "in" the pointer - it does not contain things. It points at things. That's why it's called a "pointer", not a "container".

When you write char *mystring ="love is alweys better yoe";, you cause mystring to point at a string literal. This is a chunk of memory that is dedicated to holding the text. It is in a special area of memory that may not be changed. (The text is stored in the .exe file; in most implementations, the OS loads your .exe file into memory, and then points the pointer into that part of the loaded .exe. You may not change this memory because the OS forbids it - basically, to make it harder to create a virus.)

Solution: Use a modifiable chunk of memory. Write it this way: char mystring[] = "love is alweys better yoe";. The string literal will still exist in your program, but this instructs the program to make a buffer (in memory which may be changed) and copy the literal into the buffer. This buffer is exactly the same length as the literal, so you still may not append to the string - only change the existing characters, or cut it off (by writing a null byte somewhere in the middle).

0

精彩评论

暂无评论...
验证码 换一张
取 消