Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...
|
|
- Dina Jefferson
- 6 years ago
- Views:
Transcription
1 Why Pointers Pointers They provide the means by which functions can modify arguments in the calling function. They support dynamic memory allocation. They provide support for dynamic data structures, such as binary trees and linked lists. 1 2 What Are Pointers? A pointer is the memory address of an object. A pointer variable is a variable that is specifically declared to hold a pointer to an object of its specified type. This address is the location of another object (typically another variable) in Memory address POINTERVariable Contents memory.... Pointer Declaration Two Pointer Operators General syntax: type *name; int *m; //The variable m can hold a //pointer to type int. char *ch; Address Operator: Dereference Operator: * Both & and * have a higher precedence than all other arithmetic operators except the unary minus, with which they share equal precedence. int count, *y, q; 5 6 & 1
2 & Operator The & is a unary operator that returns the memory address of its operand. & the address of m = &count; m receives the address of count. m count * Operator It is a unary operator that returns the value located at the address that follows. q = *m; * at address q receives the value at address m.? Put the value 10 into a variable called target. #include <stdio.h> int main(void) int target, source=10; int *m; m = &source; target = *m; printf("%d", target); return 0; Pointer Assignments You can use a pointer variable on the right-hand side of an assignment statement to assign its value to another pointer. When both pointers are the same type, the situation is straightforward. 10 #include <stdio.h> int main(void) int x = ; int *, *; = &x; = ; printf("%p %p",, ); printf(''%d %d\n", *, *); return 0; Illustrates Distinction between a pointer var value & a Dereference variable. main( ) int i = 777, *p = &i; printf ( value of i:%d\n, *p); printf ( Addr of i:%u or %p\n, p, p); Output value of i: 777 Addr of i: or dfffcfc u - (unsigned Dec integer) p - (whatever way is Default for system) - Here is Hex
3 Example 1 int i = 1, *j, *k; Assume addresses of i, j, k are respectively Byte addresses 10, 20, 30 i:10 j:20 k:30 1. j = &i; 1? int var Pointer var Pointer var 1 10? i:10 j:20 k:30? *j = 2; ? i:10 j:20 k:30 Stores 2 at the memory location pointed to by j. 3. i = *j + 1; ? i:10 j:20 k:30 * has higher precedence than +. Assigns to i the contents of the location pointed to by j, incremented by k = &i; i:10 j:20 k:30 5. printf ( %d, *k); int a=42, b=53, *, *; = &a; = ; = &b; Example 2 output: 3 15?? *? *? 16 int a=42, b=53, *, *;?? = ; = &a;? = &b; Example 3 int *, v1; v1 = 0; = &v1; * = 42; int a=8, b=; int *, *; v1?? *? = &a; = &b; = ; vs. * = *;
4 = ; before * = *; before 8 8 after after 8 1 Example 4 # include <stdio.h> main( ) int j, i, *intptr; scanf( %d%d, &i, &,j); intptr = i > j? &i:&j; printf( %d\n, *intptr); Address of the larger var is stored in? : then the larger number is output. 20 Example 5 int a=3,b=7,*p; p = &b; 3 7 a b p Pointer Initialization int i, *p = &i; correct *p=2**p a; printf ( b= %d\n, b); int *p = &i, i; sequence wrong. The object pointed to by p (i.e., b) is assigned the value of 2**p a. 1) 2 * *p 2 * 7 2) ) Which is assigned? b The variable must be defined before the address can be assigned p = &i; p = 0; p = NULL; p points to i Null is usually defined as 0. Pointer constant points nowhere. p = (int *) 1307; cast to pointer to int is an absolute address in memory. 23 int i = 3, j = 5, *p = &i, *q = &j, *r; double x; 3 5 i:10 j p:50 q? x 1) p = i + 7; ILLEGAL The only integer value that can be assigned to a pointer variable directly is the special value 0 (NULL). To assign any other value requires a cast (int *) (i + 7) r? 24 4
5 2) **&p All are unary operators. 1 &p - The address of p (50). 3) r = &x; Illegal Why? x is a double variable r is pointer to int i:10 p: *&p - The Addr stored at p (10) **&p The contents of the address (*&p) The contents of the variable pointed 25 to by p i.e., 3 4) 7 * *p / *q + 7 i:10 j 3 5 Dereference Right to Left p q 1. *q 5 2. *p * 3 [21] 4. 21/ ) *(r = &j) *= *p j - int var p - pointer to int j i r - pointer to int 5 3 r p 1. &j - Address of j 1 2. r = 1 r points to j 3. *p contents of thing pointed to by p i.e., 3 4. *( ) Location pointed to by r, i.e., j. 5. *= *r *= *p; *r = *r * *p; 27 int *v=(int *)100; assume int are 2 bytes long. Pointer Arithmetic Byte Address. 100 v 102 v v v v char *ch= (char *)3000; Only two operations are allowed. Addition and subtraction. int *i= (int *)3000; ch ch+1 ch+2 ch+3 ch+4 ch I i+1 i+2 e.g. p++; p--; = +12; Cannot multiply or divide pointers. Cannot add two pointers. Cannot add or subtract type float or double to or from pointers
6 Call by Reference Passing a ADDRESS of the argument to a formal parameter which is a POINTER of the same type as the argument. Code within the function can change the value of the argument passed to the function. Steps 1) Declare a function parameter to be a pointer. 2) Pass an address as an argument when the function is called. 3) Use the dereferenced pointer in the function body to reference the argument in the calling function #include <stdio.h> void swap(int *x, int *y); int main (void) int i=10, j=20; printf("i and j before swapping: %d %d\n", i, j); swap(&i, &j); /* pass the addresses of i and j */ void swap( int *x, int *y ) int temp; temp=*x; /* save the value at address x */ *x =*y; /* put y into x */ *y=temp; /* put x into y */ printf("i and j after swapping: %d %d\n", i, j); return 0; Every variable or function in C has 2 attributes: Storage Classes Type Storage Class Storage Class : determines how the compiler allocates memory to that variable. Auto Extern Register Static
7 Storage Class of a C object defines its: 1) Spatial Territory- Defines where it can be referred to in the program. Its Scope or Visibility. 2) Temporal Authority- Defines when an object is available. The Extent to which it exists. 37 Auto Variables The storage class auto can only be used in a block: it cannot be used at the global level. 1) Spatial Limitation (scope): The auto variable is limited to the block in which it is defined and its sub-blocks. LOCAL. 2) Temporal Limitation (extent): Are alive only when the block in which they are defined is running. Variable does not exist before block execution, when block terminates it dies(mortal). 38 Impact of Auto Auto is the default SC for local vars because it makes the most efficient use of memory. Good programmers use Auto when ever possible because their lifetime is brief and their scope is limited to one function. They provide an effective way to isolate the actions of each program unit from the others. auto Most common of the four storage classes. Variables declared within function bodies are auto by default. Thus auto is rarely explicitly specified. auto int a,b,c; auto float f; 3 40 Unintentional interference between functions is minimized and, making it much easier to debug large programs. Constant allocation and de-allocation of auto vars does add some overhead to the the execution time, but since is done quite efficiently, the time wasted is minimal and the advantages of the added modularity are great. 41 When a block is entered, the system allocates memory for the auto variables. When Block is exited the variables are released and their values lost. IF re-enter the Block? Memory is re-allocated. 42 7
8 Call by Reference with Auto Parameters 1) When the block is suspended, such as when it calls another function, a variable can still be accessed and changed(e.g. if it is used as an actual argument in a func call). 2) It can still be referenced and changed through a pointer to it. When the block becomes active again, the variable becomes active i.e., can directly reference it by name.) Extern Primary purpose is to make variables visible to other compilation units. Must be defined in the global area of a program, that is outside of any block. All global variables( i.e., those defined outside any function) are EXTERN by default.!!!!! EXTERN variables are DANGEROUS and should be used with CAUTION!!!!! Extern Characteristics Spatial Limitation(global): It is visible from its definition to the end of the program. It can be referred to and changed by all blocks that come after it as well as by other programs that are aware of its existence. Variables declared outside of a function have extern storage class by default. i.e., even if don t use keyword extern. Extern Var s are initialized to 0 automatically. Temporal Limitation(Immortal): The whole life of the program. From the time of its allocation till the end of the program. 45 extern - Look for the variable elsewhere either in this or another file. 46 Register Storage Class Variables so declared should be stored in high speed memory i.e., Registers(cpu) provided if is possible to do so. Defaults to Auto if register not available 47 Typically, the compiler has only a registers available. few such Many are required for system use & can t be allocated otherwise: * Base Addr of program * Return Addr after pgm execution ends, * Instruction reg, program counter, etc
9 It is an attempt to improve execution speed. Most frequently accessed variable loop control variable or function parameters. register int i ; for (i = d, i <LIMIT, ++i) Recommendation Don t Use The standard states that you can t compute the address of a register. That means you can t use the address operator or indirection operator(*) with a Register variable. If storage class is present & Type is absent - get int. register i ; 4 Secondly, the compiler may ignore your recommendation. 50 Static Storage Class A static variable can be defined inside a block(local) or in the global area of the program. Its characteristics differ slightly depending on where it is defined. We will look at each separately. 51 Local Static Variables Static variable is defined inside a block: 1) Spatial Limitation: LOCAL, undefined outside the block. 2) Temporal Limitation (Extent): Immortal. It lives as if it were a global variable. What are the consequences of the immortal extent? 52 Locally defined Static variables are one of the truly significant features of the C language. 1) Allow a Local variable to retain its previous value when a block is re-entered. (Not the case with Auto) 2) In other languages, these variables must be declared globally, which reduces the benefits of data hiding and isolation. 3) The variable will be automatically initialized to 0 when allocated. ONCE 53 Void f(void) static int cnt = 0; ++ cnt; if (cnt % 2 == 0) else st Time Function Invoked cnt is set to 0. Upon EXIT cnt NOT cleared from memory invocation, retains previous value. 54
10 Global Static Variables Global variables - that is, variables defined outside the scope of all blocks - can be referred to from other compilation units. To prevent a global variable from being exported to the LINKER for reference by other compilation units, it can be declared static. 55 Global Static Characteristics: 1) Spatial Limitation(scope): Global. Visible from its definition to the end of the compile unit. Guarantees that the scope does not extend beyond its spatial area, i.e., the current complilation unit. 2) Temporal Limitation(extent) Immortal. Same as local static, it lives as global variables live. 56 Static Extern Variable Restricted in scope to the remainder of the file in which they are defined. Not known to other files(compilation units). void f(void) /* v not available here */ static int v; void g(void) /* v can be used here */ A Function can be Static: The scope of the function is limited to one file only(privacy concern). Is known throughout the entire file. void f(int a).. /* g() is available here, but not in other files */ static int g(void) Default Initialization Extern & Static vars are initialized to 0 by the system. Auto & Register are usually not initialized by system. Start will garbage. 5 Type Qualifiers Two type qualifiers: const volatile They are used in declarations and must be coded after the storage class but precede the type names that they qualify. static const int k = 3; Control how variables may be accessed or modified
11 const Variables of type const may not be changed by your program. The compiler is free to place variables of this type into read-only memory (ROM). #include <stdio.h> void sp_to_dash( const char *str); int main(void) sp_to_dash("this is a test"); return 0; const int a=10; const int a=7; int *p=&a; //wrong const int a=7; const int *p=&a; //correct 61 void sp_to_dash(const char *str) char *here = str; while(*here) if(*here = = ' ') printf("%c", '-'); else printf("%c", *here); here++; WHY? 62 Volatile The modifier volatile tells the compiler that a variable's value may be changed in some unspecified way by the hardware. For example, a global variable's address may be passed to the operating system's clock routine and used to hold the system time. Programmers use Volatile objects in advanced systems programming(beyond our scope). Declaration vs. Definition In General, a Declaration is a program element that provides a compiler with essential information or useful advice. When you specify the type of a variable or parameter you are said to declare variable or parameter A definition causes a compiler to set aside memory at compile time (every variable definition is a declaration) - not always the reverse. e.g. when you introduce a variable outside of any function definition (global variable), you both declare and define the variable, because you have: a) informed the compiler about the variable s type. b) caused the compiler to set aside memory When you introduce a variable inside a function definition(local), you only declare that variable, because the compiler does not set aside memory for such variable at compile time [memory in stack frame]. Functions are both declared & defined Because: a) must specify return type. b) compiler sets aside memory for functions at compile time. for the variable at compile time
[0569] p 0318 garbage
A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the
More informationVariables and Operators 2/20/01 Lecture #
Variables and Operators 2/20/01 Lecture #6 16.070 Variables, their characteristics and their uses Operators, their characteristics and their uses Fesq, 2/20/01 1 16.070 Variables Variables enable you to
More informationC Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,
More informationGoals of this Lecture
C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing
More informationLanguage comparison. C has pointers. Java has references. C++ has pointers and references
Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified
More informationA Fast Review of C Essentials Part II
A Fast Review of C Essentials Part II Structural Programming by Z. Cihan TAYSI Outline Fixed vs. Automatic duration Scope Global variables The register specifier Storage classes Dynamic memory allocation
More informationVariation of Pointers
Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before
More informationPointers. 10/5/07 Pointers 1
Pointers 10/5/07 Pointers 1 10/5/07 Pointers 2 Variables Essentially, the computer's memory is made up of bytes. Each byte has an address, associated with it. 10/5/07 Pointers 3 Variable For example 1:#include
More informationArrays and Pointers. CSE 2031 Fall November 11, 2013
Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example
More informationPointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays
EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and
More informationEM108 Software Development for Engineers
EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and
More informationUnit 7. Functions. Need of User Defined Functions
Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have
More informationCSE 230 Intermediate Programming in C and C++ Functions
CSE 230 Intermediate Programming in C and C++ Functions Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse230/ Concept of Functions
More informationArrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.
Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More informationComputer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary
GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis
More informationPROGRAMMAZIONE I A.A. 2017/2018
PROGRAMMAZIONE I A.A. 2017/2018 A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. DECLARING POINTERS POINTERS A pointer represents both
More informationPointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017
Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();
More informationProcedural programming with C
Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical
More informationCOMP26120: Pointers in C (2018/19) Lucas Cordeiro
COMP26120: Pointers in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Organisation Lucas Cordeiro (Senior Lecturer, FM Group) lucas.cordeiro@manchester.ac.uk Office: 2.44 Office hours: 10-11
More informationPointer Basics. Lecture 13 COP 3014 Spring March 28, 2018
Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory
More informationC Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations
55:017, Computers in Engineering C Pointers C Pointers Powerful C feature but challenging to understand Some uses of pointers include Call by reference parameter passage Dynamic data structures Data structures
More informationLectures 5-6: Introduction to C
Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most
More informationStorage class in C. Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables.
1 Storage class in C Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables. 2 Few terms 1. Scope: the scope of a variable determines
More informationCSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions
CSE 2421: Systems I Low-Level Programming and Computer Organization Functions Read/Study: Reek Chapters 7 Gojko Babić 01-22-2018 Predefined Functions C comes with libraries of predefined functions E.g.:
More informationOperators And Expressions
Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication
More informationFirst of all, it is a variable, just like other variables you studied
Pointers: Basics 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 address (rather than the value)
More informationM.EC201 Programming language
Power Engineering School M.EC201 Programming language Lecture 13 Lecturer: Prof. Dr. T.Uranchimeg Agenda The union Keyword typedef and Structures What Is Scope? External Variables 2 The union Keyword The
More informationReview of the C Programming Language for Principles of Operating Systems
Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights
More informationDeclaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:
Declarations Based on slides from K. N. King Declaration Syntax General form of a declaration: declaration-specifiers declarators ; Declaration specifiers describe the properties of the variables or functions
More informationStorage class and Scope:
Algorithm = Logic + Control + Data Data structures and algorithms Data structures = Ways of systematically arranging information, both abstractly and concretely Algorithms = Methods for constructing, searching,
More informationFundamentals of Programming Session 12
Fundamentals of Programming Session 12 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines
More informationPointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables
Chapter 11 Pointers The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into bytes, with each byte capable
More informationOutput of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8
Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within
More informationby Pearson Education, Inc. All Rights Reserved.
Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]
More informationA Fast Review of C Essentials Part I
A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types
More informationCS113: Lecture 5. Topics: Pointers. Pointers and Activation Records
CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",
More informationLab 3. Pointers Programming Lab (Using C) XU Silei
Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions
More informationBil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7
Strings and Clases BIL104E: Introduction to Scientific and Engineering Computing Lecture 7 Manipulating Strings Scope and Storage Classes in C Strings Declaring a string The length of a string Copying
More informationCSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018
CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values
More informationC: How to Program. Week /Mar/05
1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers
More informationcalling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!
Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns
More informationLESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:
LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers
More informationINITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS
INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS Pages 792 to 800 Anna Rakitianskaia, University of Pretoria INITIALISING POINTER VARIABLES Pointer variables are declared by putting
More informationQUIZ How do we implement run-time constants and. compile-time constants inside classes?
QUIZ How do we implement run-time constants and compile-time constants inside classes? Compile-time constants in classes The static keyword inside a class means there s only one instance, regardless of
More informationComputers Programming Course 5. Iulian Năstac
Computers Programming Course 5 Iulian Năstac Recap from previous course Classification of the programming languages High level (Ada, Pascal, Fortran, etc.) programming languages with strong abstraction
More informationFeatures of C. Portable Procedural / Modular Structured Language Statically typed Middle level language
1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC
More informationFunctions. (transfer of parameters, returned values, recursion, function pointers).
Functions (transfer of parameters, returned values, recursion, function pointers). A function is a named, independent section of C/C++ code that performs a specific task and optionally returns a value
More informationChapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic
More informationProgramming for Engineers Iteration
Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers
More informationCS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor
CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static
More informationEL6483: Brief Overview of C Programming Language
EL6483: Brief Overview of C Programming Language EL6483 Spring 2016 EL6483 EL6483: Brief Overview of C Programming Language Spring 2016 1 / 30 Preprocessor macros, Syntax for comments Macro definitions
More informationFunctions and Recursion
Functions and Recursion CSE 130: Introduction to Programming in C Stony Brook University Software Reuse Laziness is a virtue among programmers Often, a given task must be performed multiple times Instead
More information15 FUNCTIONS IN C 15.1 INTRODUCTION
15 FUNCTIONS IN C 15.1 INTRODUCTION In the earlier lessons we have already seen that C supports the use of library functions, which are used to carry out a number of commonly used operations or calculations.
More informationFunction Call Stack and Activation Records
71 Function Call Stack and Activation Records To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Students
More informationFunctions. Chapter 5
Functions Chapter 5 Function Definition type function_name ( parameter list ) declarations statements For example int factorial(int n) int i, product = 1; for (i = 2; I
More informationPOINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory
More informationReview of the C Programming Language
Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the
More informationIMPORTANT QUESTIONS IN C FOR THE INTERVIEW
IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.
More informationLecture 3: C Programm
0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory
More informationProgramming. Pointers, Multi-dimensional Arrays and Memory Management
Programming Pointers, Multi-dimensional Arrays and Memory Management Summary } Computer Memory } Pointers } Declaration, assignment, arithmetic and operators } Casting and printing pointers } Relationship
More informationBasic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces
Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program
More informationFixed-Point Math and Other Optimizations
Fixed-Point Math and Other Optimizations Embedded Systems 8-1 Fixed Point Math Why and How Floating point is too slow and integers truncate the data Floating point subroutines: slower than native, overhead
More informationCprE 288 Introduction to Embedded Systems Exam 1 Review. 1
CprE 288 Introduction to Embedded Systems Exam 1 Review http://class.ece.iastate.edu/cpre288 1 Overview of Today s Lecture Announcements Exam 1 Review http://class.ece.iastate.edu/cpre288 2 Announcements
More informationB.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University
Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are
More informationCh. 3: The C in C++ - Continued -
Ch. 3: The C in C++ - Continued - QUIZ What are the 3 ways a reference can be passed to a C++ function? QUIZ True or false: References behave like constant pointers with automatic dereferencing. QUIZ What
More informationModel Viva Questions for Programming in C lab
Model Viva Questions for Programming in C lab Title of the Practical: Assignment to prepare general algorithms and flow chart. Q1: What is a flowchart? A1: A flowchart is a diagram that shows a continuous
More informationOutline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary
Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an
More informationCS 61c: Great Ideas in Computer Architecture
Arrays, Strings, and Some More Pointers June 24, 2014 Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address
More informationQUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:
QUIZ 1. Explain the meaning of the angle brackets in the declaration of v below: This is a template, used for generic programming! QUIZ 2. Why is the vector class called a container? 3. Explain how the
More informationBasics of Programming
Unit 2 Basics of Programming Problem Analysis When we are going to develop any solution to the problem, we must fully understand the nature of the problem and what we want the program to do. Without the
More informationArrays and Pointers. Lecture Plan.
. Lecture Plan. Intro into arrays. definition and syntax declaration & initialization major advantages multidimensional arrays examples Intro into pointers. address and indirection operators definition
More informationUNIT III (PART-II) & UNIT IV(PART-I)
UNIT III (PART-II) & UNIT IV(PART-I) Function: it is defined as self contained block of code to perform a task. Functions can be categorized to system-defined functions and user-defined functions. System
More informationProgramming in C - Part 2
Programming in C - Part 2 CPSC 457 Mohammad Reza Zakerinasab May 11, 2016 These slides are forked from slides created by Mike Clark Where to find these slides and related source code? http://goo.gl/k1qixb
More informationCS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers
CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers Instructor: Justin Hsia 6/20/2012 Summer 2012 Lecture #3 1 Review of Last Lecture C Basics Variables, Functions, Flow Control,
More informationExpressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators
Expressions 1 Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical
More informationQuiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.
Quiz 0 Answer Key Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d True or False. 6. T or F 7. T 8. F 9. T 10. T or F Itching for Week 0? 11. 00011001 + 00011001
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];
More informationC: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to
More informationMYcsvtu Notes LECTURE 34. POINTERS
LECTURE 34 POINTERS Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain
More informationVariables Data types Variable I/O. C introduction. Variables. Variables 1 / 14
C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition
More informationArrays and Pointers in C. Alan L. Cox
Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including
More informationAnnotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments
Variables, Data Types, and More Introduction In this lesson will introduce and study C annotation and comments C variables Identifiers C data types First thoughts on good coding style Declarations vs.
More informationLecture 2: C Programm
0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes
More informationParameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265
Parameter passing Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C implements call-by-value parameter passing int a =
More informationAt the end of this module, the student should be able to:
INTRODUCTION One feature of the C language which can t be found in some other languages is the ability to manipulate pointers. Simply stated, pointers are variables that store memory addresses. This is
More informationArrays, Pointers and Memory Management
Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by
More informationAdministrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6
CMSC 313 Introduction to Computer Systems Lecture 8 Pointers, cont. Alan Sussman als@cs.umd.edu Administrivia Project 2 posted, due October 6 public tests s posted Quiz on Wed. in discussion up to pointers
More informationIntroduction to Scientific Computing and Problem Solving
Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:
More informationPointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology
Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationCS240: Programming in C
CS240: Programming in C Lecture 5: Functions. Scope of variables. Program structure. Cristina Nita-Rotaru Lecture 5/ Fall 2013 1 Functions: Explicit declaration Declaration, definition, use, order matters.
More informationDynamic memory allocation (malloc)
1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration
More informationProblem Solving and 'C' Programming
Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 15: Files and Preprocessor Directives/Pointers 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example
More informationLecture 04 Introduction to pointers
Lecture 04 Introduction to pointers A pointer is an address in the memory. One of the unique advantages of using C is that it provides direct access to a memory location through its address. A variable
More informationSYSC 2006 C Winter 2012
SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization
More informationArrays and Pointers in C & C++
Arrays and Pointers in C & C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++,
More informationCS Programming In C
CS 24000 - Programming In C Week 16: Review Zhiyuan Li Department of Computer Science Purdue University, USA This has been quite a journey Congratulation to all students who have worked hard in honest
More information