what happens when an array is instantiated?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Nyap, May 28, 2016.

  1. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    take this statement
    Code:
    int eof[5];
    it makes a pointer to store the address of the first integer and the 5 integers are created seperately, and next to each other in memory
    is that correct?
     
  2. cots

    cots GBAtemp Fan

    Member
    485
    315
    Dec 29, 2014
    United States
    Correct.
     
  3. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    I'm confused because the guy who wrote my tutorial says otherwise
    for example:
    Code:
    testFunction(arrayArgument) //the array thats being passed to testFunction is implicitly converted to the address of the first element in the array
     
  4. Frederica Bernkastel

    Frederica Bernkastel WebPerf and PWA advocate; @antoligy on Twitter

    Member
    3,150
    758
    Jan 31, 2008
    Hinamizawa
    Declaring the array divvies up 20 bytes (5 * int (4b)) of memory, split into buckets for each int. Evaluating it will give you a pointer which points only to the array, not to the first int.

    Code:
    #include <iostream>
    
    using namespace std;
    int main(int argc, char *argv[]) {
        int eof[5];
    
        printf("Incorrect: %i\n", eof); 
        printf("Correct: %i\n", eof[0]);
     
     
        eof[2] = 3;
        printf("Item 2: %i\n", eof[1]);
        printf("Item 3: %i\n", eof[2]);
    }
    Which results in:

    Code:
    Untitled 4.cpp:7:28: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
            printf("Incorrect: %i\n", eof);
                               ~~     ^~~
    1 warning generated.
    Incorrect: 1496237840
    Correct: 0
    Item 2: 0
    Item 3: 3
     
    cots likes this.