Delete first 4 chars in char array

This is a discussion on Delete first 4 chars in char array within the C Programming forums, part of the General Programming Boards category; I'm trying to cut the first 4 chars (PWD=) of the environment variable, my_envp[0], which in this instance is "PWD=/home/usrnm/cprgms/project" ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Question Delete first 4 chars in char array

    I'm trying to cut the first 4 chars (PWD=) of the environment variable, my_envp[0], which in this instance is "PWD=/home/usrnm/cprgms/project"
    This is my attempt:

    Code:
    char pwd[] = "PWD=";
    printf("my_envp is: %s\n", my_envp[0]);
    memset(my_envp[0], '\0', strspn(pwd, my_envp[0]));
    printf("my_envp is now: %s\n", my_envp[0]);
    The second printf does not print anything. If I replace '\0' with a different character like 'k' it prints "kkkk/home/usrnm/cprgms/project"

    My questions are: What am I doing wrong? and why doesn't my implementation work logically?

    Thanks in advance for any help.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    A C-string is considered terminated by a 0-character (by most functions, at least). You are replacing the first four characters with another character. The 0-terminator. That is, the string ends there. So it ends in the beginning.

    The proper way to do it (in this case) would be to index it like: &char_array[4], or char_array+4.

    Should it be required of actually removing the four characters (possible, but unlikely), you would have to use memmove. But that would be a lot slower than simply indexing the array.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    I tried:

    Code:
    printf("my_envp is now: %s\n", &my_envp[4]);
    to see what you were talking about and my output was: `(P

    I'm obviously not getting the logic behind it. Could you please explain further?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Watch:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char *c = "Hello World!"
    
        printf( "%s\n", c + 6 );
        printf( "%s\n", &c[6] );
    
        return 0;
    }
    Quzah.
    Last edited by quzah; 05-02-2009 at 08:07 PM. Reason: I thought I had fixed that...
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cagney58 View Post
    I tried:

    Code:
    printf("my_envp is now: %s\n", &my_envp[4]);
    to see what you were talking about and my output was: `(P

    I'm obviously not getting the logic behind it. Could you please explain further?
    I said &char_array[4]. my_envp is an array of char pointers. So that would be:
    &(char_array[0])[4]
    Or, more simple:
    &char_array[0][4]

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    emaple: print first variable in linux (kde)
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21