C Arrays and Pointers

Size: px
Start display at page:

Download "C Arrays and Pointers"

Transcription

1 C Arrays and Pointers

2 C Arrays List of contiguous values in memory Array Declaration: Type Name Count int vec[5]; Type: Type of each element Name: Identifier for the entire array Count: Number of elements in the list Label Address Value 0xFFFF FFFC 0xDEAD BEEF 0xFFFF FFF8 0xDEAD BEEF 0xFFFF FFF4 0xDEAD BEEF 0xFFFF FFF0 0xDEAD BEEF vec[4] 0x0000 0C14 0x vec[3] 0x0000 0C10 0x vec[2] 0x0000 0C0C 0x vec[1] 0x0000 0C08 0x vec[0] 0x0000 0C04 0x x C 0x C 0x x x x x x

3 Inverted Arrays Standard representation of arrays in CS is top to bottom. int vec[5]={365,366,367,368,369}; vec vec[0]=365 vec[1]=366 vec[2]=367 vec[3]=368 vec[4]=369 Label Address Value 0xFFFF FFFC 0xDEAD BEEF 0xFFFF FFF8 0xDEAD BEEF 0xFFFF FFF4 0xDEAD BEEF 0xFFFF FFF0 0xDEAD BEEF vec[4] 0x0000 0C14 0x C vec[3] 0x0000 0C10 0x B vec[2] 0x0000 0C0C 0x A vec[1] 0x0000 0C08 0x vec[0] 0x0000 0C04 0x x C 0x C 0x x x x x x

4 Array Element Access Square Bracket Operator Argument: Index from 0 to (Count-1) Example: for(i=0;i<5;i++) { vec[i]=280+i; } Can be used to read or write an array element Label Address Value 0xFFFF FFFC 0xDEAD BEEF 0xFFFF FFF8 0xDEAD BEEF 0xFFFF FFF4 0xDEAD BEEF 0xFFFF FFF0 0xDEAD BEEF vec[4] 0x0000 0C14 0x C vec[3] 0x0000 0C10 0x B vec[2] 0x0000 0C0C 0x A vec[1] 0x0000 0C08 0x vec[0] 0x0000 0C04 0x x C 0x C 0x x x x x x

5 Array Name In C, by convention, the array name is the address of the first element of the array vec=&(vec[0]) Therefore, the following holds: &(vec[i]) == (char *)vec + sizeof(vec[0]) * i

6 A Pointer points to one or more elements of a specific type (Actually, zero or more, but who s counting) 6

7 Pointer Arithmetic You can do math (+-*/%) with pointers, but A unit in pointer arithmetic is the size of the data type pointed to by the pointer int *x; int vec[5]; for (x=vec; x<&vec[5]; x++) (*x)=3; Label Address Value 0xFFFF FFFC 0xDEAD BEEF 0xFFFF FFF8 0xDEAD BEEF x 0x0000 0D10 0x0000 0C18 vec[4] 0x0000 0C14 0x vec[3] 0x0000 0C10 0x vec[2] 0x0000 0C0C 0x vec[1] 0x0000 0C08 0x vec[0] 0x0000 0C04 0x x C 0x C 0x x x x x x

8 Integer Vector in Memory int v[4]={11,12,13,14}; Vector v[0] v[1] v[2] v[3] Value x b x c x d x e Address x00c00014 x00c00018 x00c0001c x00c00020 int *vp=v; // vp=x00c00014 printf( vp-> %d %d %d \n,*vp,*(vp+1),*(vp+2)); vp->

9 Abstraction An array is an indexable list of data items char buffer[200]; // buffer is a list of 200 characters buffer[0]= H ; // set the first item in buffer to H int length[3]; // length is a list of 3 integers length[0]=12; length[1]=12; length[2]=12;

10 Leaky Abstraction Under the covers, C treats an array as a pointer to zero or more data items Therefore, you can program as if an array is a pointer or a pointer is an array! int array[10]; array == &(array[0]) or (*array)==array[0] array[i] ==*(array+i);

11 Pointer / Array Ambiguity In C, we can treat pointers like arrays, and arrays like pointers Using array notation int strlen(char str[]) { } int i=0; while(str[i]!=0) { } i++; return i; Using pointer notation int strlen(char *str) { int i=0; while((*str)!=0) { i++; str++; } return i; }

12 Multi-Dimensional Arrays int xyz[2][3]; // 2 rows/3 cols xyz xyz[0][0] xyz[0][1] xyz[0][2] xyz[1][0] xyz[1][1] xyz[1][2] Row Major order like odometer, right digit changes fastest Label Address Value 0xFFFF FFFC 0xDEAD BEEF 0xFFFF FFF8 0xDEAD BEEF 0xFFFF FFF4 0xDEAD BEEF xyz[1][2] 0x0000 0C18 0x D xyz[1][1] 0x0000 0C14 0x C xyz[1][0] 0x0000 0C10 0x B xyz[0][2] 0x0000 0C0C 0x A xyz[0][1] 0x0000 0C08 0x xyz[0][0] 0x0000 0C04 0x x C 0x C 0x x x x x x

13 Matrix Pointer Arithmetic int mat[3][4]; int r; int c &(mat[r][c]) == mat + (r*4)+c Or elementsize=sizeof(int); rowsize=4*elementsize; &(mat[r][c]) == (char *)mat + (r*rowsize) + c * elementsize

14 Arrays of Pointers int *xyz[2]; // Array of two pointers xyz xyz[0] xyz[1] (*xyz[0])[0] (*xyz[0])[1] (*xyz[0])[2] (*xyz[1])[0] (*xyz[1])[1] Label Address Value xyz[1] 0x0000 CA0C 0x0000 C708 xyz[0] 0x0000 CA08 0x0000 C000 xyz[1][1] 0x0000 C70C 0x xyz[1][0] 0x0000 C708 0x xyz[0][2] 0x0000 C008 0x xyz[0][1] 0x0000 C004 0x xyz[0][0] 0x0000 C000 0x

15 Difference between Arrays and Pointers Array Fixed, pre-defined length Space reserved by compiler All 2D rows equal length Pointers to List No pre-defined length No space reserved 2D rows may be varying length

16 Dealing with Undefined lengths Implicit Explicit Guard int triperim(int sides[]) { return side[0] + side[1] + side[2]; } Agreement: sides is an array with three elements Probably better to use sides[3] in argument definition int perim(int n, int sides[]) { int j; int sum=0; for(j=0;j<n;j++) sum+=sides[j]; return sum; } Agreement: Caller specifies how many elements are in sides with an extra argument int perim(int sides[]) { int j; int sum=0; for(j=0;sides[j]>0;j++) sum+=sides[j]; return sum; } Agreement: Caller puts a guard value after last significant element of sides Guard value is an invalid value

17 Character Strings In C, there is no string type Text consists of an array of characters char nextline[80]; char bgcolor[]= magenta ; // bgcolor has 8 values Guard is a null terminator = 0x00 When we are dealing with strings, we almost always use pointer notation Length is not important BECAUSE we have a null terminator char * string; // is very similar to char string[100]

18 Pointers and Memory When you declare an int or array of ints, the compiler reserves memory for that int or array of ints When you declare a pointer to an int or array of ints, the compiler reserves memory for the POINTER, but does NOT reserve memory for the actual data being pointed to! Typical C bug: char* name; // similar to char name[100] but strcpy(name, Tom Bartenstein ); Segmentation Violation

19 Pointers and Constants Pointer notation does NOT reserve space! char * string; string[0]=0x00; // may result in SEGMENTATION VIOLATION! Space IS reserved for a constant! char * string= This is a test ; string[0]= X ; printf( String is: %s\n,string); // prints String is: Xhis is a test

20 Initialized Pointers In some cases, it looks like pointers ARE reserving memory char * name = Tom Bartenstein ; In this case, the compiler is creating a LITERAL value in memory Possibly read only memory Possibly shared with other instances of Tom Bartenstein literal in this code The name variable points to the LITERAL value May get segmentation violation if written to May get bizarre future errors if compiler re-used this literal

21 Arrays as Arguments We ve already been doing this with string functions! Here as some examples char name[100]; strcpy(name, Tom Bartenstein ); int n=strlen(name); if (0==strncmp(name, Tom,3)) printf( My name is %s\n,name); scanf( %s,name);

22 Array Return Values C does not support returning an array C will allow returning a POINTER to an array The array name with no brackets is a pointer to the array The array must be static cannot be an automatic variable! int * coldest3(int n,int temps[ ]) { static int cold3[3] = { -1, -1, -1} ; return cold3; }

23 Using a returned pointer You can use a pointer to an array as if it were an array You must know the size of the array! int * c3 = coldest3(10,temps); printf( The three coldest temps are %d, %d, and %d\n, c3[0],c3[1],c3[2]); See also: examples/xmp_hw3_ptr and examples/xmp_hw3_ptr2

24 Resources The C Programming Language, (K&R) Chapter 5 Computer Systems, Section 3.8 Wikepedia Pointers : C Pointer Tutorial : C FAQ Arrays and Pointers Section: 25

Binghamton University. CS-211 Fall Pointers vs. Arrays

Binghamton University. CS-211 Fall Pointers vs. Arrays Pointers vs. Arrays 1 Array Values are Contiguous Right next to each other in memory int vec[6] int m [4][3]; vec[0] vec[1] vec[2] vec[3] vec[4] vec[5] m[0][0] m[0][1] m[0][2] m[1][0] m[1][1] m[1][2] m[2][0]

More information

Binghamton University. CS-211 Fall Pointers vs. Arrays

Binghamton University. CS-211 Fall Pointers vs. Arrays Pointers vs. Arrays 1 Pointers in C Pointers are a special data type The VALUE of a pointer is an address The TYPE of a pointer is pointer to pointer to character pointer to integer pointer

More information

Binghamton University. CS-211 Fall Pointers

Binghamton University. CS-211 Fall Pointers Pointers 1 Memory The act of keeping track of something over time Remembering is the concept of storing information A memory is no good unless you can retrieve that information In a computer, we remember

More information

Binghamton University. CS-220 Spring Arrays in C

Binghamton University. CS-220 Spring Arrays in C Arrays in C 1 One Dimensional Array (Vector) Ordered List of Values All of the same type Individual elements accessible by index Vector has a Size (Number of elements) 0 1 2 3 4 5 17.3 14.5 3.2 12.0 5.65

More information

Binghamton University. CS-211 Fall Pointers

Binghamton University. CS-211 Fall Pointers Pointers 1 What is a pointer? Says I m not important what s important is over there Points AT or TO something else 2 Memory Array of bytes Each element has a value 0 1 2 3 xffff fffd xffff fffe xffff ffff

More information

Programming for Engineers Arrays

Programming for Engineers Arrays Programming for Engineers Arrays ICEN 200 Spring 2018 Prof. Dola Saha 1 Array Ø Arrays are data structures consisting of related data items of the same type. Ø A group of contiguous memory locations that

More information

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1) Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and

More information

Lesson 7. Reading and Writing a.k.a. Input and Output

Lesson 7. Reading and Writing a.k.a. Input and Output Lesson 7 Reading and Writing a.k.a. Input and Output Escape sequences for printf strings Source: http://en.wikipedia.org/wiki/escape_sequences_in_c Escape sequences for printf strings Why do we need escape

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 ARRAYS ARRAYS An array contains objects of a given type, stored consecutively in a continuous memory block. The individual objects are called the elements of an array. The

More information

Computer Organization & Systems Exam I Example Questions

Computer Organization & Systems Exam I Example Questions Computer Organization & Systems Exam I Example Questions 1. Pointer Question. Write a function char *circle(char *str) that receives a character pointer (which points to an array that is in standard C

More information

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming Arrays CSE101-Lec#17 (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline To declare an array To initialize an array To pass an array to a function Arrays Introduction

More information

Lab 3. Pointers Programming Lab (Using C) XU Silei

Lab 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 information

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop QUIZ: loops Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop QUIZ: loops Write a program that prints the integers from -7 to 15 using: for

More information

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size]; Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University 9/5/6 CS Introduction to Computing II Wayne Snyder Department Boston University Today: Arrays (D and D) Methods Program structure Fields vs local variables Next time: Program structure continued: Classes

More information

CS-220 Spring 2018 Test 1 Version A Feb. 28, Name:

CS-220 Spring 2018 Test 1 Version A Feb. 28, Name: CS-220 Spring 2018 Test 1 Version A Feb. 28, 2018 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : Every function definition in C must

More information

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Outline. 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 information

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays A First Book of ANSI C Fourth Edition Chapter 8 Arrays Objectives One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional

More information

Arrays and Pointers in C. Alan L. Cox

Arrays 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 information

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers (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 information

Array Initialization

Array Initialization Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]

More information

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. 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 information

Pointers as Arguments

Pointers as Arguments Introduction as Arguments How it Works called program on start of execution xw = &i xf = &d after excution xw = &i xf = &d caller program i? d? i 3 d.14159 x 3.14159 x 3.14159 R. K. Ghosh (IIT-Kanpur)

More information

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

Character Strings. String-copy Example

Character Strings. String-copy Example Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #43 Multidimensional Arrays In this video will look at multi-dimensional arrays. (Refer Slide Time: 00:03) In

More information

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook Chapter 5 Section 5.1 Introduction to Strings CS 50 Hathairat Rattanasook "Strings" In computer science a string is a sequence of characters from the underlying character set. In C a string is a sequence

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008 C Programming Language: C ADTs, 2d Dynamic Allocation Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008 Overview Row major format 1 and 2-d dynamic allocation struct

More information

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()

More information

Principles of Programming. Chapter 6: Arrays

Principles of Programming. Chapter 6: Arrays Chapter 6: Arrays In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning values to array elements Reading values from array elements Simple Searching

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses Chapter 16 Pointers and Arrays Based on slides McGraw-Hill Additional material 200/2005 Lewis/Martin Pointers and Arrays We've seen examples of both of these in our LC- programs; now we'll see them in

More information

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays A First Book of ANSI C Fourth Edition Chapter 8 Arrays One-Dimensional Arrays Array Initialization Objectives Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional

More information

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays. CS 2060 Week 5 1 Arrays Arrays Initializing arrays 2 Examples of array usage 3 Passing arrays to functions 4 2D arrays 2D arrays 5 Strings Using character arrays to store and manipulate strings 6 Searching

More information

INTRODUCTION 1 AND REVIEW

INTRODUCTION 1 AND REVIEW INTRODUTION 1 AND REVIEW hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Programming: Advanced Objectives You will learn: Program structure. Program statements. Datatypes. Pointers. Arrays. Structures.

More information

BİL200 TUTORIAL-EXERCISES Objective:

BİL200 TUTORIAL-EXERCISES Objective: Objective: The purpose of this tutorial is learning the usage of -preprocessors -header files -printf(), scanf(), gets() functions -logic operators and conditional cases A preprocessor is a program that

More information

CS C Primer. Tyler Szepesi. January 16, 2013

CS C Primer. Tyler Szepesi. January 16, 2013 January 16, 2013 Topics 1 Why C? 2 Data Types 3 Memory 4 Files 5 Endianness 6 Resources Why C? C is exteremely flexible and gives control to the programmer Allows users to break rigid rules, which are

More information

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016 CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers Kevin Webb Swarthmore College March 1, 2016 Overview Accessing things via an offset Arrays, Structs, Unions How complex structures are stored

More information

ARRAYS(II Unit Part II)

ARRAYS(II Unit Part II) ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful

More information

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

CS 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 information

Memory, Data, & Addressing II CSE 351 Spring

Memory, Data, & Addressing II CSE 351 Spring Memory, Data, & Addressing II CSE 351 Spring 2018 http://xkcd.com/138/ Review Questions 1) If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) a) 64

More information

ESC101N: Fundamentals of Computing End-sem st semester

ESC101N: Fundamentals of Computing End-sem st semester ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.

More information

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA. Arrays Defining arrays, declaration and initialization of arrays Introduction Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical

More information

The Warhol Language Reference Manual

The Warhol Language Reference Manual The Warhol Language Reference Manual Martina Atabong maa2247 Charvinia Neblett cdn2118 Samuel Nnodim son2105 Catherine Wes ciw2109 Sarina Xie sx2166 Introduction Warhol is a functional and imperative programming

More information

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

String constants. /* Demo: string constant */ #include <stdio.h> int main() { Strings 1 String constants 2 /* Demo: string constant */ #include s1.c int main() { printf("hi\n"); } String constants are in double quotes A backslash \ is used to include 'special' characters,

More information

HW1 due Monday by 9:30am Assignment online, submission details to come

HW1 due Monday by 9:30am Assignment online, submission details to come inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers and Arrays Administrivia Buggy Start Lab schedule, lab machines, HW0 due tomorrow in lab 2009-06-24 HW1 due Monday by 9:30am

More information

CS159. Nathan Sprague. September 11, 2015

CS159. Nathan Sprague. September 11, 2015 CS159 Nathan Sprague September 11, 2015 Review of Arrays Declaration: int[] numbers; String[] words; Review of Arrays Declaration: int[] numbers; String[] words; Instantiation: numbers = new int[4]; words

More information

CS240: Programming in C

CS240: 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 information

Arrays and Pointers (part 1)

Arrays 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 information

MODULE 3: Arrays, Functions and Strings

MODULE 3: Arrays, Functions and Strings MODULE 3: Arrays, Functions and Strings Contents covered in this module I. Using an Array II. Functions in C III. Argument Passing IV. Functions and Program Structure, locations of functions V. Function

More information

More Arrays. Last updated 2/6/19

More Arrays. Last updated 2/6/19 More Last updated 2/6/19 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16 3 1 4 rows x 5 columns 2 tj 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16

More information

Dynamic memory allocation

Dynamic memory allocation Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes

More information

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in

More information

C Pointers. 6th April 2017 Giulio Picierro

C Pointers. 6th April 2017 Giulio Picierro C Pointers 6th April 07 Giulio Picierro Functions Return type Function name Arguments list Function body int sum(int a, int b) { return a + b; } Return statement (return keyword

More information

Arrays and Pointers in C & C++

Arrays 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 information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be. CS 156 : COMPUTER SYSTEM CONCEPTS TEST 1 (C PROGRAMMING PART) FEBRUARY 6, 2001 Student s Name: MAXIMUM MARK: 100 Time allowed: 45 minutes Note: unless otherwise stated, the questions are with reference

More information

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Fundamental of C Programming Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Q2. Write down the C statement to calculate percentage where three subjects English, hindi, maths

More information

Multidimension array, array of strings

Multidimension array, array of strings 1 Multidimension array, array of strings char messages[3][7] ={ Hello, Hi, There ; Array of strings 0 1 2 0 1 2 3 4 5 6 H e l l o \0 H i \0 T h e r e \0 Each row (e.g., message[0]) is a char array (string)

More information

Pointers and Dynamic Memory Allocation

Pointers and Dynamic Memory Allocation Pointers and Dynamic Memory Allocation ALGORITHMS & DATA STRUCTURES 9 TH SEPTEMBER 2014 Last week Introduction This is not a course about programming: It s is about puzzling. well.. Donald Knuth Science

More information

Memory Management. CSC215 Lecture

Memory Management. CSC215 Lecture Memory Management CSC215 Lecture Outline Static vs Dynamic Allocation Dynamic allocation functions malloc, realloc, calloc, free Implementation Common errors Static Allocation Allocation of memory at compile-time

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays 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 information

ONE DIMENSIONAL ARRAYS

ONE DIMENSIONAL ARRAYS LECTURE 14 ONE DIMENSIONAL ARRAYS Array : An array is a fixed sized sequenced collection of related data items of same data type. In its simplest form an array can be used to represent a list of numbers

More information

The University Of Michigan. EECS402 Lecture 05. Andrew M. Morgan. Savitch Ch. 5 Arrays Multi-Dimensional Arrays. Consider This Program

The University Of Michigan. EECS402 Lecture 05. Andrew M. Morgan. Savitch Ch. 5 Arrays Multi-Dimensional Arrays. Consider This Program The University Of Michigan Lecture 05 Andrew M. Morgan Savitch Ch. 5 Arrays Multi-Dimensional Arrays Consider This Program Write a program to input 3 ints and output each value and their sum, formatted

More information

Chapter 13. Functions and Parameter Passing (Part 2)

Chapter 13. Functions and Parameter Passing (Part 2) Christian Jacob Chapter 13 Functions and Parameter Passing (Part 2) 13.1 Passing Arguments to Functions 13.1.1 Passing Pointers 13.1.2 Passing Arrays 13.1.3 Passing Strings 13.2 Parameter Passing Mechanisms

More information

Array. Arrays. Declaring Arrays. Using Arrays

Array. Arrays. Declaring Arrays. Using Arrays Arrays CS215 Peter Lo 2004 1 Array Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name Position number Format: arrayname[ position number] First element

More information

C library = Header files + Reserved words + main method

C library = Header files + Reserved words + main method DAY 1: What are Libraries and Header files in C. Suppose you need to see an Atlas of a country in your college. What do you need to do? You will first go to the Library of your college and then to the

More information

CS 61c: Great Ideas in Computer Architecture

CS 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 information

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3). cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion

More information

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 3 4 CMPE 013/L Pointers and Functions Gabriel Hugh Elkaim Spring 2013 Pointers and Functions Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by

More information

Assignment 1 Clarifications

Assignment 1 Clarifications Assignment 1 Clarifications Q1b: State whether having the code snippet in your code will cause compilation errors. Explain why or why not. Q3: For the code snippets, how many times is the printf statement

More information

ECE 15B COMPUTER ORGANIZATION

ECE 15B COMPUTER ORGANIZATION ECE 15B COMPUTER ORGANIZATION Lecture 13 Strings, Lists & Stacks Announcements HW #3 Due next Friday, May 15 at 5:00 PM in HFH Project #2 Due May 29 at 5:00 PM Project #3 Assigned next Thursday, May 19

More information

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019 Hacking in C Pointers Radboud University, Nijmegen, The Netherlands Spring 2019 Allocation of multiple variables Consider the program main(){ char x; int i; short s; char y;... } What will the layout of

More information

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful

More information

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements

More information

Single Dimension Arrays

Single Dimension Arrays ARRAYS Single Dimension Arrays Array Notion of an array Homogeneous collection of variables of same type. Group of consecutive memory locations. Linear and indexed data structure. To refer to an element,

More information

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming Exam 1 Prep Dr. Demetrios Glinos University of Central Florida COP3330 Object Oriented Programming Progress Exam 1 is a Timed Webcourses Quiz You can find it from the "Assignments" link on Webcourses choose

More information

Integer Representation Floating point Representation Other data types

Integer Representation Floating point Representation Other data types Chapter 2 Bits, Data Types & Operations Integer Representation Floating point Representation Other data types Why do Computers use Base 2? Base 10 Number Representation Natural representation for human

More information

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal MA 511: Computer Programming Lecture 3: http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html Partha Sarathi Mandal psm@iitg.ernet.ac.in Dept. of Mathematics, IIT Guwahati Semester 1, 2010-11 Last

More information

Brook+ Data Types. Basic Data Types

Brook+ Data Types. Basic Data Types Brook+ Data Types Important for all data representations in Brook+ Streams Constants Temporary variables Brook+ Supports Basic Types Short Vector Types User-Defined Types 29 Basic Data Types Basic data

More information

C-string format with scanf/printf

C-string format with scanf/printf CSI333 Lecture 4 C-string format with scanf/printf char mycstring[4]; int intvar; scanf("%3s", &intvar ); /*reads up to 3 chars and stores them PLUS \0 in the 4-byte var. intvar*/ scanf("%3s", mycstring);

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #33 Pointer Arithmetic In this video let me, so some cool stuff which is pointer arithmetic which helps you to

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of

More information

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Pointers and Arrays

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Pointers and Arrays Chapter 16 Pointers and Arrays Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, S. Rajopadhye Colorado State University Pointers and Arrays! We ve seen examples

More information

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays 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 information

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = abc;! How do you tell how long a string is?! CS61C L04 Introduction to C (pt 2) (1)! Reference slides! C Strings! You ARE responsible for the material on these slides (they re just taken from the reading anyway). These were the slides that generated

More information

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 5 C Structs & Memory Mangement 1/27/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L05 C Structs (1) C String Standard Functions

More information

Fall 2018 Discussion 2: September 3, 2018

Fall 2018 Discussion 2: September 3, 2018 CS 61C C Basics Fall 2018 Discussion 2: September 3, 2018 1 C C is syntactically similar to Java, but there are a few key differences: 1. C is function-oriented, not object-oriented; there are no objects.

More information

Arrays and Pointers (part 1)

Arrays 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 information

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers. 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 information

Pointers (continued), arrays and strings

Pointers (continued), arrays and strings Pointers (continued), arrays and strings 1 Last week We have seen pointers, e.g. of type char *p with the operators * and & These are tricky to understand, unless you draw pictures 2 Pointer arithmetic

More information

Programming Studio #9 ECE 190

Programming Studio #9 ECE 190 Programming Studio #9 ECE 190 Programming Studio #9 Concepts: Functions review 2D Arrays GDB Announcements EXAM 3 CONFLICT REQUESTS, ON COMPASS, DUE THIS MONDAY 5PM. NO EXTENSIONS, NO EXCEPTIONS. Functions

More information

CS 241 Data Organization Pointers and Arrays

CS 241 Data Organization Pointers and Arrays CS 241 Data Organization Pointers and Arrays Brooke Chenoweth University of New Mexico Fall 2017 Read Kernighan & Richie 6 Structures Pointers A pointer is a variable that contains the address of another

More information

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 Cyrus Bazeghi POINTERS AND ARRAYS We have briefly seen these before, here are the details Pointer Array Address of a variable in memory Allows

More information

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 220: Computer Systems & Programming Expressions and Operators in C (Partially a Review) Expressions are Used

More information

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal Decimal Representation Binary Representation Can interpret decimal number 4705 as: 4 10 3 + 7 10 2 + 0 10 1 + 5 10 0 The base or radix is 10 Digits 0 9 Place values: 1000 100 10 1 10 3 10 2 10 1 10 0 Write

More information