Array Initialization

Similar documents
Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Arrays and Pointers in C. Alan L. Cox

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Dynamic memory allocation

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Pointers. Pointers. Pointers (cont) CS 217

Pointers. Pointer References

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

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

Dynamic memory allocation (malloc)

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

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

ARRAYS(II Unit Part II)

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

Arrays and Pointers (part 2) Be extra careful with pointers!

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

Arrays and Pointers (part 2) Be extra careful with pointers!

Approximately a Test II CPSC 206

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Personal SE. Functions, Arrays, Strings & Command Line Arguments

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

Programming for Engineers Arrays

CSC 2400: Computer Systems. Arrays and Strings in C

Computers Programming Course 10. Iulian Năstac

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

8. Characters, Strings and Files

Computer Programming: Skills & Concepts (CP) Strings

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Introduction to Programming Block Tutorial C/C++

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

CS1100 Introduction to Programming

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

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

Language comparison. C has pointers. Java has references. C++ has pointers and references

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Fundamental of Programming (C)

Quick review pointer basics (KR ch )

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

This is CS50. Harvard University Fall Quiz 0 Answer Key

Arrays, Strings, & Pointers

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

PRINCIPLES OF OPERATING SYSTEMS

Course organization. Course introduction ( Week 1)

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

CSC 2400: Computer Systems. Arrays and Strings in C

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

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

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Arrays, Pointers, and Strings

File IO and command line input CSE 2451

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

CS 61c: Great Ideas in Computer Architecture

Fundamentals of Programming

CSC 270 Survey of Programming Languages. C-String Values

C programming basics T3-1 -

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

ECE 15B COMPUTER ORGANIZATION

Common Misunderstandings from Exam 1 Material

Characters, Character Strings, and string-manipulation functions in C

Arrays and Strings. Arash Rafiey. September 12, 2017

Pointers, Arrays, and Strings. CS449 Spring 2016

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

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Fundamental of Programming (C)

Strings. Steven R. Bagley

CSE2301. Arrays and Pointers. These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University. Arrays

CSE2301. Arrays. Arrays. Arrays and Pointers

Arrays, Strings, and Pointers

Programming in C. What is C?... What is C?

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

Programming in C UVic SEng 265

Arrays, Strings, and Pointers

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Programming in C. What is C?... What is C?

AMCAT Automata Coding Sample Questions And Answers

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Programming for Electrical and Computer Engineers. Pointers and Arrays

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

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

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

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

BSM540 Basics of C Language

Arrays, Strings, and Pointers

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

ONE DIMENSIONAL ARRAYS

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

Computer Science & Engineering 150A Problem Solving Using Computers

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Lecture 04 Introduction to pointers

CS61C : Machine Structures

Transcription:

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] to 5, etc. Initialization is permitted (in ANSI C) for arrays of both static and auto storage classes. Array initializers must be compile-time constants. If not enough initial values are specified, the remainder are initialized to zero.

Arrays and Pointers Arrays and pointer variables have similar properties: int a[10], *p, x; p = a; x = p[3]; a[3] = x; There is an important difference, though: pointer variables are variables, arrays are constants. Thus, a = p; /* ILLEGAL! */

Array Arguments Arrays can be passed as actual parameters to functions. corresponding formal parameters are pointer variables. The #include <stdio.h> int main(void) { int i; char *a[] = { "The", "quick", "brown", "fox", "jumped" ; void print(int i, char *x[]); for(i = 0; i < sizeof(a)/sizeof(char *); i++) print(i, a); printf("\n"); void print(int i, char **x) { printf("%s ", x[i]);

An equivalent way of declaring the print function is as follows: void print(int i, char *x[]) { printf("%s ", x[i]);

Multidimensional Arrays C (like Java) supports two- and higher-dimensional arrays. A two-dimensional array is just an array of arrays. int a[10][20]; The array a is stored in a contiguous region of memory, in row major order. &a[0][0] == a ( decay to pointer convention) &a[i][0] == a[i] ( decay to pointer convention) a and a[i] are constants. A three-dimensional array is an array of two-dimensional arrays, a four-dimensional array is an array of three-dimensional arrays, and so on.

Arrays of Pointers Though C syntax makes them appear similar, a one-dimensional array of pointers is a different sort of thing than a two-dimensional array. char a[10][20]; /* A two-dimensional array */ char *p[10]; /* An array of (uninitialized) pointers */ The quantities a[0], a[1],..., a[9] are constants, which require no initialization. The quantities p[0], p[1],..., p[9] are variables, which must be initialized before use. a occupies 10*20*sizeof(char) bytes of storage. p occupies 10*sizeof(char *) bytes of storage.

In both cases, C allows two subscripts to be supplied: a[3][12] = 0; p[3][12] = 0; but the latter is only meaningful if p[3] has been initialized to point to a char[] array with at least 13 entries.

Strings In C, strings are represented as arrays of characters. The end of a string is indicated by the null character ( \0 ). The number of elements of the array used to hold a string must be at least one more than the length of the string. String literals (e.g. "Hello world!\n") are actually pointers to a static array that is initialized at the beginning of program execution. String manipulation generally involves extensive use of pointers.

A Common Mistake A common mistake is to try to input a string without previously having allocated storage: /* Maximum length of an input line */ #define MAXLINE 132 int main(void) { char *str; printf("enter your name: "); scanf("%s", str); /* ERROR! */ printf("hello there, %s!\n", str); The pointer variable str is uninitialized, and no storage has been allocated to hold the input string!

String Library Functions The C library provides a number of library functions for manipulating strings: int strlen(char *s); Returns the length of the string s. int strcmp(char *s1, char *s2); Compares s1 to s2, and returns negative (less), 0 (equal), or positive (greater), based on lexicographic ordering. char *strcpy(char *dst, char *src); Copies string src into area pointed to by dst (must be large enough!).

char *strcat(char *dst, char *src); Concatenates string src with string dst, leaving the result in area pointed to by dst (must be large enough!). char *strchr(char *s, int c); Finds the first occurrence of c in the string s. pointer to the located character, if any, else NULL. Returns a To use these, #include <string.h>.

String Manipulation using Pointers The manipulation of strings generally makes heavy use of pointer variables. Often these are used to march through a string from beginning to end. /* * Determine the length of a string */ int length(char *s) { int n = 0; while(*s++!= \0 ) n++; return n;

/* * Convert a string into lower-case */ void lower_caseify(char *s) { for( ; *s!= \0 ; s++) { if(isupper(*s)) *s = tolower(*s);

/* * Reverse a string in place */ void reverse(char *s) { char *e, c; for(e = s; *e!= \0 ; e++) /* skip */; e--; for( ; s < e; s++, e--) { c = *e; *e = *s; *s = c;

Arguments to main() The main() function can be declared as follows: int main(int argc, char *argv[]); argc is the argument count, which is always 1. argv is an array of pointers to strings, which are the actual arguments. argv[0] is (by convention) the name of the program being run. argv[1], argv[2],..., argv[argc-1] are the command-line arguments (as would be available in Java String[] args argument to main). argv[argc] is NULL.

For example, if you invoke an executable C program as follows: $ myprog myarg1 myarg2 myarg3 then: argc will be 4, argv[0] will be "myprog", argv[1] will be "myarg1", argv[2] will be "myarg2", argv[3] will be "myarg3", and argv[4] will be NULL.