4.DynMemAlloc-02 - Enumeration Types Eventually we will...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Enumeration Types Eventually we will talk about creating our "own" types, but initially, we'll talk about how to create new atomic type that is a subset of a given atomic type in C. (Some atomic types in C are int, float, double, char, etc.) Typically, enumerated types are used to enhance a program's readability. Consider the following declaration example: typedef enum { North, East, South, West } directionT; Once you do this, you can declare a variable of the type directionT: directionT housedir; Internally, any enumerated type declared this way has corresponding integer values stored, starting at 0. (Thus in our example, North is 0, East is 1, South is 2, and West is 3.) However, we can specify the internal integer values of enumerated types as follows: typedef enum { penny = 1, nickel = 5, dime = 10, quarter = 25, halfdollar = 50 } coinT; Why use enumerated types? 1) Each code/value does not need to be specified explicitly. 2) Code readability, which is also useful in debugging.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Data and Memory bit - a single zero or one. The word comes from the contraction of "binary digit." byte - eight consecutive bits. Can store a char. word - the size of this depends on the computer, but they are almost always either 2 or 4 bytes. Usually stores an int. You can imagine memory as a long array of numbered cells. For example, if we had 4 megabytes, then we would have memory locations (each storing a byte) numbered from 0 to 4x2 20 -1. If we execute the line ch = 'A'; and the variable ch happened to be stored in memory location 1000, our picture would look like this: An integer would be stored over 4 consecutive bytes, and the address of the variable storing the integer would be the first memory address the variable was stored in. In certain situations, it's useful to know the size of a particular variable or type/struct. The following expression returns the desired information: sizeof(int) or sizeof x
Background image of page 2
Pointers The designers of the C programming language WANTED the programmer to have the ability to directly manipulate memory. Pointers allow this. In other languages (such as Java), pointers are hidden from the user. In C, this feature is a mixed blessing. The programmer has more control, but also has to take responsibility for that control. Here are some of the reasons to use pointers in C: 1) Allow you to refer to a large data structure in a compact way. 2) Allow you to share data efficiently among functions. 3) Allow you to dynamically reserve memory. 4)Allow you to record relationships between data. l-value: An internal memory location capable of storing data. Here are some rules that lvalues adhere to: 1) Every lvalue is stored somewhere in memory and has an address. 2) Once it's been declared the address of an lvalue never
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/22/2010 for the course EECS COP 3502 taught by Professor Guha during the Fall '09 term at University of Central Florida.

Page1 / 14

4.DynMemAlloc-02 - Enumeration Types Eventually we will...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online