Thread: Elementary question - shifting memory adresses

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    Elementary question - shifting memory adresses

    Hi,

    I have a fundamental question due to my lack of understanding how arrays work.

    Let say I have a character array (byte array).

    1. do array elements have memory adresses? i guess not but ....
    2. if yes is it possible to switch two array positions? For examle:

    a[x] change with a[y] for x,y < |a|

    as oppose to changing values assigned to individual array positions.

    As I said, the lack of basic understanding.


    thnx
    b

  2. #2
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    When you declare an array, you are basically allocating memory automatically, which is saved on the internal memory. If the array is declared in global code or with the static storage class modifier, then the array will have static storage, on the stack. Every data type is stored in memory so respectively they have a memory address. There is one exception for data, declared on the register, but this occurs in rare cases if you want to perform hardcore hardware operations. The offset address, for little-endian system is the name identifier.
    Code:
    char arr [50];
    arr will be the offset address. arr+1 will be the next address. You are basically "dereferencing" that address, using the index operator [ ] and the index itself.
    arr[5] is the value located on arr + 5; You can dereference it just like a regular pointer. *(arr + 5); At the end, array address decays to a pointer after all.

    Usually you can only declare array with a fixed length (number of items). Which means you can only pass a constant as an index (in the example above, 50), when declaring.
    But in C99 you can also use variable-length index.

    No, you can not mess with the addresses of the array. One array can not point to any other address of memory.
    You can copy values, but not redirect or reposition (unless it isn't an array of pointers). The array is very similar to any other regular data type.
    char arr[4]; can be considered as a 4 byte integer for example. You could treat like so, using type-casting.
    int arr[1]; is almost same as int arr. The difference is in the usage.
    Last edited by Al3; 02-12-2015 at 07:31 AM.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    Thank you !!
    This clarifies things

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Elementary question
    By Dan. in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2011, 01:47 PM
  2. Very elementary C++ question
    By Dondrei in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2008, 09:00 AM
  3. elementary question on vector and pop_back
    By mc61 in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2008, 09:08 AM
  4. An Elementary Windows Question
    By samGwilliam in forum Windows Programming
    Replies: 7
    Last Post: 01-20-2006, 04:20 PM
  5. elementary question
    By misplaced in forum Tech Board
    Replies: 8
    Last Post: 12-17-2004, 07:27 AM

Tags for this Thread