1 / 13

Arrays and Arrays as Parameters

myArray[5]. myArray[4]. myArray[3]. myArray[2]. myArray[1]. myArray[0]. Arrays and Arrays as Parameters. If an array is passed as a parameter to a function, copying each element is a waste of space. Solution: don’t pass around copies of arrays!

gbrooks
Download Presentation

Arrays and Arrays as Parameters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. myArray[5] myArray[4] myArray[3] myArray[2] myArray[1] myArray[0] Arrays and Arrays as Parameters • If an array is passed as a parameter to a function, copying each element is a waste of space. • Solution: don’t pass around copies of arrays! • Locally-instantiated arrays are already implemented in C as automatically de-referenced pointers. • Array parameters are also treated like automatically de-referenced pointers. myArray

  2. int age = 18; double weight = 121.5; char initial = ‘t’; int *pI; double *pD; /* Initialize pI, pC */ *pI; *pD; int i = 5; double weights[100]; char name[25]; weights[5]; weights[i]; weights void getName(char dest[]); void getInitial(char *pC); Arrays as Parameters • What does this mean to you, the programmer? • Changes made to an array parameter affect the caller’s original array! • There’s no difference in the syntax of the array’s use! • What are the types of the following expressions? Of the function parameters?

  3. Array Parameter Syntax • Because arrays are actually pointers, array parameters can use either pointer syntax or array syntax (but using array syntax for arrays is clearer). • If using array syntax, you do not need to include the array size in the parameter list (the compiler will ignore it). However, it’s useful for the programmer to know what the array size is.int myFunc(int x[]) vs. int myFunc(int *x) int myFunc(int x[SIZE]) vs. int myFunc(int x[]) • Array arguments cannot be passed with the brackets! int arr[ARRAY_SIZE]; myFunc(arr); /* Argument is an array of ints */ myFunc(arr[ARRAY_SIZE]); /* ERROR: type is int! */

  4. A Poll • How many of you could do Hw4 without using any functions? • Try to write everything in main() • What are some reasons for using functions? 1) 2) 3) 4) 5)

  5. Structs • Parallel arrays (Hw4) were kind of annoying, weren’t they? • Structs • A better way to group information • More reusable - you can think of the data as one unit, without needing to worry about the details of its internals • Unlike arrays, a struct can be composed of differing types • Functions • A better way to group operations • More reusable - you can think of the operations as one unit, without needing to worry about the details of its implementation • Structs are user-defined types which group together logically-related data. • Structs are used exactly like you use other types • Structs as variables • Pointers to structs • Structs as return values • etc.

  6. “Logically-related” data • A “time” struct might contain: • Month • Date • Hour • Minute • Second • Millisecond • What else would make a good struct?

  7. Struct Syntax These subcomponents are called fields. Every struct has its own copy of a field. To use a specific struct’s fields, you must refer to them by name A struct declaration: typedef struct { char month; int date; int hour double mSecond; } Date; Date d; /* What are the types? */ Date *pD; Do not forget the semi-colon!!

  8. Using structs Date myBirthday, summerBreak, today; • You can assign entire structs (unlike arrays) • summerBreak = today; • You access fields using the dot operator and the name of the field • summerBreak.month = ‘j’; • if(today.date == 17) • double timer = today.mSecond;

  9. Pointers to Structs A caveat … • . has greater precedence than * • *pD.hour behaves as if it were *(pD.hour) • (*pD).hour does the right thing …but the syntax is cumbersome • The solution is the -> operator. -> behaves like (*structPtr).field, but with a cleaner syntax (*pD).hourvs.ptrDT->hour

  10. Arrays and Structs • Arrays can be composed of any data type • So you can have arrays of structs, just like you would with ints, doubles, etc. • Structs can be composed of any data type(s) • This includes simple and aggregate data types • So you can create structs with array data members. typedef struct { char monthName[MAX_MONTH_LEN]; int date; int hour double mSecond; } Date; Date calendar[366];

  11. Ad infinitam In fact, the arrays and structs can be nested arbitrarily deep! typedef struct { Date d; char entry[100]; } PlannerEntry; typedef struct { PlannerEntry year[366]; double cost; } DayPlanner; typedef struct { player_info p; int superPower; QuizSection qs; } TA;

  12. Arrays and Structs (cont) • What are the types of … Date calendar[366]; Date today; calendar; calendar[356].mSecond; TA cse142[NUM_TAS], hannah; cse142[10].superPower; hannah.qs; PlannerEntry p; p.entry p.entry[i];

  13. Structs (and Arrays) as Parameters • Just like our basic types: • Structs can be arguments or return values to/from functions. Date getCurrTime(void); void printTime(Date d); • Structs, like other basic types, are passed by copy. Changing a struct parameter does not affect the original in the caller function. • Is this the same case as arrays? • What about arrays of structs? • What about structs with array fields?

More Related