Thread: address of array

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    address of array

    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";

    how can &alphabet = alphabet ?

    that means "alphabet" contains it's own address.
    so how can alphabet[0] = 'a' ?

    this is my understanding of the following. correct me if i am wrong:

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    When the name of an array is used in an expression there is a conversion to a pointer to the first element of the array.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    how can &alphabet = alphabet ?
    It's just the way the language was defined. The name of the array alone is defined (to the compiler) as the address of the first element. So, you can't have an array called alphabet[], and another (different) variable called alphabet.

    Both &alphabet and alphabet point to the address of the 'a' in your string array.

    I'd say this was done to make it easy to pass an array to a function, but I don't think the designers of C/C++ were trying to make anything easy!

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    9
    wrong,

    &alphabet is not alphabet !!

    alphabet is a pointer to the first character while &alphabet is a pointer to the variable alphabet.

    alphabet is a char *
    &alphabet is a char **

  5. #5
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    To be exact, &alphabet should be of type char (*)[24]. It cannot be of type char ** as if it were, that would mean *alphabet would give access to a variable containing a char pointer, which is not true because alphabet "is" (not really but...) the adress of the first character thus making it a rvalue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. base address of a simple array
    By nacho4d in forum C Programming
    Replies: 13
    Last Post: 04-07-2008, 01:28 PM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM