CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.

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

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Lectures 10 & 11.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Programming Languages and Paradigms The C Programming Language.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
CS 450 MPX P ROJECT Introduction to MPX. I NTRODUCTION TO MPX The MPX module is divided into six modules Module R1: User Interface Module R2: Process.
Week 5 - Wednesday.  What did we talk about last time?  Arrays.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 10.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Pointers and Arrays C and Data Structures Baojian Hua
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Guide To UNIX Using Linux Third Edition
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C-strings Array with base type char One character per indexed variable
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays and Strings in Assembly
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
C Basics.
Visit for more Learning Resources
Learning Objectives What else in C++ Bitwise operator
CPS120: Introduction to Computer Science
Course Outcomes of Programming In C (PIC) (17212, C203):
Variables and Constants
Presentation transcript:

CS 450 MPX P ROJECT A Quick C Review

P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will need Wikipedia: In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. A pointer is a variable that holds an address to another memory location, not an actual value. To declare a variable as a pointer, use the *: int *myIntPointer; The myIntPointer variable will hold an address to a memory location that contains a value of type int

P OINTERS IN C To access the value held in a pointer, you need to dereference it int *myIntPointer; //declare pointer *myIntPointer = 30; //assign value to the referenced address *myIntPointer++; //access that value If you code myIntPointer = 30; you are not changing the value referenced by myIntPointer, you are changing the address myIntPointer references To access the address of a variable, use &: int myInt = 8;// new int variable, not a pointer myIntPointer = & myInt; //copy address of myInt into myIntPointer

C OMMON P OINTER E RRORS int *a = 8; a = 19; //should say *a = 19. int b = 20; a = b; //should be *a = b to set the value referenced by ‘a’ equal to the value of ‘b’ OR a = &b to set the address referenced by ‘a’ = the address of ‘b’.

S TRINGS I N C Strings in C are stored as character arrays and must be “null terminated”. This means that the last character in the string should be ‘\0’ Declare a string as a character array and give it a maximum size. Use this method if you do not know what you wish to put in the String at compile time: char[20] myString; Declare a string as a character pointer. Use this method if you know the exact string you wish to store in this character array at compile time and do not plan on storing a longer string in it later: char* myString = “hello world\0”;

C OPYING AND C OMPARING S TRINGS Copy the value of one string to another: Do NOT use myString1 = myString2 or *myString1 = *myString2 Use strcpy: strcpy(myString1, myString2); This will copy the value held in myString2 into myString1 Compare two strings Do NOT use myString1 == myString2, etc. Use strcmp(myString1, myString2); If equal, returns 0, otherwise returns something else.

C S TRINGS Potential Problems: You declare a character array of size X, then store a string of size > X in that array. You declare a character pointer and later try to store a string in it (since you declared it as a * with no initial value, no memory is allocated for the string): char *myCharPointer; …… code …. *myCharPointer = “hello world\0”; Not null terminating your strings- printf, strcpy, strcmp find the end of a string by looking for \0.

C OMPILER D IRECTIVES Use “#” to give specific instructions to the compiler #include: essentially directs the compiler to “copy and paste” the contents of a file into another file #include “MyHeaderFile.H” #include Use “…” for header files you create, for system header files NEVER include “.C” files #define: symbolic constants #define MYCONSTANT 40 Typically in all upper case In this example, every time you type “MYCONSTANT” in your code, the compiler will replace it with “40” Allows you to define at compile time constants that you will use throughout your code Use for array sizes, giving numeric values to categories, and register addresses

H EADER F ILES C programs are comprised of two types of files Source Files (.C) Header Files (.H) Source files contain all of your source code Header files contain prototypes for all of your functions, definitions of structures, symbolic constants, and global variable declarations. For each source (.C) file, there should be a header file (.H) You should link the source files together, and include header files in the source files

P ROPER U SE OF H EADER F ILES If the function prototypes contained in the header files are included more than once, errors will be generated by the compiler. To avoid this problem, wrap the header files in an if…end if statement: #ifndef MYHFILE_ #define MYHFILE_ ….. Header file contents ….. #endif

O NLINE R ESOURCES In depth tutorials and reviews of just about every C topic you will need in MPX, including basic syntax, file I/O, C-strings, pointers, function pointers, structures, linked lists, and bitwise operators. m Pointers and arrays