DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How to delete data inside of character

  1. #1
    NANOSAURUS Guest

    How to delete data inside of character


    Hi, how do i delete data inside of a character and reinitialize it with different
    data.

    #include <stdlib.h>
    #include <iostream.h>
    struct CMDIP
    {
    char COM[20];
    };

    int main()
    {

    CMDIP input;
    char DRVIN[20]= "COMMAND:>";
    cout << "enter data";
    top:
    cout << "\n\n\n" << DRVIN;
    cin >> input.COM;
    if (input.COM == "CHANGE" || input.COM == "change")
    {

    DRVIN = "FLOPPY:>";
    goto top;
    }
    return 0;
    }

    i need to change the value of DRVIN when the user inputs "change or CHANGE"
    i keep getting compiler errors that say incompatable assignment types of
    const char[20] and char [9] in DEVC++. THANX in advance....NANOSAURUS

  2. #2
    rd Guest

    Re: How to delete data inside of character


    hii friend.
    U have made a common mistake.
    Pl note that u r declaring DRVIN as an array of 20 characters.It is an array.


    So when u want to change the contents of it, following statement is illegal

    DRVIN = "FLOPPY:>"; //illegal

    here DRIVIN represents pointer to the first block of contiguous memory locations.

    When u declare an array, framework permits initialisation using the statement
    as

    char DRVIN[20]= "COMMAND:>";

    Here it internally uses loop to assign the individual elements.

    Whenever u want to copy strings use following:

    strcpy(DRVIN,"FLOPPYA");

    for this u will have to include header <string.h>

    Whenever u want to handle strings, best way is to use pointers and dynamic
    memoy allocation using malloc().

    Does this answer ur question.


  3. #3
    bobbyO Guest

    Re: How to delete data inside of character


    "rd" <phantom2k@rediffmail.com> wrote:
    >
    >hii friend.
    >U have made a common mistake.
    >Pl note that u r declaring DRVIN as an array of 20 characters.It is an array.
    >
    >
    >So when u want to change the contents of it, following statement is illegal
    >
    >DRVIN = "FLOPPY:>"; //illegal
    >
    >here DRIVIN represents pointer to the first block of contiguous memory locations.
    >
    >When u declare an array, framework permits initialisation using the statement
    >as
    >
    >char DRVIN[20]= "COMMAND:>";
    >
    >Here it internally uses loop to assign the individual elements.
    >
    >Whenever u want to copy strings use following:
    >
    >strcpy(DRVIN,"FLOPPYA");
    >
    >for this u will have to include header <string.h>
    >
    >Whenever u want to handle strings, best way is to use pointers and dynamic
    >memoy allocation using malloc().
    >
    >Does this answer ur question.
    >


    If you are going to use heap allocations you would want to new these buffers
    and when you reassign it do a realloc or you may not utilize the whole buffer
    or worse yet overrun it.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center