Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.

Slides:



Advertisements
Similar presentations
Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
Advertisements

An introduction to pointers in c
Lecture 20 Arrays and Strings
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.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Homework #4CS-2301 B-term Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Week 3 - Monday.  What did we talk about last time?  Math library  Preprocessor directives  Lab 2.
Microsoft Visual C++.NET Chapter 61 Memory Management.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Arrays: Part Opening Discussion zWhat did we talk about last class? zWhat do you think the picture of memory looks like when we declare.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
System Programming Practical Session 7 C++ Memory Handling.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Today’s Material Strings Definition Representation Initialization
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Week 5 - Monday.  What did we talk about last time?  Processes  Lab 4.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Lesson #8 Structures Linked Lists Command Line Arguments.
A bit of C programming Lecture 3 Uli Raich.
Motivation and Overview
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Command Line Arguments
Command line arguments
Command Line Arguments
Instructor: Ioannis A. Vetsikas
CSC 253 Lecture 8.
Command Line Parameters
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Understanding Program Address Space
Strings and Pointer Arrays
Functions Reasons Concepts Passing arguments to a function
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Week 6 - Friday

 What did we talk about last time?  Pointers  Passing values by reference

 Just as we can declare a pointer that points at a particular data type, we can declare a pointer to a pointer  Simply add another star int value = 5; int* pointer; int** amazingPointer; pointer = &value; amazingPointer = &pointer; int value = 5; int* pointer; int** amazingPointer; pointer = &value; amazingPointer = &pointer;

 Well, a pointer to a pointer ( ** ) lets you change the value of the pointer in a function  Doing so can be useful for linked lists or other situations where you need to change a pointer  Pointers to pointers are also used to keep track of dynamically allocated 2D arrays

 Can you have a pointer to a pointer to a pointer to a pointer… ?  Absolutely!  The C standard mandates a minimum of 12 modifiers to a declaration  Most implementations of gcc allow for tens of thousands of stars  There is no reason to do this, however int*********** madness;

Three Star Programmer A rating system for C-programmers. The more indirect your pointers are (i.e. the more "*" before your variables), the higher your reputation will be. No-star C-programmers are virtually non-existent, as virtually all non-trivial programs require use of pointers. Most are one-star programmers. In the old times (well, I'm young, so these look like old times to me at least), one would occasionally find a piece of code done by a three-star programmer and shiver with awe. Some people even claimed they'd seen three-star code with function pointers involved, on more than one level of indirection. Sounded as real as UFOs to me. Just to be clear: Being called a ThreeStarProgrammer is usually not a compliment. From C2.com Three Star Programmer A rating system for C-programmers. The more indirect your pointers are (i.e. the more "*" before your variables), the higher your reputation will be. No-star C-programmers are virtually non-existent, as virtually all non-trivial programs require use of pointers. Most are one-star programmers. In the old times (well, I'm young, so these look like old times to me at least), one would occasionally find a piece of code done by a three-star programmer and shiver with awe. Some people even claimed they'd seen three-star code with function pointers involved, on more than one level of indirection. Sounded as real as UFOs to me. Just to be clear: Being called a ThreeStarProgrammer is usually not a compliment. From C2.com

 Before we get into command line arguments, remember the definition of a string  An array of char values  Terminated with the null character  Since we usually don't know how much memory is allocated for a string (and since they are easier to manipulate than an array), a string is often referred to as a char*  Remember, the only real difference between a char* and a char array is that you can't change where the char array is pointing

 Did you ever wonder how you might write a program that takes command line arguments?  Consider the following, which all have command line arguments: ls –al chmod a+x thing.exe diff file1.txt file2.txt gcc program.c -o output ls –al chmod a+x thing.exe diff file1.txt file2.txt gcc program.c -o output

 Command line arguments do not come from stdin  You can't read them with getchar() or other input functions  They are passed directly into your program  But how?!

 To get the command line values, use the following definition for main()  Is that even allowed?  Yes.  You can name the parameters whatever you want, but argc and argv are traditional  argc is the number of arguments (argument count)  argv are the actual arguments (argument values) as strings int main(int argc, char** argv) { return 0; } int main(int argc, char** argv) { return 0; }

 The following code prints out all the command line arguments in order on separate lines  Since argv is a char**, dereferencing once (using array brackets), gives a char*, otherwise known as a string int main(int argc, char** argv) { int i = 0; for( i = 0; i < argc; i++ ) printf("%s\n", argv[i] ); return 0; } int main(int argc, char** argv) { int i = 0; for( i = 0; i < argc; i++ ) printf("%s\n", argv[i] ); return 0; }

 Dynamic memory allocation

 Keep reading K&R chapter 5  Keep working on Project 3