Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
Dynamic memory allocation
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
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.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 6 Data Types
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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:
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int.
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]
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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)
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Introduction to Data Structures Systems Programming Concepts.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Charles Clute Tom Most Michael Hein. Strings in C  There is no String... But there’s hope! Strings are character arrays char volume[6]; char volume[6]
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
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.
More Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Allocating Arrays Dynamically You allocate an array by.
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 Nine Strings. Char vs String Literals Size of data types: Size of data types: –sizeof(“hello\n”)7 bytes –sizeof(“hello”)6 bytes –sizeof(“X”)2.
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.
Pointers, Arrays, And Dynamic Memory Allocation by Bindra Shrestha CSCI 3333 Data Structures.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Pointers and Dynamic Arrays
Programming Languages and Paradigms
Basic notes on pointers in C
14th September IIT Kanpur
Pointers.
Outline Defining and using Pointers Operations on pointers
CS111 Computer Programming
C++ Pointers and Strings
C Programming Lecture-8 Pointers and Memory Management
C++ Pointers and Strings
Dynamic Memory – A Review
Presentation transcript:

pointer, malloc and realloc 1

Name entered was 6 char, not enough space to put null terminator 2 Array of char

3 Pointer to array of char

3.cpp(15) : error C2440: '=' : cannot convert from 'int' to 'int *' 4 Pointer to array name[] vs Pointer to variable age (wrong version)

4.cpp(14) : warning C4313: 'printf' : '%d' in format string conflicts with argument 1 of type 'int *' 5 Pointer to array name[] vs Pointer to variable age (correct version)

6 Access address and value pointed by pointer to array name[] Access address and value pointed by pointer to variable age

7 strlen()

8 nameptr points to an empty offset (with size 5 bytes) allocate new memory using malloc, determine no of offset by strlen name azlan [0][1][2][3][4] nameptr

9 char *strcpy(char *destination, const char *source); The argument order mimics that of an assignment: destination "=" source. The return value is destination strcpy() – copy value from array to offset name azlan [0][1][2][3][4] nameptr azlan

10 Null terminator nameptr azlan [0][1][2][3][4] \0 [5] Use for loop to go to each offset

11 azlan [0][1][2][3][4] nameptr Go to the next offset nameptr++ azlan [0][1][2][3][4] [0][1][2][3] nameptr++ will makes nameptr point to the next offset

12 azlan [0][1][2][3][4] name azlan [0][1][2][3][4] nameptr 12ff58 12ff4c ff5912ff5a12ff5b12ff5c &name = &name[0] = 12ff58 Line 16, create 1 byte x 5 offset Line 17, copy string from name to nameptr Check the address for each array name[] element and nameptr offset

13 azlan [0][1][2][3][4] name 12ff5812ff5912ff5a12ff5b12ff5c &name = &name[0] = 12ff58 azlan [0][1][2][3][4] tempptr 12ff4c [0] listptr 12ff **listptr – pointer to pointer

14 [0] ptr 12ff60 364d68 [0] [0] d68 [0] 3631b [1] 3631b [2] 3631b8 listptr 12ff54 realloc() – continue create a new offset, to point to other address

e8 3631b0 3631ec f0 listptr 12ff14 a [0] ptr l [1] i [2] c [0] 3631b0 h [1] 3631b1 o [2] 3631b2 n [3] 3631b3 g [4] 3631b4 m [0] u [1] t [2] h [3] u [4] [0][1][2] when offset=0 when offset=1 when offset=2 Line 11-create new malloc, line 12-copy value from name[] to ptr, line 18-create new offset, line 15 and 19-offset points to where ptr points