Thread: Changing chars in a string

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    68

    Changing chars in a string

    Hey there! I want to cut a string by setting a char of to \0.

    The string is an argument of a function.

    Code:
    void FindDirectory(char *path)
    {
    
    ... char *pch; pch = strrchr(path, '*'); memset(path, 'e', pch-path); OR memset(path, 'e', (int)pch-(int)path); OR path[pch-path] = 'e'; ...
    }
    pch-path returns a correct value (3 in my case).

    My program crashes at the line where I want to change the character and I don't understand why, maybe because there are differences between those 3 methods?

    Code:
    char *path = "string";
    ...
    char path[] = "string";
    ...
    char path[100];
    wsprintf(path, "%s", "string");
    Please explain me why or show me a reference
    TIA Hawk

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char *path = "string";
    path is just a pointer to some read-only memory where the "string" is located
    so as it is should be declared as const char* to indicate that the buffer where the pointer points cannot be modified

    Code:
    char path[] = "string";
    is modifiable buffer which length is determined by the compiler to be suffitient to copy there "string" string including nul-character (in this sample it will be 7 chars long)

    So you can modify the contents of the buffer as soon as the new contents is not longer than the initial one

    Code:
    char path[100] = "string";
    is buffer of 100 characters initialized with "string" string

    and as so - it can be modified as long as new contents is not longer than 99 bytes
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    In this function:
    Code:
    void FindDirectory(char *path)
    {
    ...
    char *pch;
    pch = strrchr(path, '*');
    memset(path, 'e', pch-path);
    OR
    memset(path, 'e', (int)pch-(int)path);
    OR
    path[pch-path] = 'e';
    ...
    }
    You've got three different methods that do three different things.

    The third argument to memset() is the number of bytes to write at the location pointed to by the first argument. So you get something like:
    Code:
    char s[] = "string";
    memset(s, 'e', 3);
    puts(s); /* will print "eeeing" */
    This is what your first memset() call is doing. Your second memset() call is not correct because you're casting pointers to ints. Don't do that. Subtracting pointers is perfectly valid in C (with the caveat that you can't subtract arbitrary pointers, but rather pointers that point inside the same object).

    The last example you have will set just one character to 'e'. Namely, it will set the last '*' it finds to 'e'. If that's your goal, there is a clearer way to do it, if you weren't aware:
    Code:
    char *pch;
    pch = strrchr(path, '*');
    if(pch == NULL) return; /* your code should be doing something like this */
    *pch = 'e';
    "Clearer" because it's idiomatic.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Thanks for the answers.
    As my function FindDirectory() does stuff which needs a pointer to a const char I stay with
    Code:
    char *path = "string";
    but inside the function I copy the read only buffer to a modifiable buffer (as vart said) so I can change chars inside the string.

    Code:
    char fullpath[MAX_PATH];
    memcpy(fullpath, path, strlen(path));
    ...
    char *pch;
    pch = strrchr(fullpath, '*');
    if(pch == NULL)
    {
    	SendMessage(GetDlgItem(hwnd, IDC_LISTBOX1), LB_ADDSTRING, 0, (LPARAM)"Couldn't find *");
    	continue;
    }
    *pch = '\0';
    Problem solved.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use strcpy for copying strings. Your example is a little bad since it doesn't copy the NULL char.
    Use memcpy for more generic types when you can't assign it directly using =.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Oh that is so true but isn't strcpy bad too if there's no NULL at the end of binary data?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But path is NULL terminated, so it will copy the string into the fullpath buffer and add a new NULL char.
    For binary data, memcpy should be used, but this isn't binary data, is it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM