Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

An introduction to pointers in c
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
C Course Lecture 4 This lecture we'll talk about: Multi-dimensional arrays. Pointer arithmetic. Pointers to structures. Multi-file programming. What is.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
ECE122 Feb. 22, Any question on Vehicle sample code?
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS102 Introduction to Computer Programming Chapter 9 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
CSC Programming for Science Lecture 36: Structures.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
POINTERS. // Chapter 3 - Program 1 - POINTERS.CPP #include int main() { int *pt_int; float *pt_float; int pig = 7, dog = 27; float x = , y = 32.14;
Variables and memory addresses
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
Week 6 - Friday.  What did we talk about last time?  Loop examples.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Pointers.
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Lecture 18 Arrays and Pointer Arithmetic
Arrays as pointers and other stuff
Presentation transcript:

Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Administrative stuff Quiz #6 was ?? Quiz #7 this Friday. Will tell you what to expect on Wednesday. Homework #5 Any questions?

Functions

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

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:

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);

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)

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'

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.

Returning on a function with “void” as return type void printPositiveInt(int a) { if(a < 1 ) { return; } printf("%i", a); }

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

Arrays as pointers

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] = ; printf(“%.5f\n", (*array)); //This prints Why?

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?

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)

Pointer arithmetic float array[100]; array[0] = f; array[2] = f; printf("%p: %.5f\n", array, *(array)); printf("%p: %.5f\n", array+2, *(array+2));

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);