<datatype> <arrayname> [ <size> ];
To confuse the issue a bit, some compilers (g++ being one of them) actually let you get away with declaring variable-sized arrays, as an extension of C (where this was allowed). If you are trying to create quality portable code you should stick with constants. |
char foo[30];
const int SIZE = 12; int main() { float myarray[SIZE]; }
#include <cstdio> // mystring will describe a type of array, // consisting of 80 characters const int SIZE = 80; typedef char mystring[SIZE]; int main() { // declare two variables of type mystring, // i.e. two character arrays of size 80 mystring firststring, anotherstring; }
myarray[i]
int myarray[25]; int x, y, z; myarray[12] = 99; myarray[8] = 0; x = myarray[12]; myarray[23] = x + myarray[8] * 3;
int x = 0; int myarray[20]; // an array of 20 integers myarray[0] = 17; // store 17 in position 0 x = myarray[0]; // x is assigned 17 myarray[x] = 3; // store 3 in position 17 myarray[x-3] = 0; // store 0 in position 14 myarray[1] = myarray[0]; // copy 17 from position 0 to position 1
for (int index = 0; index < 9; index++) { myarray[index] = index * index; }
int myarray[SIZE]; bool neg_flag = false; for (int index = 0; index < SIZE; index++) { if (myarray[index] < 0) neg_flag = true; } printf("The array did "); if (!neg_flag) printf("not "); printf("hold negative numbers\n");
#include <cstdio> const int INTSIZE = 20; const int FLOATSIZE = 32; const int CHARSIZE = 80; typedef int intarray[INTSIZE]; typedef float floatarray[FLOATSIZE]; typedef char chararray[CHARSIZE]; int main() { intarray myints; floatarray myfloats; chararray mychars; for (int index = 0; index < INTSIZE; index++) myints[index] = 0; for (int index = 0; index < FLOATSIZE; index++) myfloats[index] = 0.0; for (int index = 0; index < CHARSIZE; index++) mychars[index] = ' '; // and on with the rest of the program }
for (int i = 0; i < SIZE; i++) { printf("%d ", myarray[i]); }
printf("%f", myarray);
(usually) won't work
and can have really weird results
int main() { float myarray[30]; // do whatever to create array values // and now print it out: print_float_array(myarray, 30); } void print_float_array(float arr[], int size) { for (int i = 0; i < size; i++) { printf("%f ", arr[i]); } printf("\n"); }
myarray[3] = 17; x = myarray[12] + myarray[z]
const int SIZE = 80; int main() { bool boolarray[SIZE]; int intarray[SIZE]; float floatarray[SIZE]; char chararray[SIZE]; for (int index = 0; index < SIZE; index++) { boolarray[index] = false; intarray[index] = 0; floatarray[index] = 0.0; chararray[index] = ' '; } }
int myarray[50]; ... myarray[93] = 7;If the program doesn't immediately crash it will still probably screw up other program data, another cause of really weird behaviour
In the example below, the functions expect a floating point value,
and each array element contains a floating point value
so we can pass an individual array element:
void printfloat(float data); void incrementfloat(float &data); int main() { float myfloats[5] = { 10, 20, 30, 40, 50 }; printfloat(myfloats[3]); incrementfloat(myfloats[3]); printfloat(myfloats[3]); } void printfloat(float data) { printf("%f\n", data); } void incrementfloat(float &data) { data = data + 1; }
void change(float arr[], int place, float newval); int main() { float thearray[SIZE]; change(thearray, 3, 17.4); } void change(float arr[], int place, float newval) { arr[place] = newval; }Function change replaces the array value at position place with the newval. Here effectively
thearray[3] = 17.5
void cannotChange(const float arr[], int val) { // this function cannon alter the contents of the array parameter }
void printarray(float arr[], int arrsize); int main() { float myarray[10]; for (int index = 0; index < 10; index++) { myarray[index] = 0 - index; } printarray(arr, 10); } void printarray(float arr[], int arrsize) { for (int index = 0; index < arrsize; index++) printf("%f\n", arr[index]); } }
void fillarray(int array[], int size); int main() { int myarray1[20], myarray2[12]; printf("Filling first array\n"); fillarray(myarray1, 20); printf("Filling second array\n"); fillarray(myarray2, 12); } void fillarray(int array[], int size) { int numRead; // variable to track how many things scanf read int value; // variable for converted value bool valid; // recording validity of input for (int index = 0; index < size; index++) { // get and test input value valid = false; do { printf("Enter a positive integer\n"); int numRead = scanf("%d", &value); if (numRead <= 0) { printf("sorry, invalid input\n"); } } while (numRead <= 0); // assign to array once valid entry obtained array[index] = value; } }
const int STRINGSIZE = 80; typedef char mystring[STRINGSIZE];The value STRINGSIZE here tells us the longest string we can store However most string manipulation functions look for the null character, '\0', to tell them where the string actually ends.
// Here we use a typedef to create our own group of arrays, // used to represent names, and with a predefined fixed length // of up to 20 characters for the name itself, so a 21-character array const int NAMESIZE = 21; typedef char name[NAMESIZE]; int main() { // declare some name variables name firstname, lastname;
// here we use a typedef to create our own string type, // again just an array of characters, with a fixed length const int STRINGSIZE = 81; typedef char MyStringType[STRINGSIZE]; int main() { MyStringType text; printf("Enter a line of up to 80 characters\n"); // we use fgets to read the text, telling it to read from stdin (standard input) fgets(text, STRINGSIZE-1, stdin); printf("The line you entered was\n"); printf("%s", text); }
MyStringType name = "Dave";
printf("%s", arrayname);
printf("%c", arrayname[i]);
printf("%s", &(arrayname[i]));
fgets
scanf("%80s", arrayname); // (the 80 is the char limit)
the null terminator is automatically inserted.
If you insert the characters into the array one at a time
you must remember to put the null terminator in as well
The programmer may either specify the data is read from the keyboard (using the keyword stdin) or specify an open file pointer to use instead. (File pointers are discussed in the file io notes.)
A sample call would look like:
fgets(arrayName, maxSize, stdin);
(Note: this is NOT the same as the C++ string library and class.)
#include <cstdio> #include <cstring> const int SIZE = 81; typedef char mystring[SIZE]; int main() { mystring surname1, given1, whole1; // names for person 1 mystring surname2, given2, whole2; // names for person 2 printf( "Person 1: enter your given name\n"); fgets(given1, SIZE-1, stdin); printf( "and now your surname (family name)\n"); fgets(surname1, SIZE-1, stdin); printf( "Person 2: enter your given name\n"); fgets(given2, SIZE-1, stdin); printf( "and now your surname (family name)\n"); fgets(surname2, SIZE-1, stdin); // create whole names for each by copying given name // then appending the surname strcpy(whole1, given1); strcat(whole1, " "); strcat(whole1, surname1); strcpy(whole2, given2); strcat(whole2, " "); strcat(whole2, surname2); // print them in order of last name if (strcmp(surname1, surname2) < 0) { printf("%s\n%s\n", whole1, whole2); } else if (strcmp(surname1, surname2) > 0) { printf("%s\n%s\n", whole2, whole1); } else { printf("You have the same names!\n"); } }
#include <cstdio> #include <cstdlib> int main() { int myint; printf("Enter an integer\n"); int numRead = scanf("%d", &myint); // see if (atoi returned 0), and // (user didn't actually enter value 0) if (numRead == 0) { scanf("%*s"); printf( "Invalid value entered\n"); } else { printf("Valid value %d\n"); } }
char name[80]; name = "Dave";