Structured data types An elegant way to deal with the rgb image is to employ structured data types. A Java class is a generalization of a C struct. A structure.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

C Language.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Structures Spring 2013Programming and Data Structure1.
Structs CSE 2451 Rong Shi. Structures Structure – one or more values, called members, with possibly dissimilar types that are stored together – Group.
ISBN Chapter 7 Expressions and Assignment Statements.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Multiple-Subscripted Array
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
Image representation methods. Bitmap graphic method of a black-and-white image.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
We have seen the notation “p=&a; ” used to set the pointer “p” to the address of another variable. For arrays and strings, the technique for setting the.
1 Structured Data Types A structure can be used to combine data of different types into a single (compound) data value. Like all data types, structures.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Welcome Topic: Pixels A.M.Meshkatur Rahman Class: vii Roll: 07.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Stack and Heap Memory Stack resident variables include:
Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Expressions and Assignment Statements
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
Introduction to Raytracing Raytracing Introduction (from Wikipedia) In computer graphics, ray tracing is a technique for generating an image by tracing.
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CS 261 – Data Structures Introduction to C Programming.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
June 14, ‘99 COLORS IN MATLAB.
Structs, Enums, Unions Rudra Dutta CSC Spring 2007, Section 001.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Chapter Seven: Expressions and Assignment Statements Lesson 07.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Java Programming Language Lecture27- An Introduction.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Expressions and Assignment Statements
Representation of image data
Expressions and Assignment Statements
The Machine Model Memory
Expressions and Assignment Statements
Expressions and Assignment Statements
Structured Data Types A structure can be used to combine data of different types into a single (compound) data value. Like all data types, structures must.
Expressions and Assignment Statements
Expressions and Assignment Statements
Looping through pixels.
Hank Childs, University of Oregon
Expressions and Assignment Statements
Presentation transcript:

Structured data types An elegant way to deal with the rgb image is to employ structured data types. A Java class is a generalization of a C struct. A structure is an aggregation of basic and structured data elements, possibly including pointers, but unlike a class, it contains no embedded methods (functions). Structures and “typedef

A C structure is declared as follows: struct pix_type { unsigned char r; unsigned char g; unsigned char b; }; As with Java, it is important to be aware that struct pix_type is the name of a user defined structure type. It is not the name of a variable. Structures and “typedef

To declare an instance (variable) of type struct pix_type use: struct pix_type pixel; struct pix_type is the name of the type pixel is the name of a variable of type struct pix_type To set or reference components of the pixel use: pixel.r = 250; // make Mr. Pixel yellow pixel.g = 250; pixel.b = 0; Structures and “typedef

To declare a pointer to a struct pix_type use: struct pix_type *pixptr; Before using the pointer we must always make it point to something: pixptr = (struct pix_type *)malloc(sizeof(struct pix_type)); Pointers to structures

To set or reference components of the pix_type to which it points use: (*pixptr).r = 250; // make Mr. *pixptr magenta (*pixptr).g = 0; (*pixptr).b = 250; Warning: the C compiler is very picky about the location of the parentheses here. Perhaps because of the painful nature of the above syntax, an alteranative “short hand” notation has evolved for accessing elements of structures through a pointer: pixptr->r = 0; // make Mr. pixptr-> cyan pixptr->g = 250; pixptr->b = 250; This shorthand form is almost universally used. Pointers to structures:

To enhance the readability of a program, C provides the “typedef” statement as a way to define new data types. An example of the typedef statement is: typedef int boolean; This defines the new data type “boolean” as being equivalent to the existing type “int”. After defining this equivalence, the programmer can declare variables that will be used to represent a TRUE or FALSE value as follows: boolean flag; boolean doneTest; Declaring a variable as boolean as opposed to int can help the reader understand the role of the variable in the program. typedef statement:

Another common use of typedef is to define a data type that is equivalent to a structure. For example, consider the “pixel” structure that was defined earlier. We could define a new data type called “pixel_t” as follows: typedef struct pix_type { unsigned char r; unsigned char g; unsigned char b; } pixel_t; typedef statement:

In this case we are equating the name “pixel_t” to the structure definition. Now in a program instead of having to say: struct pix_type newpixel; to declare the structure variable “newpixel”, we can instead simply say: pixel_t newpixel; Functionally there is NO difference – the advantage comes through readability and a little less typing. typedef statement:

Other examples of declaring pixel variables are: pixel_t *pixelPtr; /* Declare a pointer to a pixel */ pixel_t image[100][100]; /* Declare a pixel matrix */ typedef statement:

Space for a color image in binary rgb format can be allocated by: pixel_t *pixptr; : pixptr = (pixel_t *)malloc(sizeof(pixel_t) *numrows * numcols); To read the image data pixcount = fread(pixptr, sizeof(pixel_t), numrows * numcols, stdin); if (pixcount != numrows * numcols) { fprintf(stderr, “pix count err - wanted %d got %d \n”, numrows * numcols, pixcount); exit(1); } The Color Image:

To access a specific pixel red = *(pixptr + row * numcols + col).r; green = *(pixptr + row * numcols + col).b; blue = *(pixptr + row * numcols + col).g; or red = (pixptr + row * numcols + col)->r; green = (pixptr + row * numcols + col)->g; blue = (pixptr + row * numcols + col)->b; or even but not in class assignments red = pixptr[row * numcols + col].r; Pointers to structures:

typedef struct img_type { int numrows; int numcols; unsigned char pixval[1024][768]; } image_t; image_t image; Elements of the array are accessed in the usual way: image.pixval[5][10] = 125; Structures and Arrays:

It is also possible to create an array of structures typedef struct pixel_type { unsigned char r; unsigned char g; unsigned char b; } pixel_t; pixel_t pixmap[100]; To access an individual element of the array place the subscript next to the name it indexes pixmap[15].r = 250; Structures and Arrays:

It is common for structures to contain elements which are themselves structures or arrays of structures. In these cases the structure definitions should appear in “inside-out” order. This is done to comply with the usual rule of not referencing a name before it is defined. typedef struct pixel_type { unsigned char r; unsigned char g; unsigned char b; } pixel_t; typedef struct img_type { int numrows; int numcols; pixel_t pixdata[1024][768]; } img_t; Structures containing structures:

img_t image; image.pixdata[4][14].r = 222; Structures containing structures: