A solid understanding of pointers and the ability to effectively use them separates a novice C programmer from a more experienced one. Pointers pervade the language and provide much of its flexibility. They provide important support for dynamic memory allocation, are closely tied to array notation, and, when used to point to functions, add another dimension to flow control in a program. Pointers have long been a stumbling block in learning C. The basic concept of a pointer is simple: it is a variable that stores the address of a memory location. The concept, however, quickly becomes complicated when we start applying pointer operators and try to discern their often cryptic notations. But this does not have to be the case. If we start simple and establish a firm foundation, then the advanced uses of pointers are not hard to follow and apply. The key to comprehending pointers is understanding how memory is managed in a C program. After all, pointers contain addresses in memory. If we don’t understand how memory is organized and managed, it is difficult to understand how pointers work. To address this concern, the organization of memory is illustrated whenever it is useful to explain a pointer concept. Once you have a firm grasp of memory and the ways it can be organized, understanding pointers becomes a lot easier. This chapter presents an introduction to pointers, their operators, and how they interact with memory. The first section examines how they are declared, the basic pointer oper‐ ators, and the concept of null. There are various types of “nulls” supported by C so a careful examination of them can be enlightening. The second section looks more closely at the various memory models you will un‐ doubtedly encounter when working with C. The model used with a given compiler and operating system environment affects how pointers are used. In addition, we closely examine various predefined types related to pointers and the memory models.
1
Pointer operators are covered in more depth in the next section, including pointer arithmetic and pointer comparisons. The last section examines constants and pointers. The numerous declaration combinations offer many interesting and often very useful possibilities. Whether you are a novice C programmer or an experienced programmer, this book will provide you with a solid understanding of pointers and fill the gaps in your education. The experienced programmer will want to pick and choose the topics of interest. The beginning programmer should probably take a more deliberate approach.
Pointers and Memory
When a C program is compiled, it works with three types of memory: Static/Global Statically declared variables are allocated to this type of memory. Global variables also use this region of memory. They are allocated when the program starts and remain in existence until the program terminates. While all functions have access to global variables, the scope of static variables is restricted to their defining func‐ tion. Automatic These variables are declared within a function and are created when a function is called. Their scope is restricted to the function, and their lifetime is limited to the time the function is executing. Dynamic Memory is allocated from the heap and can be released as necessary. A pointer references the allocated memory. The scope is limited to the pointer or pointers that reference the memory. It exists until it is released. This is the focus of Chapter 2. Table 1-1 summarizes the scope of and lifetime of variables used in these memory regions. Table 1-1. Scope and lifetime
Scope Global Static Dynamic The entire file The function it is declared within Lifetime The lifetime of the application The lifetime of the application While the function is executing
Automatic (local) The function it is declared within
Determined by the pointers that reference this memory Until the memory is freed
Understanding these types of memory
Related Documents: Essay on Understanding and Using C Pointers
8 Name School THE UNIVERSITY OF THE STATE OF NEW YORK GRADE 8 INTERMEDIATE-LEVEL TEST SCIENCE WRITTEN TEST JUNE 2003 The questions on this test measure your knowledge and understanding of science. The test has two parts. Both parts are contained in this test booklet. Part I consists of 45 multiple-choice questions. Record your answers to these questions on the separate answer sheet. Use only a No. 2 pencil on your answer sheet. Part II consists of 25 open-ended questions. Write…
Toormina High School - English Faculty Programs (Stage 5) Focus Area: Film Unit: Tim Burton Director Study Fairbairn 2009 Term 3 weeks 1-6 Year: 9E6 Band: C | | |Outcomes: A student:…
Case Analysis of an organisational behaviour using the Job Characteristics Model Word Count: 2500 Introduction The purpose of this case analysis is to apply the theory of Job Characteristic Model (JCM) to my own work experience and provide insightful recommendations for more effective organisational behaviour in the future. This will be achieved through the discussion of literature review, diagnosis, recommendations and reflection. Literature review Job design does not increase work…
Why give a talk? 1.1 How do you currently feel about giving a talk? 1.2 Drawing on your own experiences 3 3 3 2 Planning a talk 2.1 Where to start 2.2 Twelve preparation tasks 2.3 Using visual and auditory aids 2.4 Giving presentations as part of a team 4 5 5 7 9 3 Delivering the talk 3.1 Beginnings and endings 3.2 Inviting and responding to questions 4 Reflecting on and learning…
Fundamentals, Techniques & Theory CASH FLOWS AND FINANCIAL RATIO ANALYSIS CHAPTER TWO ANALYSIS OF THE STATEMENT OF CASH FLOW AND FINANCIAL RATIO ANALYSIS “Patience is the best remedy for every trouble.” Plantus, Titus Maccius (c. 254- 184 B.C.) “Be not afraid of going slowly; be only afraid of standing still.” Chinese Proverb Learning Objective The Statement of Cash Flow provides valuation analysts with valuable information about an entity’s operating investment and financing…
sufficient scope to achieve the necessary breadth and depth of content required at Diploma level. Before attempting the Unit D and ID assignment it is necessary for candidates to be fully conversant with key elements of the syllabus for Units A, IA, B, IB, C and IC. To facilitate this learning process it is essential that candidates hold regular discussions with their tutor(s) throughout the period of their studies, and complete the Assignment Log provided in the NEBOSH Unit D and ID Candidate…
CFI-I Oral Plan of Action Objective: Discussion of the elements related to Basic fundamentals of Fundamentals of Instruction, Technical Subject Areas, Preflight Planning. Contents: Fundamentals of Instruction Review Certificates and Documents Airworthiness Requirements Weather Information IFR Cross-Country Flight Planning National Airspace Technical Subject Areas Regulations and Publications Operations of Systems Aeromedical Aircraft Systems, Flight Instruments, and Navigational…
Monarch Butterflies. The main focus is the life cycle of a butterfly and the caterpillar. A number of skills will be taught including knowledge acquisition, cognitive development and physical motor development. The children will have a better understanding of life and the environment around them. Activities include discussion of the books themselves as well as exploration and discovery learning through materials designed to promote independent work in learning centers. II. OVERVIEW A. Nursery…
Rights Reserved. Proximity Technology Inc. The Proximity/Merriam-Webster Inc./Franklin Electronic Publishers Inc. All other trademarks are the property of their respective owners. Second Ed. C: “Copyright (C) 1987-1992 Numerical Recipes Software. Portions copyright 1984-1998 FairCom Corporation. “FairCom” and “c-tree Plus” are trademarks of FairCom Corporation and are registered in the United States and other countries. All Rights Reserved. This product includes software developed by the Apache Software…
appropriate approach to the second language is replaced by an approach that is artificial as said by Gattegno. This artificial approach is focused on the rules that a fruitful learning includes consignment of one’s self to acquiring a language by the using of silent consciousness and then a dormant tryout. Objectives There are several objectives of the silent method as for example: Its main objective is that it helps starting level learners in gaining aural and oral skills of the other language that…