240-222 CPT: Arrays of Pointers/121 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.

Slides:



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

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.
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
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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:
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
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.
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.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
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]
Arrays, Pointers and Strings Chapter 6 in ABC. One Dimensional Arrays #defineN100 int a[N]; for ( i = 0; i< N; ++i ) sum += a[i]; space for a[0],...,
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",
Pointers and Arrays C and Data Structures Baojian Hua
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Outline Midterm results Static variables Memory model
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
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.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 3 – Pointers.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
Functions & Pointers in C Jordan Erenrich
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
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;
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
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.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
Arrays and Pointers (part 2) CSE 2031 Fall March 2016.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
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.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Memory allocation & parameter passing
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
C Primer.
Characters and Strings
Command Line Arguments
Command Line Arguments
Understand argc and argv
Arrays and Pointers CSE 2031 Fall September 2018.
Pointers.
Pointers.
Dynamic Memory Allocation
Pointers.
Pointers.
Outline Defining and using Pointers Operations on pointers
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
(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)
Characters and Strings
15213 C Primer 17 September 2002.
Presentation transcript:

CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays of pointers and contrast them with 2D arrays 12. Arrays of Pointers

CPT: Arrays of Pointers/122 Overview: 1.An Array of Pointers 2.A 2D Array of Characters 3.An Array of char* 4.Sorting Words 5.Dynamic Memory Allocation 6.A Dynamic Array 7.Static versus Dynamic Memory 8. main() Arguments

CPT: Arrays of Pointers/ An Array of Pointers int *b[10]; int x[3], y[35]; : : b[0] = x;b[1] = y;

CPT: Arrays of Pointers/ A 2D Array of Characters char names[][5] = { "Bob", "Jo", "Ann", "Fred"}; printf("%s", names[0]); ‘B’‘o’‘b’‘\0’ ‘J’‘o’‘\0’ ‘A’‘n’ ‘\0’ ‘F’‘r’‘e’‘d’‘\0’ names

CPT: Arrays of Pointers/ An Array of char * Sec 7.9 / 7.8 char *names[] = {"Augustin", "Ann", "Bob", "Freddy"} printf("%s", names[0]); A ugustin\0 Ann Bob Freddy names[0] names[1] names[2] names[3] names

CPT: Arrays of Pointers/ Sorting Words test.dat : –A is for apple or alphabet pie which all get a slice of, come taste it and try. l Sort the words: $ sort_words < test.dat l Output: A a all alphabet... which

CPT: Arrays of Pointers/127 sort_words.c #include #include #include #define MAXWORD 50 /* max word length */ #define SIZE 1000 /* array size */ void sort_words(char [][MAXWORD], int); void string_swap(char **, char **); : continued

CPT: Arrays of Pointers/128 int main() { char w[SIZE][MAXWORD]; /* array of fixed length strings */ int num, i; for (i=0; scanf("%s", w[i]) == 1; i++){ if (i >= SIZE) { printf("Too many Words!"); exit(1); } } : : continued

CPT: Arrays of Pointers/129 : num = i; sort_words(w, num); for (i=0; i < num; i++) printf("%s\n", w[i]); return 0; }

CPT: Arrays of Pointers/ Dynamic Memory Allocation sort_words.c can save space by defining the size of each w[i] dynamically at run time. l New data structure: char *w[SIZE]; l Make a pointer to a block of memory using: calloc(, )

CPT: Arrays of Pointers/1211 New Top Level #include #include #include #define MAXWORD 50 /* max word length */ #define SIZE 1000 /* array size */ void sort_words(char *[], int); void string_swap(char **, char **); : continued

CPT: Arrays of Pointers/1212 int main() { char *w[SIZE]; /* array of pointers */ char word[MAXWORD]; /* work space */ int num, i; for (i=0; scanf("%s", word) == 1; i++){ if (i >= SIZE) { printf("Too many Words!"); exit(1); } w[i] = calloc(strlen(word)+1, sizeof(char)); strcpy(w[i], word); } : continued

CPT: Arrays of Pointers/1213 : num = i; sort_words(w, num); for (i=0; i < num; i++) printf("%s\n", w[i]); return 0; }

CPT: Arrays of Pointers/1214 Some Comments calloc(strlen(word)+1, sizeof(char)) cannot just use strcpy(w[i], word)

CPT: Arrays of Pointers/1215 void sort_words(char *w[], int n) /* n elements are to be sorted */ /* similar to bubble sort */ { int i, j; for(i = 0; i 0) string_swap(&w[i], &w[j]); }

CPT: Arrays of Pointers/1216 In picture form: Augustin\0 Ann Bob Freddy w[0] w[1] w[2] w[3] w

CPT: Arrays of Pointers/1217 void string_swap(char **p, char **q) /* swap the strings using pointers */ { char *temp; temp = *p; *p = *q; *q = temp; }

CPT: Arrays of Pointers/1218 In picture form: Augustin\0 Ann w[0] w[1] Augustin\0 Ann w[0] w[1] Before After

CPT: Arrays of Pointers/ A Dynamic Array #include #include int main() { int *a; /* will point to the array */ int i, size, sum = 0; printf("An array will be created dynamically. Input an array size followed by values: "); : continued

CPT: Arrays of Pointers/1220 scanf("%d", &size); /* allocate space in a for size int's */ a = (char *)malloc(size * sizeof(int)); for (i=0; i < size; i++) scanf("%d", &a[i]); for(i=0; i < size; i++) sum += a[i]; printf("Sum is %d\n", sum); free(a); return 0; }

CPT: Arrays of Pointers/ Static versus Dynamic Memory l The choice between static or dynamic memory is usually based on if you know how much data will be stored in your program. l If you know that an array of 100 integers is required then declare: int data[100]; l If you do not know, then consider creating dynamic memory.

CPT: Arrays of Pointers/ main() Arguments /* my_echo.c: Echo command line input */ #include int main(int argc, char *argv[]) { int i; printf("argc = %d\n", argc); for (i = 0; i <= argc-1; ++1) printf("argv[%d] = %s\n", i, argv[i]); return 0; }

CPT: Arrays of Pointers/1223 Compilation and Execution: $ gcc -Wall -o my_echo my_echo.c $ my_echo a is for apple argc = 5 argv[0] = my_echo argv[1] = a argv[2] = is argv[3] = for argv[4] = apple

CPT: Arrays of Pointers/1224 Pictorially $ my_echo hello, world produces: my_echo\0 world argv[0] argv[1] argv[2] argv[3] hello,\0 NULL argv