#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    Arrays & 2nd file.cpp


    I need to make four or five arrays for my program. I would like to place them in another file as to not cluter up my main program, how can I accomplish this? Also, if I create my arrays in another file will that file aslo need #include<iostream> & a main function? I just tried it and I left the file pretty much blank except for my array which looks like this:

    Code:
    char *Array1={"one", "two", "three"};
    But I'M getting the following error:

    In file included from ArrayTestCase.cpp:3:0:
    Arrays.h:4:45: error: scalar object ‘Array1’ requires one element in initializer
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,889
    Rep Power
    481
    char*Array1[]={"one", "two", "three"};

    "one" is a pointer to character.
    "two" is a pointer to character.
    "three" is a pointer to character.
    {"one","two","three"} are several pointers to character.

    char* is a pointer character.

    char*Array1[] is space for an (memory limited) infinite number of pointers to character. Except that you nicely chose to use only 3.


    How can I tell? Operator precedence.
    [] evaluates before *.

    Thus you could parenthesize as

    char(*(Array1[]))

    Array1 can be indexed, has fixed size (determined by the initializer list), and is accessed as an array. On access c gets a value of the type outside those parentheses.
    Array1[0] is a pointer to character.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo