Thread: How to delete part of an array?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    How to delete part of an array?

    In one tutorial I downloaded, it said use "delete [] arrayname", but when I attempted to compile, it said "delete function undefined" or something to that effect. So I tried this:

    Code:
    char myname[4] = "ash";
    ash[1] = ""; //trying to delete the s
    printf("%s", ash);
    but when it printed, it printed "a*h" (in place of the asterick it had some other weird symbol that I have no idea how to reproduce here ) So I guess I'm just asking if someone can tell me how to delete a single element of a string array. (I assume it's the same for a non-string array too). Also I see that you can't put a space in that way either..., I tried ash[1] = " "; so it should print "a h", but instead it has that weird character again....

    Thanks for any help, I promise that once I start to learn more of c i'll ask more complicated stupid questions

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    delete
    That is C++ .. decide whether you want to code in C or C++ and post in the appropriate forum.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > In one tutorial I downloaded, it said use "delete [] arrayname"
    1. That delete is C++, this is the C forum
    2. You can only delete what you got from doing new.

    Basically, to remove an element from an array, you need to manually shuffle down all the following elements, so
    Code:
    ash[1] = ash[2];
    ash[2] = ash[3];
    Naturally, a for loop would make this a lot easier.

    Or even using the standard function memmove() if you get bored of writing too many loops.

    Don't use something like strcpy( &ash[1], &ash[2] ). Nearly all the standard functions are undefined if the memory blocks overlap, as in this case. memmove() is a specific exception which works properly even with overlapping memory ranges.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    delete is a C++ keyword, and with a [] context, it means to deallocate something that was previously pointing to an array, I think. It doesn't exist in C.

    Anyway, you can't "delete an array", and you can't "delete part of an array". An array has a fixed size, all you can do is change the values inside. In the context of what you are asking above, you actually want to replace the s with a space, so ash[1] = ' '; (note single quotes) would suffice. Your double quote efforts won't work, they are for string literals, not characters.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Ping, my mistake, I didn't know it was a c++ keyword. I am actually just trying to learn c, sorry 'bout that.

    Salem & cwr - thanks again to both y'all for the help, it is *greatly* appreciated.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include<stdio.h>
    
    int main()
    {
          char myname[4] = "ash";
          ash[1] = " "; //need to give space in between
          printf("%s", myname);
          getchar();
          return 0;
    }
    
    /*myoutput
    a h
    */
    ssharish2005

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by ssharish2005
    Code:
    #include<stdio.h>
    
    int main()
    {
          char myname[4] = "ash";
          ash[1] = " "; //need to give space in between
          printf("%s", myname);
          getchar();
          return 0;
    }
    
    /*myoutput
    a h
    */
    ssharish2005
    That code is wrong. For a start, ash is undeclared. Also, it should be ' ' not " ".

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cwr
    That code is wrong. For a start, ash is undeclared. Also, it should be ' ' not " ".
    So what you're really saying is:
    Quote Originally Posted by ssharish2005
    /*myoutput
    a h
    */
    Should read something like:
    Quote Originally Posted by ssharish2005
    /*myoutput
    myout.c: In function `main':
    myout.c:6: error: `ash' undeclared (first use in this function)
    myout.c:6: error: (Each undeclared identifier is reported only once
    myout.c:6: error: for each function it appears in.)
    myout.c:6: error: parse error before '/' token
    myout.c:5: warning: unused variable `myname'
    */



    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    sorry i thought i could just type the code but it end up with the disaster sorry about that here is a compiled code
    Code:
    #include<stdio.h>
    
    int main()
    {
        char str[4] = {"ash"};
    
        str[1] = ' ';
        printf("%s",str);
        getchar();
    }
    /*my outout
    a h
    */
    NOTE: it shouldn't be " " it should be ' ' cose it's just a single char rather than stringssharihs
    Last edited by ssharish2005; 12-31-2005 at 10:35 PM.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    Smile solution to deleting ,part of an array

    the solution to the problem i figured out ,is as follows

    /*Program ----> deleting a part of an array */
    /*In this program we are going to delete 'd'
    key idea --->make the adderess of 'd' point to the next , and the next to the next
    From -----------> Ekarshi Mitra (Btech ------1Year----CSIT)
    Date -----------> 1-01-2006
    Time -----------> 11:45 P.M*/
    Code:
    #include <stdio.h>
    #include <string.h>
    int main ()/*void main() can also be used , depends unpon programmer*/
    
    {
        char name[]="under test";
        int marker=2,count,string_length=strlen(name);
         
        /*now suppose we remove 'd' from under test*/
        for(count=marker;count<string_length;count++)
        {
                                                        name[count]=(int)*&name[count+1];/*typecasting done here*/
                                                     /* can be compiled removing (int) */
                                                     }
                                                     printf(" ans = %s",name);
                                                     getchar();
                                                     return 0;
                                                     }
    /*compiled and run under devc++ (IDE) */

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > /*void main() can also be used , depends unpon programmer*/
    As in clueless ?

    > name[count]=(int)*&name[count+1];/*typecasting done here*/
    Well that was pointless. Since the * and & cancel out, why not just write the bloody obvious
    name[count]=name[count+1];

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Removing an element from an array is easy, but it does not change the size of the array. You would have to remove the element, re-allocate a new array the size you need, copy the current elements from the old one to the new one, point your pointer to the new one, de-allocate the old one.

    A pain in the butt. If you want a better approach to dynamic arrays, move to C++ STL and use a vector.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  2. delete dynamically allocated char array
    By xddxogm3 in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2003, 04:57 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Cannot delete last element in array
    By Simon in forum C Programming
    Replies: 10
    Last Post: 09-12-2002, 08:29 PM
  5. How do i delete a certain part of a text file?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2001, 06:00 PM