Programmers (c specifically)....

Discussion in 'OT Technology' started by dtfromep, Apr 3, 2005.

  1. dtfromep

    dtfromep New Member

    Joined:
    Oct 11, 2003
    Messages:
    1,674
    Likes Received:
    0
    Location:
    Saint Paul, MN
    Quick question-

    How would one take a (char *) and remove the first character but leave the rest?



    Example:

    $variable

    -needs to become-

    variable



    I cannot find any functions to do this, as they all search for $ from the beginning and return a pointer to the same place.

    Thanks in advance.
     
  2. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    i think it's more like arrays are pointers and yea you can just access all the elements after [0] and copy them into another string
     
  3. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,923
    Likes Received:
    11
    Location:
    Los Angeles
    :hsugh: No. Pointers ARE arrays. Pointers use an array index which allow pointers to point to any memory location in an array. There is of course a much bigger process involved since everything behind the scene are just memory locations in an array. Pointers exist because we could only access limited data in arrays.

    Anyway, extracting each element from the string and copying it over to another string takes much more effort than removing the first element in the array [0] and returning the rest of the string.
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Depends on what you need. If all you need is a pointer to the portion of the array that contains "variable" then you can do this:

    char *array = "$variable";
    char *mypointer = &array[1];

    This doesn't affect the contents of the original array, you're just skipping the 1st character and obtaining a pointer to the string starting at the 2nd character.

    If you want to actually shift the contents of the array one space to the left so that the '$' is overwritten, you can use strcpy:

    strcpy(array, &array[1]);

    strcpy simply copies one character at a time, so it's safe to do.

    And to clarify, a pointer is NOT an array. A pointer is reference to a memory location, and address. An array is a logically contiguous block of memory containing one or more instances of a data type. You can indeed use a pointer and the array access operator [] to access an array, but whether it's safe to do depends on whether the pointer references an actual array or not.
     
  5. mace

    mace i don't read

    Joined:
    Jun 6, 2004
    Messages:
    12,567
    Likes Received:
    0
    Location:
    Texas
    char *s = "$variable";
    s = &s[1];

    or

    char *s = "$variable";
    s++;

    doesn't remove anything, just moves the pointer up one byte.
     
    Last edited: Apr 4, 2005

Share This Page