# Lecture 9: Introduction to pointers

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Handout 10 24/03/03 1 Pointers to variables Lecture 9: Introduction to pointers A pointer is nothing but a variable that contains an address of a location in memory. We can declare the variable ptr as a pointer to an integer as int *ptr; You can think of this as being a new type, which is (int *), which means pointer to an integer. When it is declared, a pointer does not have a value, which means that it does not yet store an address in memory, and hence it does not point to an address in memory. Let s say we want to have the pointer that we defined point to the address of another integer. This is done with int x; int *ptr; ptr = &x; The unary operatory & returns the address of a particular variable. We can represent the above three lines of code graphically with Since we did not assign the value of x, it does not contain a value. We can assign the value of x in one of two ways. The first way is the most obvious way, which is simply x=5; The pointer diagram now has a value at the location in which the variable is x is stored: The other way we can assign the value of x is to use the pointer that points to its address. Since ptr points to its address, we can assign a value to the address that this pointer points to with *ptr = 5;

2 Handout 10 24/03/03 2 This is called dereferencing the pointer and it is identical to x=5;. The dereferencing operator says to set the contents of location pointed to by the pointer to the specified value. Pointers themselves also take up memory, since they store a number that is an address to another location in memory. For the sake of argument, let s say that the pointer ptr is located at memory address 100 and that x is located at memory address 200, as depicted the following diagram, which results when you declare int x, *ptr; When you set the pointer to point to the address of x with ptr=&x, you are assigning a value of 200 to the pointer, so that the pointer diagram now looks like And when we set the value of x with either x=5 or *ptr=5, the pointer diagram looks like Since the pointer simply stores the address of another variable, there is nothing to stop us from setting the value of the integer x to the pointer ptr with x=ptr; In this case the pointer diagram would look like

3 Handout 10 24/03/03 3 But it is very rare that you would ever want to do this. Since pointers in general only store addresses, it is much easier to understand pointers if we represent them in memory as blocks with a dot at their center, with an arrow that points to the particular variable that exists at the address of the pointer, as we did before, as in since x is not a pointer but it still contains the address of ptr, then its value is 200. Now consider the case when we declare more variables and pointers, such as int x=10, y=5, *ptr1, *ptr2; ptr1=&x; ptr2=&y; In this case the pointer diagram would look like For this simple case, you can think of dereferencing a pointer as only changing the values at the addresses, while re-equating a pointer actually changes the pointers around. Consider the dereferencing operations *ptr1=y; *ptr2=x; This first one changes the value of x to y, while the second one changes the value of y to the new value of x, which is the same as y, and hence does not change its value. The pointer diagram after this operation looks like

4 Handout 10 24/03/03 4 These operations are identical to x=y; y=x; Now consider the case in which we swap the pointers with ptr1=&y; ptr2=&x; The pointer diagram now looks like We can also set one pointer to the other, ptr1=ptr2; so now the pointers both point to the memory location of x, as in Using pointers in functions When you provide arguments to functions, the function stores the declared arguments as well as the variables within the function on the stack, and makes a copy of the function arguments to be used in the function. For example, consider the code void ChangeI(int i); main() { int i=0; ChangeI(i);

5 Handout 10 24/03/03 5 void ChangeI(int i) { i=10; If we draw a pointer diagram for this function then we have So we can see that since the function call merely makes a copy of its arguments, then the value of i is not changed in the calling main function. This is because the argument to the function is passed by value. We can get around this problem by passing the arguments to the function by reference using a pointer to i, as in void ChangeI(int *i); main() { int i=0; ChangeI(&i); void ChangeI(int *i) { *i=10; In this case we passed the address of the variable i to the function instead of its value. The call to the function still makes a copy of its arguments, but since that copy is the value of the address of i, then we can use that address to change the value in main. The pointer diagram for how this happens is given by A classic example of why you need to pass variables to functions by reference rather than by value arises when you want to swap the values of two variables. Consider the following function that does not use pointers

6 Handout 10 24/03/03 6 void swap(int a, int b) { int tmp = b; b=a; a=tmp; Clearly this does not work if we try and swap two values using swap(a,b); However, if we send the addresses of x and y with swap(&a,&b);, then it will work, as in void swap(int *a, int *b) { int tmp = *b; *b=*a; *a=tmp; You can see what is happening when you make a call to the swap function with int a=20,b=10; swap(&a,&b); in the following pointer diagram: Arrays and strings as pointers On the stack We have already seen how we can declare arrays and strings without pointers. For example, we can declare an array of 5 floats as float a[5];

7 Handout 10 24/03/03 7 and we can declare a string with char str[] = "hello"; Both of these declarations are actually declaring pointers to a block of memory. When you declare an array of floats as above, you are declaring a to be a pointer that points to a block of memory on the local stack, and the pointer diagram looks like You access different portions of that block of memory by dereferencing the pointer with, for example, a[3]. The same goes for strings, except the compiler does a little extra on your behalf because it sees that "hello" is a string. So the pointer diagram for a string declaration looks like but in addition to the five characters in "hello", the additional null character \0 is added on to ensure that this is a string. Dynamically allocated in the heap In the previous example, we declared arrays statically so that the memory that was allocated for them was allocated on the stack. This memory is allocated when the program begins and is allocated for the life of the program. It cannot be used for anything other than the array or string for which it was originally allocated. C provides us with the capability of allocating memory dynamically to store arrays and strings in the heap. This memory can be freed up for use by other arrays during the life of the program. To allocate space dynamically, we use the malloc function, which takes as its argument the amount of space we need to allocate. For example, if we want to allocate enough space in the heap for an array containing 5 floats, we would use float *a = malloc(5*sizeof(float)); The sizeof command is conveniently used to tell us how many bytes we need for that particular type. In this case sizeof(float) returns 4 because a floating point number requires 4 bytes of memory. The pointer diagram for this allocation is similar to that for the static allocation in the previous section, except now the pointer a points to 5 4-byte blocks of memory in the heap, rather than the stack. For comparison, the declaration float b[5]; is also shown in the pointer diagram.

8 Handout 10 24/03/03 8 The benefit of using arrays that are dynamically allocated in the heap is that we can free up the memory that was allocated for them with free(a); A note of Warning: you can only free space that is dynamically allocated in the heap. If you attempt to use free on a statically allocated variable, such as free(b);, your code will crash with a memory error that will read Segmentation fault Malloc and the void * pointer If you use the malloc command as it is, it will be fine, but you most likely will get a compiler warning such as warning: initialization makes pointer from integer without a cast This is because the function prototype for the malloc function appears as void *malloc(size_t size); The size_t type-casts your integer input for you, but the return type of malloc is a void * pointer. C requires 4 bytes for every pointer you declare, regardless of its type, so it doesn t really matter what kind of pointer malloc returns. This is because no matter what kind of pointer you are using, it only needs 4 bytes to store a memory address. Therefore, malloc only needs to return the requisite 4-byte memory address. To fix the warning, you need to type-cast the return type of the function so that it is the same as the pointer you are equating it to. Since in this example we are creating a pointer to a floating point number, then we need to type-cast the return of the call to malloc as a float *, as in float a = (float *)malloc(5*sizeof(float)); Pointer arithmetic When you declare a pointer such as int *x; x is a pointer to a location in memory and, regardless of the type to which it points, we only need 4 bytes to store the pointer. But while every pointer only needs 4 bytes to store a memory address, the compiler also needs to know the type of variable that it points to in order to perform pointer arithmetic. As an example, consider the declaration

9 Handout 10 24/03/03 9 int x, *b, a[]={5,10,15,20,25; In this declaration, x is an integer, and a and b are pointers to an integer (we could also have used *a=(int *)malloc(5*sizeof(int)) and it would also have applied to this section). The difference between the two is that a points to the first element of a 5-integer block on the stack, while b doesn t point to anything yet. The pointer diagram for this declaration looks like We can set b to point to the first element of the array with b=a, so now the pointer diagram looks like To access the first element of the integer array a, we use x=a[0]; This is identical to x=*a; which dereferences the pointer a which simply points to the first element of the 5-integer block. To access the second element, we can use x=a[1]; Using pointer arithmetic, we can let b point to the next element of the array by setting it with b=a+1; Now the pointer diagram looks like

10 Handout 10 24/03/03 10 This statement is why it is important for the compiler to know what types are stored at the addresses of pointers a and b. When it sees addition applied to pointers, such as the above, it knows that because the pointers store addresses of 4-byte integers, that incrementing a pointer by 1 means going to the next block of memory that is 4 bytes away. After doing this, we can dereference pointer b with x=*b; so that x is now 10. This also works for any of the elements in the array, so that to access element i, we can use either x=a[i]; or x=*(a + i); Integer arithmetic also allows us to increment or decrement pointers using the ++ or -- operators. For example, to loop through the elements of a, we would use b=a; for(i=0;i<5;i++) printf( %d,*b++); The statement *b++ says to first dereference the pointer b and get its value, and then to increment it, or to have it point to the next element in the array. Note that the operation a++ will not work. This is because while the declaration a[] declares a pointer to an integer, it is a constant pointer and cannot be changed (The same goes for a declaration such as a[5]). If you attempt to do this then the compiler will give you an error like wrong type argument to increment Because it is very rare that you will actually initialize pointers like we initialized a, it is better to initialize non-constant pointers in the heap, and then setting the values afterwards, such as in int i, *a = (int *)malloc(5*sizeof(int)); for(i=0;i<5;i++) *a++=5*(i+1); The important thing to remember here is that the two statements printf( %d,*b++); and printf( %d,(*b)++); are very different. The first statement is identical to *(b++), because the ++ operator takes precendence over the * operator. So in this case, we first dereference the pointer, and then add 1 to the pointer, so that it points to the next element. But in the second case, (*b)++, we only increment the value stored at the address of b and never increment the pointer. So the first statement will give us , but the second statement will give us

Lecture 11 Doubly Linked Lists & Array of Linked Lists In this lecture Doubly linked lists Array of Linked Lists Creating an Array of Linked Lists Representing a Sparse Matrix Defining a Node for a Sparse

### Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Announcements Memory management Assignment 2 posted, due Friday Do two of the three problems Assignment 1 graded see grades on CMS Lecture 7 CS 113 Spring 2008 2 Safe user input If you use scanf(), include

### Comp Arrays and Pointers. Class Notes. T h e G r o u p o f T h r e e

Comp 2401 Arrays and Pointers Class Notes 2013 T h e G r o u p o f T h r e e Introduction To Arrays: In C programming, one of the frequently problem is to handle similar types of data. For example: if

### Pointers and dynamic memory management in C

Pointers and dynamic memory management in C Jakob Rieck Proseminar C - 2014 Contents 1 Introduction 2 2 Pointers 4 2.1 Declaration & Initialization................... 5 2.2 Using pointers..........................

### Common Errors in C. David Chisnall. February 15, 2011

Common Errors in C David Chisnall February 15, 2011 The C Preprocessor Runs before parsing Allows some metaprogramming Preprocessor Macros Are Not Functions The preprocessor performs token substitution

### Example Solution to Exam in EDA150 C Programming

Example Solution to Exam in EDA150 C Programming Janurary 12, 2011, 14-19 Inga hjälpmedel! Examinator: Jonas Skeppstedt, tel 0767 888 124 30 out of 60p are needed to pass the exam. General Remarks A function

### Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc

Arrays and Structs and Pointers, Oh My! Programming in C Input and output Using printf Standard input and output Pointers Arrays Structures Combining these things together Arrays and Structs and Pointers,

### Strings in C++ and Java. Questions:

Strings in C++ and Java Questions: 1 1. What kind of access control is achieved by the access control modifier protected? 2 2. There is a slight difference between how protected works in C++ and how it

### C A short introduction

About these lectures C A short introduction Stefan Johansson Department of Computing Science Umeå University Objectives Give a short introduction to C and the C programming environment in Linux/Unix Go

### Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Dr. Martin O. Steinhauser University of Basel Graduate Lecture Spring Semester 2014 Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct Friday, 7 th March

### C for Java Programmers

C for Java Programmers CS 414 / CS 415 Niranjan Nagarajan Department of Computer Science Cornell University niranjan@cs.cornell.edu Original Slides: Alin Dobra Why use C instead of Java Intermediate-level

### The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements:

1 2 Simple and compound s The if- Simple s: E.g.: expression; Various jumps : break, goto, continue, return. k = a * p + 3; printf("k = %d\n", k); 1 + 2; ; The if- comes in two forms: or E.g.: if (expression)

### The C Programming Language course syllabus associate level

TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

### strsep exercises Introduction C strings Arrays of char

strsep exercises Introduction The standard library function strsep enables a C programmer to parse or decompose a string into substrings, each terminated by a specified character. The goals of this document

### A Rudimentary Intro to C programming

A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 4: Strings and Pointers 18 Strings.................................... D1 19 String Functions..............................

### M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: IMPORTANT INSTRUCTIONS: 1. Question Paper in English and Hindi and Candidate can choose any one language. 2. In case of discrepancies in

### Arrays. Arrays, Argument Passing, Promotion, Demotion

Arrays Arrays, Argument Passing, Promotion, Demotion Review Introduction to C C History Compiling C Identifiers Variables Declaration, Definition, Initialization Variable Types Logical Operators Control

### Pointers and Strings. Objectives

5 Pointers and Strings Objectives To be able to use pointers. To be able to use pointers to pass arguments to functions by reference. To understand the close relationships among pointers, arrays and strings.

### COMP MIPS instructions 3 Feb. 10, 2016

In the past two lectures, we discussed MIPS operations on integers. Today we will consider a few data structures that you are familiar with, namely arrays and strings, and discuss how to implement them

### Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Problem 1 Recall the definition of root in project 1. (The declaration of struct entrynode appears below.) struct entrynode * root; Give the type of each of the following expressions. The answer may be

### CPSC 213. C vs. Java. Introduction to Computer Systems. Reading. Java Hello World... C Hello World... C, Pointers, and Dynamic Allocation.

Reading CPSC 213 Textbook New to C, Understanding Pointers, The malloc and free Functions, Why Dynamic Memory Allocation 2ed: "New to C" sidebar of 34, 310, 991-992 1ed: "New to C" sidebar of 34, 311,1091-1092

### arrays C Programming Language - Arrays

arrays So far, we have been using only scalar variables scalar meaning a variable with a single value But many things require a set of related values coordinates or vectors require 3 (or 2, or 4, or more)

### Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006

Basic C Syntax Comp-206 : Introduction to Software Systems Lecture 10 Alexandre Denault Computer Science McGill University Fall 2006 Next Week I'm away for the week. I'll still check my mails though. No

### Understanding Valgrind memory leak reports

Understanding Valgrind memory leak reports Aleksander Morgado aleksander@es.gnu.org Thanks to the development team of Azetti Networks not only for supplying so many example memory leaks, but also for their

### Primitive Data Types Summer 2010 Margaret Reid-Miller

Primitive Data Types 15-110 Summer 2010 Margaret Reid-Miller Data Types Data stored in memory is a string of bits (0 or 1). What does 1000010 mean? 66? 'B'? 9.2E-44? How the computer interprets the string

### 5 Arrays and Pointers

5 Arrays and Pointers 5.1 One-dimensional arrays Arrays offer a convenient way to store and access blocks of data. Think of arrays as a sequential list that offers indexed access. For example, a list of

### The char Data Type. Character and String Processing. Another Example /* Capitalize all lowercase letters */ while ((c = getchar())!

Character and String Processing CSE 130: Introduction to C Programming Spring 2005 The char Data Type A char value can be thought of as either a character or a small integer printf( %d, a ); /* prints

### CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 Basics of C Remark: Skim Chapters 1 through 6 of Deitel & Deitel. You will notice the following: C is (more or less) a subset of Java. (So, you are

### Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Data types define the way you use the storage (memory) in the programs. By specifying a data type, you tell the sompiler how to create a particular piece

### C++ for Game Programmers

C++ for Game Programmers Course Description C++ has become one of the favourite programming language for game programmers. Reasons for wide spread acceptability of C++ are plenty, but primary reasons are,

### Summary. Pre requisition. Content Details: 1. Basics in C++

Summary C++ Language is one of the approaches to provide object-oriented functionality with C like syntax. C++ adds greater typing strength, scoping and other tools useful in object-oriented programming

### CS1020E: DATA STRUCTURES AND ALGORITHMS I

CS1020E: DATA STRUCTURES AND ALGORITHMS I Tutorial 1 Basic C++, OOP Problem Solving (Week 3, starting 22 August 2016) 1. Evaluation Order (Note: You can use any other C++ code editor/compiler). Examine

### 6.096 Lab 1. Due: 7 January 12:30:00. September 20, 2011

6.096 Lab 1 Due: 7 January 1:30:00 September 0, 011 1 Additional Material 1.1 Constants A constant is an expressions with a fixed value. Kinds of constants: Literals: used to express particular values

### IC221: Systems Programming 06-Week Written Exam [SOLUTIONS]

IC221: Systems Programming 06-Week Written Exam [SOLUTIONS] February 12, 2014 Answer the questions in the spaces provided on the question sheets. If you run out of room for an answer, continue on the back

### Introduction to C Programming S Y STEMS

Introduction to C Programming CS 40: INTRODUCTION TO U NIX A ND L I NUX O P E R AT ING S Y STEMS Objectives Introduce C programming, including what it is and what it contains, which includes: Command line

### Object Oriented Software Design II

Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

### Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Module No. # 02 Lecture No. # 05 Run-time Environments-Part 3 and Local Optimizations

### Lecture 7: Arrays, strings, and functions

Handout 8 17/03/03 1 Arrays Lecture 7: Arrays, strings, and functions Arrays are declared to store a certain number of variables of a specified type either on the stack, which is the local static memory

### Exam in EDAA25 C Programming

Exam in EDAA25 C Programming October 30, 2014, 14-19 Inga hjälpmedel! Examinator: Jonas Skeppstedt Grading instructions In general: assess if a function or program works as intended while ignoring syntax

### Going from Python to C

Going from Python to C Darin Brezeale December 8, 2011 Python is a high-level, interpreted language. C has many of the same types of programming constructs as in Python: arrays, loops, conditionals, functions,

### C Primer. Fall Introduction C vs. Java... 1

CS 33 Intro Computer Systems Doeppner C Primer Fall 2016 Contents 1 Introduction 1 1.1 C vs. Java.......................................... 1 2 Functions 1 2.1 The main() Function....................................

### Semantic Analysis: Types and Type Checking

Semantic Analysis Semantic Analysis: Types and Type Checking CS 471 October 10, 2007 Source code Lexical Analysis tokens Syntactic Analysis AST Semantic Analysis AST Intermediate Code Gen lexical errors

### Programming for MSc Part I

Herbert Martin Dietze University of Buckingham herbert@the-little-red-haired-girl.org July 24, 2001 Abstract The course introduces the C programming language and fundamental software development techniques.

### INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming

INDEX 1) Introduction to C Programming a. What is C? b. Getting started with C 2) Data Types, Variables, Constants a. Constants, Variables and Keywords b. Types of Variables c. C Keyword d. Types of C

### DYNAMIC MEMORY ALLOCATION AND FRAGMENTATION IN C & C++ COLIN WALLS, MENTOR GRAPHICS

DYNAMIC MEMORY ALLOCATION AND FRAGMENTATION IN C & C++ COLIN WALLS, MENTOR GRAPHICS E M B E D D E D S O F T W A R E W H I T E P A P E R w w w. m e n t o r. c o m INTRODUCTION In C and C++, it can be very

### 6.087 Lecture 5 January 15, 2010

6.087 Lecture 5 January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String

### Quiz for Chapter 2 Instructions: Language of the Computer3.10

Date: 3.10 Not all questions are of equal difficulty. Please review the entire quiz first and then budget your time carefully. Name: Course: Solutions in Red 1. [5 points] Prior to the early 1980s, machines

### CIS 190: C/C++ Programming. Lecture 1 Introduction and Getting Started

CIS 190: C/C++ Programming Lecture 1 Introduction and Getting Started This course will teach you the basics of C and C++ give you more programming experience be appropriate for majors and non-majors not

### Java Interview Questions and Answers

1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write and compile the java

### CS 261 Data Structures. Introduction to C Programming

CS 261 Data Structures Introduction to C Programming Why C? C is a simple language, C makes it easier to focus on important concepts C is a lower level, imperative language Lays groundwork for many other

### 1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C

Character set in C We should use only the following characters in writing a C program. These characters can be combined to create C words. Alphabet: A, B, C, D.. Z, a, b, c, d..z Numeric digits: 0, 1,

### Lecture 8: Header files, functions, and other data types

Handout 9 20/03/03 1 Lecture 8: Header files, functions, and other data types A simple header file In the last lecture we saw how to declare a function in C in the same code in which it is used. For example,

### Punctuation in C. Identifiers and Expressions. Identifiers. Variables. Keywords. Identifier Examples

Identifiers and Expressions CSE 130: Introduction to C Programming Spring 2005 Punctuation in C Statements are terminated with a ; Groups of statements are enclosed by curly braces: { and } Commas separate

### Reading. C Programming Language. Basic syntax Whitespaces. Whitespaces (cont d) #include. Basic syntax Comments

Reading C Programming Language Types, operators, expressions Control flow, functions Basic IO K&R Chapter 2 Types, Operators, and Expressions K&R Chapter 3 Control Flow K&R Chapter 7 Basic I/O NEWS Assignment

### Format String Vulnerability. printf ( user input );

Lecture Notes (Syracuse University) Format String Vulnerability: 1 Format String Vulnerability printf ( user input ); The above statement is quite common in C programs. In the lecture, we will find out

### C Pointers and Arrays. University of Texas at Austin CS310 - Computer Organization Spring 2009 Don Fussell

C Pointers and Arrays University of Texas at Austin CS310 - Computer Organization Spring 2009 Don Fussell Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them

### C Programming, Chapter 1: C vs. Java, Types, Reading and Writing

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing T. Karvi August 2013 T. Karvi C Programming, Chapter 1: C vs. Java, Types, Reading and Writing August 2013 1 / 1 C and Java I Although the

### Dynamic Memory Allocation using a Custom Implemented Single-Linked List

Dynamic Memory Allocation using a Custom Implemented Single-Linked List Anders Eiler April 8, 2013 1 Introduction The objective of this paper is to describe how to dynamically allocate memory at runtime

### Stacks. Linear data structures

Stacks Linear data structures Collection of components that can be arranged as a straight line Data structure grows or shrinks as we add or remove objects ADTs provide an abstract layer for various operations

### Primitives and Arithmetic

CS106A, Stanford Handout #29 Fall, 2004-05 Nick Parlante Primitives and Arithmetic Objects vs. Primitives Types in Java are divided into two camps: objects and primitives. Objects are relatively complex

### SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK (2110003) [F.Y.B.E.

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK Subject: Computer Programming and Utilization (2110003) [F.Y.B.E.: ALL BRANCHES] Unit 1

### Final Exam Review. CS 1428 Fall Jill Seaman. Final Exam

Final Exam Review CS 1428 Fall 2011 Jill Seaman 1 Final Exam Friday, December 9, 11:00am to 1:30pm Derr 241 (here) Closed book, closed notes, clean desk Comprehensive (covers entire course) 25% of your

### Lecture 03 Bits, Bytes and Data Types

Lecture 03 Bits, Bytes and Data Types In this lecture Computer Languages Assembly Language The compiler Operating system Data and program instructions Bits, Bytes and Data Types ASCII table Data Types

### Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

JAVA TYPES BASIC DATA TYPES GENERAL Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type

### Common C Errors. Compiled by: Leela Kamalesh Yadlapalli

Common C Errors Compiled by: Leela Kamalesh Yadlapalli This document shows some of the common errors and warnings that you may encounter during this class. Always remember to use the Wall option if you

### The Basics of C Programming. Marshall Brain

The Basics of C Programming Marshall Brain Last updated: October 30, 2013 Contents 1 C programming 1 What is C?................................. 2 The simplest C program, I........................ 2 Spacing

### Chapter 13. Pointers and Linked Lists

Chapter 13 Pointers and Linked Lists Overview 13.1 Nodes and Linked Lists 13.2 Stacks and Queues Slide 13-2 13.1 Nodes and Linked Lists Nodes and Linked Lists n A linked list is a list that can grow and

### 05 Case Study: C Programming Language

CS 2SC3 and SE 2S03 Fall 2009 05 Case Study: C Programming Language William M. Farmer Department of Computing and Software McMaster University 18 November 2009 The C Programming Language Developed by Dennis

### Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic memory allocation How to allocate memory for variables (esp. arrays/strings) during run time malloc(), calloc(), realloc(), and free() 1 Why dynamic memory allocation?

### SYSTEMS PROGRAMMING C++ INTRODUCTION

Faculty of Computer Science / Institute of Systems Architecture / Operating Systems SYSTEMS PROGRAMMING C++ INTRODUCTION Alexander Warg WHY C++? C++ is the language that allows to express ideas from the

### UNIT-1 PART-A UNIT-2 PART-A

UNIT-1 1. Define computer? 2. Discuss briefly about input and output devices? 3. What is a RAM? 4. What is arom? 5. What is a compiler? 6. Write about a linker? 7. List any 5 key wordsof c? 8. Illustrate

### Sources: On the Web: Slides will be available on:

C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

### Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

### Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Coding Rules Section A: Linux kernel style based coding for C programs Coding style for C is based on Linux Kernel coding style. The following excerpts in this section are mostly taken as is from articles

### CS193D Handout 04 Winter 2005/2006 January 11, 2006 A Crash Course in C++

CS193D Handout 04 Winter 2005/2006 January 11, 2006 A Crash Course in C++ Most people taking CS193D will already have some experience with C++. This handout is a quick refresher in case it s been a while.

### CSCI-1200 Data Structures Fall 2015 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Fall 2015 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

### 1. What is the output of this program?

1. What is the output of this program? char *ptr; char string[] = "How are you?"; ptr = string; ptr += 4; printf("%s",ptr); (a) How are you? (b) are you? (c) are (d) No output 2. Which of the following

### 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

### MPI and C-Language Seminars 2010

MPI and C-Language Seminars 2010 Seminar Plan (1/3) Aim: Introduce the C Programming Language. Plan to cover: Basic C, and programming techniques needed for HPC coursework. C-bindings for the Message Passing

### TN203. Porting a Program to Dynamic C. Introduction

TN203 Porting a Program to Dynamic C Introduction Dynamic C has a number of improvements and differences compared to many other C compiler systems. This application note gives instructions and suggestions

### 1 (1x17 =17 points) 2 (21 points) 3 (5 points) 4 (3 points) 5 (4 points) Total ( 50points) Page 1

CS 1621 MIDTERM EXAM 1 Name: Problem 1 (1x17 =17 points) 2 (21 points) 3 (5 points) 4 (3 points) 5 (4 points) Total ( 50points) Score Page 1 1. (1 x 17 = 17 points) Determine if each of the following statements

### Lecture 4. Announcements:

Announcements: Lecture 4 HW1 posted on Saturday morning. We gave a little over a week. We are going to discuss relevant material over the next two lectures. Lab1 posted. But if you start doing it, you

### GDB Tutorial. A Walkthrough with Examples. CMSC 212 - Spring 2009. Last modified March 22, 2009. GDB Tutorial

A Walkthrough with Examples CMSC 212 - Spring 2009 Last modified March 22, 2009 What is gdb? GNU Debugger A debugger for several languages, including C and C++ It allows you to inspect what the program

### Fundamentals of Computer & Programming in C Short-answer type questions

Fundamentals of Computer & Programming in C Short-answer type questions Section- A i. What is a computer system? Draw the block diagram of computer. ii. Differentiate between Real Time Operating System

### OUTCOMES BASED LEARNING MATRIX

Course: CTIM371 Programming in C++ OUTCOMES BASED LEARNING MATRIX Department: Computer Technology and Information Management Course Description: This is the first course in the C++ programming language.

### Dept. of CSE, IIT KGP

Programming in C: Basics CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of variable We must declare the

### Introduction to C ++ : Questions

Introduction to C ++ : Questions Thomas Branch 28th November 204. A Tutorial Introduction. Getting Started Aim: Make sure the program compiles and runs, learn to get the program to output text. [Debug]

### School of Informatics, University of Edinburgh

CS1Ah Lecture Note 5 Java Expressions Many Java statements can contain expressions, which are program phrases that tell how to compute a data value. Expressions can involve arithmetic calculation and method

### Overview of a C Program

Overview of a C Program Programming with C CSCI 112, Spring 2015 Patrick Donnelly Montana State University Programming with C (CSCI 112) Spring 2015 2 / 42 C Language Components Preprocessor Directives

### 3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging?

Freshers Club Important 100 C Interview Questions & Answers 1) How do you construct an increment statement or decrement statement in C? There are actually two ways you can do this. One is to use the increment

### C Interview Questions

http://techpreparation.com C Interview Questions And Answers 2008 V i s i t T e c h P r e p a r a t i o n. c o m f o r m o r e i n t e r v i e w q u e s t i o n s a n d a n s w e r s C Interview Questions

### Borland C++ Compiler: Operators

Introduction Borland C++ Compiler: Operators An operator is a symbol that specifies which operation to perform in a statement or expression. An operand is one of the inputs of an operator. For example,

### CSE 303 Concepts and Tools for Software Development. Magdalena Balazinska Winter 2010 Lecture 19 Inheritance (virtual functions and abstract classes)

CSE 303 Concepts and Tools for Software Development Magdalena Balazinska Winter 2010 Lecture 19 Inheritance (virtual functions and abstract classes) Where We Are We have already covered the introduction

### COMP 321: Introduction to Computer Systems

Assigned: 1/21/16, Due: 2/4/16, 11:55 PM Important: This project must be done individually. Be sure to carefully read the course policies for assignments (including the honor code policy) on the assignments

### 6.S096 Lecture 1 Introduction to C

6.S096 Lecture 1 Introduction to C Welcome to the Memory Jungle Andre Kessler Andre Kessler 6.S096 Lecture 1 Introduction to C 1 / 30 Outline 1 Motivation 2 Class Logistics 3 Memory Model 4 Compiling 5

### Introduction to C++ Data Abstraction w/ Classes Topic #2

Introduction to C++ Data Abstraction w/ Classes Topic #2 CS202 2-1 Lecture #1 plus Review Abstract Data Types Introduction to...object Models Introduction to...data Abstraction Using Data Abstraction in

### C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 2: Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with the basic components of a C++ program,