Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
CS61C L4 C Pointers (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 –C Strings,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Functions & Pointers in C Jordan Erenrich
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
CSC Programming for Science Lecture 34: Dynamic Pointers.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
Arrays and Pointers (part 2) CSE 2031 Fall March 2016.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Stack and Heap Memory Stack resident variables include:
5.13 Recursion Recursive functions Functions that call themselves
C Primer.
Command Line Arguments
Command Line Arguments
2016.
Arrays and Pointers CSE 2031 Fall September 2018.
Programming Languages and Paradigms
14th September IIT Kanpur
C Programming Language
Dynamic Memory Allocation
prepared by Senem Kumova Metin modified by İlker Korkmaz
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
CS111 Computer Programming
7. Pointers, Dynamic Memory
C Programming Lecture-8 Pointers and Memory Management
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Arrays and Pointers (part 2)
Arrays, Pointers, and Strings
Arrays and Pointers (part 2)
15213 C Primer 17 September 2002.
Presentation transcript:

Arrays, Strings, and Memory

Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc; i++) { printf("%2d %s\n", (i+1), argv[i]); } // End for return(0); } // End main

Example Input and Output Arg# Contents 1 a.exe 2 Apple Cat Eagle g H:\>a.exe Apple 5 Cat 26.3 Eagle g

argv Array Contents argc argv 100: 102: a.exe\0 Apple\0 5\0 Cat\0 26.3\0 Eagle\ \0 g\0

Memory Contents a.exe\0Apple 5 Cat 26.3 Eagle g : 120: 140: 160: 180: 200: 220: 240: 260: 280: 300: 320:

Pointer Arithmetic int main(int argc, char *argv[]) { int i; char *programName; char *argNameA; char *argNameB; programName = argv[0]; argNameA = argv[1]; argNameB = *(argv + 1); printf("%s %s %s\n", programName, argNameA, argNameB); argNameA = argv[4]; argNameB = *(argv + 4); printf("\n%s %s\n", argNameA, argNameB); return(0); } // End main

Importance of Range Checking int main(void) { int sum = 50; int valuesTable[5] = {3, 6, 9, 12, 15}; int cost = 100; valuesTable[0] = 13; *(valuesTable + 0) = 13; *(valuesTable + 2) = 19; valuesTable[-1] = 36; // Puts 36 in the sum variable *(valuesTable - 1) = 36; // Also puts 36 in the sum variable printf("%d %d %d\n", &sum, &(valuesTable[20]), &cost); printf("Cost figure: %d\n", cost); printf("Sum figure: %d\n", sum); } // End main

Dynamic Memory Allocation malloc() – allocates a data object and returns a pointer to it calloc() – allocates an array data object and returns a pointer to it free() – deallocates the data object whose address is the pointer sizeof – returns the size in bytes of a data object

Dynamic Memory Allocation using malloc int main(void) { int a = 27; float b = ; int *xPtr; float *yPtr; xPtr = (int *) malloc( sizeof(int) ); *xPtr = a; yPtr = (float *) malloc( sizeof(float) ); *yPtr = b; printf("%d %.2f\n", a, b); printf("%d %.2f\n", *xPtr, *yPtr); free(xPtr); free(yPtr); return(0); } // End main

Dynamic Memory Allocation using calloc int initializeAdjacentStatesList(char *adjacentList[]) { int i; for (i = 0; i < MAX_ADJACENT_STATES; i++) { // Allocate memory for each character string element // of the adjacentList adjacentList[i] = calloc(STATE_CODE_LENGTH, sizeof(char)); strcpy(adjacentList[i], ZZ_STATE); } // End for return(0); } // End initializeAdjacentStatesList Example