Download presentation
Presentation is loading. Please wait.
Published byAndrew Skinner Modified over 8 years ago
1
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC 230 - Spring 2007, Section 001
2
Copyright Rudra Dutta, NCSU, Spring, 20072 Array Syntax Recall - – Arrays are alternative representations of blocks of memory pointed to by pointers To define an array: – type arrayName[size]; Size should be static (compiler can compute) For example – int id[1000]; id is like int * – char *names[2*50+1]; names is like char** – #define SIZE 10 – double scores[SIZE+1]; Variable size is okay on the stack – Not in global variables – (C99 prohibits variable even on stack)
3
Copyright Rudra Dutta, NCSU, Spring, 20073 Passing Arrays, Again When passing as argument, it is purely a pointer – Size must be passed some other way – Additional parameter is obvious way – void func1 (double val[], int num_elements); – void func1 (double *val, int num_elements); – void func3 (double val[20]); ???
4
Copyright Rudra Dutta, NCSU, Spring, 20074 Arrays and sizeof int id [1000]; int *pointerId; sizeof(id) is 1000*sizeof(int) – NOT 1000 sizeof(pointerId) is the number of bytes used to store a pointer to an int. The following sets pointerId to the last element of the array id : pointerId = id + sizeof(id)/sizeof(id[0]) - 1;
5
Copyright Rudra Dutta, NCSU, Spring, 20075 Passing Arrays #include typedef int mintar[1000]; void test (int fid[20]) { printf ("%d \n", sizeof(fid)); } int main () { int id[1000]; int * const pid = malloc (1000 * sizeof(int)); int k = rand(); int vid[k]; mintar tid; int iid[] = {1, 2, 3}; id[1] = 20; pid[20] = 1; vid[0] = 6; tid[6] = 21; printf ("%d %d %d %d %d %d\n", sizeof(id), sizeof(pid), sizeof(vid), k, sizeof(tid), sizeof(iid)); test(id); }
6
Copyright Rudra Dutta, NCSU, Spring, 20076 2-dimensional Arrays int values[2][3]; Recall that values[] looks just like a pointer access int a [3] – “ a is a pointer to a block of 3 int s” int values [2] [3]; – “ values is an array of 2 pointers to block of 3 int s” – “Pointer to block of 2 pointers to block of 3 int s” But internally “flattened” Can be accessed as pointers
7
Copyright Rudra Dutta, NCSU, Spring, 20077 Blocks Containing Pointers Can create the structure explicitly with pointers – block points to a block containing three pointers to double – In order to access a single object, the code has to apply dereferencing twice to block For a block b of pointers, b[i][j] refers to the j-th object in a block pointed to by b[i]
8
Copyright Rudra Dutta, NCSU, Spring, 20078 Blocks Containing Pointers double **block; #define SIZE 3 if((block=calloc(SIZE, sizeof(double*)))==NULL) { /* error code */ } for(i = 0; i < SIZE; i++) if((block[i]=calloc(1, sizeof(double)))==NULL) { /* error code */ } *(*block) = 2.1; block[0][0] = 2.1; /* initialize block */ for(i = 0; i < SIZE; i++) block[i][0] = 2.1 + i; /* free memory */ for(i = 0; i < SIZE; i++) free(block[i]); free(block); block = NULL;
9
Copyright Rudra Dutta, NCSU, Spring, 20079 Pointers vs. Multi-Dim Arrays Array notation may be simpler (initially) – Is less efficient Internally, compiler re-creates pointer structure – Wastes space for strings Internally, space must be reserved for longest sub-array Pointer notation is complicated, but consistent – But incurs explicitly space for extra pointers – However, only space required is actually used In general, memory management becomes programmer’s task beyond simple cases
10
Copyright Rudra Dutta, NCSU, Spring, 200710 Another Way: Explicit “Flattening” Ultimately, multi-dimensional arrays must be kept in (1- dimensional) memory This “flattening” can be explicitly handled by the programmer to avoid the overhead of pointer arrays Disadvantage: cannot access with multi-d array notation any more int *p; p = (int *) malloc (cols * rows * sizeof (int); *(p + cols *i + j) = 5; /* element of Row i, Column j */
11
Copyright Rudra Dutta, NCSU, Spring, 200711 Main Function’s Arguments hex file1 file2 int main(int argc, char **argv); int main(int argc, char *argv[]);
12
Copyright Rudra Dutta, NCSU, Spring, 200712 Command Line Arguments Many (most?) programs have command line options or arguments – E.g., man, gcc, … These are passed to the program as input parameters of the function main() – First parameter: count of the number of arguments – Second parameter: an array of pointers to the arguments (which are character strings) By convention, the first argument is defined to be the name by which this program was invoked
13
Copyright Rudra Dutta, NCSU, Spring, 200713 Command Line Arguments The following just prints out the command line arguments, one by one (starting with program name) #include int main(int argc, char **argv) { int i; for (i=0; i < argc; i++) printf("%s\n", *argv++); return 0; } Count of # of arguments Array of pointers to arguments
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.