Arrays and Functions in Programming

arrays and funcs n.w
1 / 9
Embed
Share

Dive into the world of arrays and functions, learning about their structure, memory allocation, and passing arrays into functions efficiently. Explore how arrays organize data and how functions can interact with them for effective programming. Get insights into memory management and pointers related to arrays in programming languages.

  • Programming
  • Arrays
  • Functions
  • Memory
  • Pointers

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. ARRAYS (AND FUNCS) (STILL REVIEW) (STILL REVIEW)

  2. WHAT IS AN ARRAY? An ordered list of elements of the same type An ordered list of elements of the same type Ordered meaning there is an order (something occurs first, then second, then third, etc.) Ordered meaning there is an order (something occurs first, then second, then third, etc.) NOT Sorted (necessarily!) NOT Sorted (necessarily!) Of the same Of the same type type: : ints ints, strings, doubles, or even class objects , strings, doubles, or even class objects Every element is the same Every element is the same type type Arrays are no longer primitive types! Arrays are a simple data structure! int arr[5] = {3,2,7,8,2}; //space for 5 ints, 3 comes before 2, etc. int arr2[] = {7,8,2,4}; //space for 4 ints, 4 comes after 2, etc. int arr3[5]; //space for 5 ints, arr3[2] = 4; //the 3rd space holds 4

  3. VARIABLES (QUICK REVIEW) int x = 3; cout << x << endl; Location in memory (i.e., 0x32ef11) Name of location in memory: x Value at location: 3 If we want to get the address in memory instead of the value: cout << &x << endl; //will print out 0x32ef11 (address of x in memory) Variables that hold addresses: int *y = &x; // y points to x, aka, y holds x s address

  4. M O R E S P E C I F I C A L LY, W H AT I S A N A R R AY ? A variable A variable The variable The variable HOLDS THE ADDRESS IN MEMORY OF THE FIRST VALUE OF THE ARRAY. . The rest of the values follow sequentially in memory The rest of the values follow sequentially in memory That s it That s it It is in essence a pointer! It is in essence a pointer! Memory 3 3 2 2 7 7 8 8 int arr[5] = {3,2,7,8}; 0x32e4 0x32e8 0x32ec 0x32f0 arr arr holds 0x32e4 holds 0x32e4 The address of the first value The address of the first value cout cout << << arr arr << Gives you the same as: Gives you the same as: cout cout << & << &arr arr[0] << << endl endl; //address of first value (0x32e4) ; //address of first value (0x32e4) [0] << endl endl; // address of first value (0x32e4) ; // address of first value (0x32e4)

  5. PASSING ARRAYS INTO FUNCTIONS: Given the following array: int x[4000000]; for (int i = 0; i < 4000000; i++) { x[i] = i; } In terms of memory space, do we want to make a new, local copy of the array when we call a function? To get the address of the array (aka a pointer to the array): int arr[4] = {3,2,1,4}; cout << &arr[0] << endl; //address of first value in array cout << arr << endl; //same value, address of array, of where first value in array is

  6. ARRAYS & FUNCTIONS: Note: no straightforward way of getting size of an array. int main() { int x[4]= {3,2,4,1}; double k = getAverage(&x[0],4); //gets the address of the first value in the array Easiest way: pass the length of the array along with a pointer to the array (aka the address of the array) // Or double l = getAverage(x,4); // the address of the first value in the array cout << k << endl; } // main Function definition: double getAverage(int *arr, int size) // a pointer, a variable that holds an address { int sum = 0; for (int i = 0; i < size; i++) sum += arr[i]; // now, in essence, the value at (the address of arr + i) { } //for double avg = double(sum) / size; return avg; }//main Alternative: double getAverage(int arr[], int size) { //holds address of the first value in the array

  7. RETURNING ARRAYS: When you call a function with an array as a parameter: You pass in the address of the first value of the array: e.g., int arr[4]={3,4,2,1}; callingFunction(arr,4); //arr is address of first value of array in memory Or callingFunction(&arr[0],4); //again, address of first value of array in memory When you return an array from a function: Same thing! Return the address So the return TYPE must be an address: int *callingFunction(int arr[], int size){ // return type: address! arr[2] = 7; return arr; // returning address of first value of array! }

  8. YOU CANNOT CREATE AN ARRAY INSIDE OF A FUNCTION AND RETURN IT FROM THE FUNCTION (YET): RETURNING ARRAYS: E.G., int *createArray(int size); int main() { int *a; //a holds an address that points to an int (or the first in a list of ints) a = createArray(4); cout << a[x] << endl; return 0; }//main int *createArray(int size) // what is returned??? { int r[4] = {3,4,8,6}; // Creating an array return r; // or return &r[0]; - returns address of the first value of array } //createArray WHY? Yes, we returned the address of the first value of the array. BUT: When CreateArray ended, all the variables created in the function are freed up. So the array itself went away Even though we returned the array s address, the array itself is gone Gibberish will be printed.

  9. Arrays are sequential in memory An array variable actually holds the address of the first value in memory So when we pass an array variable into function, we are passing in the address In essence, the default with arrays is call by pointer If you change the array, it will stay changed outside the function call by pointer TA K E A W AY S : Returning arrays: Return type is an address (pointer) Again, returning the address of the first value of an arr

More Related Content