Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Strings ( מחרוזות ). 2 Agenda Definition and initialization Termination Input / Output String library.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Arrays and Pointers in C Alan L. Cox
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Introduction to C programming
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
C Lab 1 Introduction to C. Basics of C Developed by Dennis Ritchie in the 1970s. Maps very easily to machine instructions. Even allows inline assembly!
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topic 4: C Data Structures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Arrays and Pointers.
Strings, Pointers and Tools
Today’s Material Strings Definition Representation Initialization
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Computer Organization and Design Pointers, Arrays and Strings in C
ECE Application Programming
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
C Programming Tutorial – Part I
CSE 303 Concepts and Tools for Software Development
CSE 303 Lecture 14 Strings in C
Strings A string is a sequence of characters treated as a group
Lecture 6 C++ Programming
CS111 Computer Programming
Lecture 8b: Strings BJ Furman 15OCT2012.
EECE.2160 ECE Application Programming
Homework Continue with K&R Chapter 5 Skipping sections for now
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
EECE.2160 ECE Application Programming
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
SPL – PS2 C++ Memory Handling.
Memory, Arrays & Pointers
Introduction to C CS 3410.
Presentation transcript:

Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable] Declare an array with an initializer list int nums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Note: Don't need a number between [ and ] Access elements like primitive variables printf ("%d\n", nums[i]); Declare in a function: void foo(int nums[]) or void foo(int *nums)

Pointer arithmetic int i, nums[6]; for (i=0; i<6; i++) nums[i] = i*10; int *np = nums; *(np+3) = 33; nums[5] = 66; *(&np[2]) = 22; int x = &(np[2]) - &(np[0]) Address Content 2 x Nums Note: *(nums + 1) is not equal to *nums + 1

Accessing Arrays using pointers Declaring in a function: void foo(char *data) Accessing the 10 th element printf("%c\n", *(data + 9)); Another way char *ptr = data+9; printf("%c\n", *ptr); Declare: int *x; –Be careful, there is no memory allocated –C does no memory bound checks An array is just a pointer to a contiguous block of memory

Notes on Pointers C Programmers often favor pointers to process arrays (less typing) You can use all the relational operators with pointers (, ==, etc.) You can use all the arithmetic operations (++, +=, --, etc.) The size in bytes of pointers can be obtained using the sizeof operator

Examples with functions Declaration void foo (const int nums[], int s) { int i=0, sum=0; for (i=0; i<s; i++) sum += nums[i]; printf("%d\n", sum); } Call int nums[] = {1, 2, 3}; foo(nums, 3); Declaration void foo(const int *nums, int s) { int sum=0, *ptr; for(ptr=nums; ptr-nums<s; ptr++) sum += *ptr; printf("%d\n", sum); } Call int nums[] = {1, 2, 3}; foo(nums, 3); Note: The const modifier is not required, some arrays can be changed

Strings In C –A string is an array of characters (C has no String type) –The entire array may not be filled –Unlike Java, strings are mutable –The string is terminated by a null ('\0') character The hard way: char data[6]; data[0] = 'a'; data[1]= 'b'; data[2] = 'c'; data[3] = '\0'; Declaring a string using a literal: char[] data = "abc"; Replace the third character: data[2] = 'd'; or *(data+2) = 'd'; Note: 'a' is not "a". Question: How do they differ? 'a''b''c''\0'???

Inputting Strings (scanf with %s) Note: scanf reads till it sees white space Example:char s[2]; scanf("%s", s); Problem: inputting "ab" stores '\0' outside the bounds of the array Result: possible "Segmentation Fault" Solution: Be sure to define enough space Another Solution: Use fgets (later topic) 'a''b' '\0'

String Output Print entire string: printf("%s\n", str); Character by character (a line each) int i; for (i=0; str[i]!='\0'; i++) printf("%c\n", str[i]); Another way int i, len = strlen(str); for (int i=0; i<len; i++) printf("%c\n", str[i]);

String Functions Header file: string.h Length of a string (excluding the null ('\0')) int strlen: int strlen(const s[]) Copy from one string to another char* strcpy(char toStr[], const char fromStr[]) Compare Strings: like the Java Comparable interface int strcmp(const char s1[], const char s2[]) Concatenate a string char* strcat(char toStr[], const char fromStr[]) Notes: –Make sure that the destination string is big enough –There are many more string functions than these –Some (not all) systems include string functions in stdio.h –If there is no null ('\0') bizarre things can happen

Header Files Look usual places: #include Look in local folder: #include "header.h" Put in your header files: –includes: #include –constants: #define –other preprocessing directives: #ifndef (see next slide)

#ifndef Problem: If more than one header refers to stdio, it will be included twice Solution: use "if not defined directive" #ifndef UNIQUENAME #define UNIQUENAME #endif file1.h #define MAX 5 file2.h #include "file1.h" prog.c #include file1.h #include file2.h MAX is defined twice

GDB Debugger Compile: gcc –g main.c func.c –o main Invoke: gdb main Popular commands: –break point: break or break –List break points: break or list watches: display –List source: list or list –Step into: step or Step over: next or Continue: cont –Display variables and expressions: print –Create a watch: display –Run the program: run or Exit debugger: quit –Delete watch: delete display # or Delete breakpoint delete # –Current stack record: where and Caller's stack record: up –GDB Help: help or help command Note: There are cheat sheets available that can help (see class web site)