Array Varible

This is a discussion on Array Varible within the C++ Programming forums, part of the General Programming Boards category; I want to know how to delete an array of varible which is already exists? For example: char *hello = ...

  1. #1
    Jonh
    Guest

    Array Varible

    I want to know how to delete an array of varible which is already exists?


    For example:

    char *hello = "Billgates";

    now hello varible of array is exists,

    now i want to clear all the element of hello array or delete it hello varible?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786

    Re: Array Varible

    Originally posted by Jonh
    I want to know how to delete an array of varible which is already exists?


    For example:

    char *hello = "Billgates";

    now hello varible of array is exists,

    now i want to clear all the element of hello array or delete it hello varible?
    You cant...what hello represents is a pointer to an array of chars situated in const memory.....you cant alter that memory or resize it...only read it....

  3. #3
    jonh
    Guest
    I see,

    How about to check if name[256] is exists or not,

    I need something that cant be repeated twice or more using: char name[256];
    if it do then it would crash or something.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You can, however, put an empty string in its place. C-style strings are terminated with zero, usually expressed as '\0'. If you set the first element (element 0) of the array to zero and then print (display) it, nothing will print.

    hello[0] = '\0': // Set string length to zero by replacing the B at position zero with '\0'

    hello[4] = '\0'; // Set string length to 4 by replacing the g at position 4 with '\0' now hello will print as Bill.

    '\0' is often replaced with NULL, or you can use a numerical 0 (zero) but not an ASCII zero

    hello[4] = '\0'; // OK Changes Billgates to Bill
    hello[4] = NULL; // OK Changes Billgates to Bill
    hello[4] = 0; // OK Changes Billgates to Bill
    hello[4] = '0'; // OOPS! Changes Billgates to Bill0ates

    You can also fill the entire array with zeros if it makes you feel better

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Ahhh, your second question is a bit more "interesting" because there are probably some really cool (more advanced) ways of accomplishing whatever you're trying to do...

    The simpilest way of dealing with a bunch of names it to make an "array of arrays" Which is actually a two-dimensional array... and you can have multi-dimension arrays.

    char name[3][256] ; // Declares 3 each 256 character arrays (or a 2-dimensional 3 x 256 array)

    Now...
    name[0] can contain "BillGates"
    name[1] can contain "LarryEllison"
    name[2] can contain "SteveJobs"

    The more advanced concepts that come to mind are "structures", "linked lists", "vectors", as well as memory allocation and reallocation... but don't think you have to understand all that stuff now... I just wanted to give you a clue about what I thought was "interesting".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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