Download presentation
Presentation is loading. Please wait.
Published byAdrian Walker Modified over 9 years ago
1
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ
2
Administrative stuff Quiz #6 was ?? Quiz #7 this Friday. Will tell you what to expect on Wednesday. Homework #5 Any questions?
3
Functions
4
Declaring functions Declaration Return type Name/identifier Parameters Function content ( ) { } Example: int multiply(int a, int b) { return a*b; } Declaring a function is “creating a hammer” but is not “using the hammer” No code gets executed from defining a function
5
Calling a function How do we use the “hammer”? Once we have one defined: int multiply(int a, int b) { return a*b; } We can call it by it’s name and giving values to it’s parameters:
6
Calling a function How do we use the “hammer”? Once we have one defined: int multiply(int a, int b) { return a*b; } We can call it by it’s name and giving values to it’s parameters: multiply(3,4);
7
Returning values We can give a function as many return statements as we want. For example: char toUpperCase(char c) { switch(c) { case 'a': return 'A'; case ‘b': return ‘B'; case ‘c': return ‘C'; case ‘d': return ‘D'; … case ‘z': return ‘Z'; } } That’s a very inefficient function (only used for demonstration of the concept) They only have to have the same type (matching the return type)
8
Returning values A call to a function will return the value next to the return statement that it’s execution reaches. What does that mean? If we execute toUpperCase('k'); then it’s value will be 'K' Then if we do: char uppercase = toUpperCase('h'); //uppercase will have a value of 'H'
9
Returning on a function with “void” as return type We talked about void being used when we don’t want to return anything. Functions returning void can still have return statements. They don’t get a value They are simply: return; These return statements just stop the execution of the function Useful to handle errors.
10
Returning on a function with “void” as return type void printPositiveInt(int a) { if(a < 1 ) { return; } printf("%i", a); }
11
What can the content of a function be? Statements Variable declarations Variable assignments If statements Switch statements Loops Return statements Calls to other functions What cannot be part of the content? A declaration of another function
12
Arrays as pointers
13
Arrays vs. pointers I have said it before but in terms of their structure in memory: Arrays and pointers are the same thing. Notation can be interchanged and C won’t complain about it. For example: float array[100]; array[0] = 3.14159; printf(“%.5f\n", (*array)); //This prints 3.14159 Why?
14
Arrays vs. pointers Our array definition is technically a pointer to the first element of the array. So when I write (*array) on the previous example, it goes and grabs the first element. Actually (*array) and array[0] are the exactly same piece of code. Think about them as synonyms in a way… Is there a synonym for array[2]? Yes! *(array+2) The parenthesis are important… What does it mean to add 10 to a pointer?
15
Pointer arithmetic and offsets Remember that pointers are memory addresses Those memory addresses are usually in bytes. If we added 2, what exactly does that mean? Would it be 2 bytes? Nope – Moving one byte would lead to numbers that don’t really make sense. Pointers do have a type. In our example we have a float array, so the type is float* Floats have a certain size 4 bytes. So adding 1 to a pointers moves the pointer to the next float (not the next byte)
16
Pointer arithmetic float array[100]; array[0] = 3.14159f; array[2] = 0.17170f; printf("%p: %.5f\n", array, *(array)); printf("%p: %.5f\n", array+2, *(array+2));
17
Does that have an implication for malloc? malloc: Dynamic memory allocation float *float_ptr = (float*)malloc(sizeof(float)); Now… what happens if I write it like this: float *float_arr = (float*)malloc(sizeof(float)*100); Or even better int size = 100; float *float_arr = (float*)malloc(sizeof(float)*size);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.