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.

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

ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
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:
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
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.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
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",
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
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.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
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”
ECE Application Programming
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
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;
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
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.
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.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Stack and Heap Memory Stack resident variables include:
C Primer.
Day 03 Introduction to C.
Day 03 Introduction to C.
Dynamic Memory Allocation
Some examples.
Pointers and dynamic memory
CSC215 Lecture Memory Management.
Programming with Pointers
EECE.2160 ECE Application Programming
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Chapter 16 Pointers and Arrays
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Arrays, Pointers, and Strings
Dynamic Memory – A Review
15213 C Primer 17 September 2002.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Chapter 16 Pointers and Arrays
Presentation transcript:

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 –Both of the above functions must have void return types –Print out the doubled value in main

Solution int main(void) { double value; inputDataAndDouble(&value); printf(“Your value doubled is %f.”, value); return 0; } void inputDataAndDouble(double *num) { printf(“Enter a number and I will double it: “); scanf(“%lf”, num); /* Why not &num? */ doubleValue(num); /* Why not &num or *num? */ } void doubleValue(double *n) { *n = *n * *n; }

Pointers What gets printed? int main(void) { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z); return 0; }

Arrays, and Pointers Pointer and Array equivalence –Array name is “pointer” to first element in array int x[] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x; *y = x[3]; *x = y[6]; y[4] = *x; y = &x[4]; *y = x[9]; for (i = 0; i < 10; i++) printf(“%d “, x[i]);

Pointer Arithmetic int x[] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x + 1; ++y; ++x; /* can’t do */ *y = ++x[3]; *(y+1) = x[5]++; *x = y[6]; y[4] = *x; y = &*(x+4) *y = *(x + 9);

Passing Arrays as Arguments C passes arrays by reference –the address of the array (i.e., of the first element) is passed to the function –otherwise, would have to copy each element main() { int numbers[MAX_NUMS], size; … size = getValues(numbers); mean = Average(numbers, size); … } int Average(int inputValues[], int size) { … for (index = 0; index < size; index++) sum = sum + indexValues[index]; return (sum / size); }

16-7 Arrays of Pointers char *names[] = {“hello”,”how”,”are”,”you?”}; What does this look like?

16-8 char *names[] = {“hello”,”how”,”are”,”you?”}; char *word = names[1]; char **all = names+1; What are the values of the following? names[3][1] **names *(names[0]+3) *(++word) *(*(++all)+1)

Returning arrays from functions Assume that str is never more than 128 characters What is wrong with this function? How can it be fixed? char *copyString(char *str) { char buffer[128]; int index = 0; while ((str[index] != ‘\0’) && (index < 128)) {buffer[index] = str[index]; index++;} return buffer; }

Dynamic Memory Allocation void *malloc(size_t size) –malloc allocates size number of bytes in memory and returns a pointer to it. The memory is not cleared. –Use: –Use malloc to fix copyString Make size flexible What must caller do with return value when done with it? char *line; int *x; line = (char *) malloc (sizof(char) * 80); x = (int *) malloc(sizeof(int));

Dynamic Memory Allocation void *calloc(size_t nelm, size_t size) –calloc allocates size * nelm number of bytes in memory and returns a pointer to it. Memory is zeroed out. –Use: char *line; int *x; line = (char *) calloc (80, sizof(char)); x = (int *) calloc(1, sizeof(int));

Dynamic Memory Allocation void *realloc(void *ptr, size_t size) –realloc changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. –Use: See Example

Dynamic Memory Allocation Memory Layout Activation Record Code Static Data Stack Heap

Dynamic Memory Allocation void free(void *ptr) –free releases the memory pointed to by ptr. The memory must have been created by malloc or one of its kind. –Use: char *line; line = (char *) calloc (80, sizof(char)); free(line);

Example #include #define INCREMENT 80 char *getLine(void); int main(void) { char *line = getLine(); /* no limit to the size of line */ printf("%s\n", line); free(line); return 0; }

char *getLine(void) { char *line, c; int size = sizeof(char) * INCREMENT; int i = 0; line = (char *)malloc(size); while((c = getchar()) != EOF && c != '\n') { line[i] = c; if (i >= size - 2) { size += INCREMENT; line = (char *)realloc(line, size); } i++; } line[i] = '\0'; return line; }

1 #include 2 #include 3 4 int main() { 5 char **data; /* same as char char *data[] */ 6 int num, i; 7 8 printf("How many items: "); 9 scanf("%d",&num); data = (char **) calloc(num, sizeof(char *)); 12 for(i=0; i<num; i++) 13 data[i] = (char *) calloc( 20, sizeof(char)); for(i=0; i<num; i++) { 16 printf("Enter item %d: ", i+1); 17 scanf("%s", data[i]); 18 } printf("You entered:\n"); 21 for(i=0; i<num; i++) 22 printf("%s ", data[i]); 23 printf("\n"); return 0; 26 }

Working With Strings Write version of strcat –Treating string parameters as arrays –Treating string parameters as pointers

Command Line Arguments main(int argc, char *argv[]) So, if your program is invoked as: % a.out one two three these parameters would look like:

#include int main(int argc, char *argv[]) { int i; for ( i = 0; i < argc; i++ ) { printf("%s ", argv[i]); } printf("\n"); return 0; }

#include int main(int argc, char *argv[]) { while (argc--) printf("%s ", *argv++); printf("\n"); return 0; }