Thread: Deleting first character of an array

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Deleting first character of an array

    Suppose we have:

    char myString[] = "This is a test";

    If I want to delete the first character of an array, I am always tempted to do:
    myString1++

    The above doesn't work. What does work however is:


    char* myString1 = myString + 1;


    What makes this even more contradictory is that "The name of an array usually evaluates to the address of the first element of the array,"

    Why doesnt the first line of code work?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The name of an array usually evaluates to the address of the first element of the array, but what it doesn't do is evaluate to a modifiable address. Defining a separate pointer, though, gives you something you can move. (EDIT: So you can use its value, but you cannot put it on the left side of an =, and you are doing that [implicitly] with ++.)

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    I understand. Is there any design rationale behind this (Edit: I mean why was the language designed like this)? If you could evaluate an array name to a modifiable address, you would only lose the reference to the original element which isn't a disaster.
    Last edited by Vespasian_2; 10-25-2018 at 07:14 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you could evaluate an array name to a modifiable address, you would only lose the reference to the original element which isn't a disaster.
    But then the compiler wouldn't be able to easily free all the memory it allocated and would probably cause memory leaks. By the way if you had dynamically allocated the memory for that array changing where that pointer points would likely cause a memory leak as well.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Vespasian_2 View Post
    I understand. Is there any design rationale behind this (Edit: I mean why was the language designed like this)? If you could evaluate an array name to a modifiable address, you would only lose the reference to the original element which isn't a disaster.
    This is true for every variable.
    Code:
    int a; \\a particular memory slot is set aside for a
    int *b = &a;  \\b can be set to point wherever you want it to point
    &a = &b; \\ERROR: can't move actual variable a
    &b = b + 1; \\ERROR: can't move actual variable b
    b = b + 1; \\fine: changes the value of b so it points to the "next" memory space.
    There's no reason to believe that your variables live in places that have addresses, necessarily.

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Thumbs up

    Quote Originally Posted by tabstop View Post
    This is true for every variable.
    Code:
    int a; \\a particular memory slot is set aside for a
    int *b = &a;  \\b can be set to point wherever you want it to point
    &a = &b; \\ERROR: can't move actual variable a
    &b = b + 1; \\ERROR: can't move actual variable b
    b = b + 1; \\fine: changes the value of b so it points to the "next" memory space.
    There's no reason to believe that your variables live in places that have addresses, necessarily.
    Ah I never saw it that way. Interesting, makes sense now that I think about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-08-2014, 08:52 AM
  2. How do I remove a character from character array?
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 12-08-2010, 11:27 AM
  3. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  4. Deleting a single character from a string
    By caduardo21 in forum C Programming
    Replies: 10
    Last Post: 02-16-2005, 06:51 PM
  5. Deleting from an array
    By Dargoth in forum C++ Programming
    Replies: 5
    Last Post: 02-06-2002, 03:56 PM

Tags for this Thread