what happens when an array is instantiated?

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
54
Location
That Chaos Site
XP
482
Country
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?
 

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
54
Location
That Chaos Site
XP
482
Country
I'm confused because the guy who wrote my tutorial says otherwise
No, int test[5] makes a fixed array with 5 elements, and takes 20 bytes. In most cases, when test is evaluated, it will decay into a temporary pointer.

There’s nothing stopping you from indexing an array out of range due to the way the subscript index works -- this is discussed in more detail in the lesson on pointer arithmetic.
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
 
Joined
Jan 31, 2008
Messages
3,169
Trophies
2
Age
28
Location
Hinamizawa
XP
988
Country
Japan
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
 
  • Like
Reactions: cots
General chit-chat
Help Users
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
  • Iron_Masuku @ Iron_Masuku:
    Hello
    Skelletonike @ Skelletonike: hmm