mca ii dfs u-2 array records and pointer
TRANSCRIPT
Course: MCA
Subject: Data and File Structure
Unit-2
String, Record and Pointer
Definition of a Record
A data type created/customisable by the
programmer
consisting of a set of fields/multiple data items
which can be of different data types
Records
Arrays can only hold data items with the same type
Records can contain different types of data
This makes them more complex to manipulate
Associated with databases
Record Example[1]
Field Type
Surname String
FirstName String
Gender Character
Address String
PostCode String
Customer Number String
A record can be considered as a two dimensional array with different data types.
Record Example[2]
Surname FirstName Sex Address 1 PostCode ‘phone
Tod Andy M 35 Brookside
Dr
TY7 8UK 225 3625
Boyd Mary F 27 The Grange OB7 RF1 335 2901
Bell Charles M 2 Larch Rd HT5 WA3 213 1157
Records[3]
In the computer’s memory, records are stored as
Record 1 * Record 2 * Record 3 * Record 4 * Record 5 * EOF
Each record is terminated by a CR, LF (indicated by *)
and EOF control codes.
Assigning values
BookInfo.Title = InputBox("Enter the title of the book")
BookInfo.Author = InputBox("Enter the author's name")
BookInfo.ISBN = InputBox("Enter the ISBN")
BookInfo.Price = InputBox("Enter the price")
Displaying record values
ListBox1.Items.Add("Title-" & BookInfo.Title)
ListBox1.Items.Add("Author-" & BookInfo.Author)
ListBox1.Items.Add("ISBN-" & BookInfo.ISBN)
ListBox1.Items.Add("Price- £" & BookInfo.Price)
Comparison of Arrays v. Records
Arrays (1D & 2D)– all data must be of same type (Integer,
string,..)
Records – fields of different types
Arrays – simple to implement
Records – more complex to implement
Arrays
An array is an indexed set of variables, such as
dancer[1], dancer[2], dancer[3],… It is like a set of boxes
that hold things.
A list is a set of items.
An array is a set of variables that each store an item.
Arrays and Lists[4]
You can see the difference between arrays and lists
when you delete items.
What is a pointer
• In a generic sense, a “pointer” is anything that tells us
where something can be found.
– Addresses in the phone book
– URLs for webpages
– Road signs
Java Reference
• In Java, the name of an object is a reference to that
object. Here ford is a reference to a Truck object. It
contains the memory address at which the Truck
object is stored.
Truck ford = new Truck( );
• The syntax for using the reference is pretty simple.
Just use the “dot” notation.
ford.start( );
ford.drive( 23 );
ford.turn (LEFT);
What is a pointer ?
• In C, a pointer variable (or just “pointer”) is similar to a
reference in Java except that
– A pointer can contain the memory address of any
variable type (Java references only refer to objects)
– A primitive (int, char, float)
– An array
– A struct or union
– Dynamically allocated memory
– Another pointer
– A function
– There’s a lot of syntax required to create and use
pointers
Why Pointers?
• They allow you to refer to large data structures in
a compact way
• They facilitate sharing between different parts of
programs
• They make it possible to get new memory
dynamically as your program is running
• They make it easy to represent relationships
among data items.
Pointer Caution
• They are a powerful low-level device.
• Undisciplined use can be confusing and thus the
source of subtle, hard-to-find bugs.
– Program crashes
– Memory leaks
– Unpredictable results
C Pointer Variables
To declare a pointer variable, we must do two things
Use the “*” (star) character to indicate that the
variable being defined is a pointer type.
Indicate the type of variable to which the pointer
will point (the pointee). This is necessary
because C provides operations on pointers (e.g.,
*, ++, etc) whose meaning depends on the type of
the pointee.
General declaration of a pointer
type *nameOfPointer;
Pointer Declaration
The declaration
int *intPtr;
defines the variable intPtr to be a pointer to a variable of
type int. intPtr will contain the memory address of some
int variable or int array. Read this declaration as
– “intPtr is a pointer to an int”, or equivalently
– “*intPtr is an int”
Caution -- Be careful when defining multiple variables on the
same line. In this definition
int *intPtr, intPtr2;
intPtr is a pointer to an int, but intPtr2 is not!
Pointer Operators
The two primary operators used with pointers are
* (star) and & (ampersand)
The * operator is used to define pointer variables
and to deference a pointer. “Dereferencing” a
pointer means to use the value of the pointee.
The & operator gives the address of a variable.
Recall the use of & in scanf( )
Pointer and Variable types
• The type of a pointer and its pointee must match
int a = 42;
int *ip;
double d = 6.34;
double *dp;
ip = &a; /* ok -- types match */
dp = &d; /* ok */
ip = &d; /* compiler error -- type mismatch */
dp = &a; /* compiler error */
More Pointer Code
• Use ampersand ( & ) to obtain the address of the pointee
• Use star ( * ) to get / change the value of the pointee
• Use %p to print the value of a pointer with printf( )
• What is the output from this code?
int a = 1, *ptr1;
/* show value and address of a ** and value of the pointer */
ptr1 = &a ;
printf("a = %d, &a = %p, ptr1 = %p, *ptr1 = %d\n",
a, &a, ptr1, *ptr1) ;
/* change the value of a by dereferencing ptr1
** then print again */
*ptr1 = 35 ;
printf(“a = %d, &a = %p, ptr1 = %p, *ptr1 = %d\n", a, &a, ptr1, *ptr1) ;
References
An introduction to Datastructure with application by jean Trembley and sorrenson
Data structures by schaums and series –seymour lipschutz
http://en.wikipedia.org/wiki/Book:Data_structures
http://www.amazon.com/Data-Structures-Algorithms
http://www.amazon.in/Data-Structures-Algorithms-Made-Easy/dp/0615459811/
http://www.amazon.in/Data-Structures-SIE-Seymour-Lipschutz/dp
LIST OF IMAGES
1. searchoracle.techtarget.com/definition/record
2. en.wikipedia.org/wiki/Array_list
3. An introduction to Datastructure with application by jean Trembley and sorrenson
4. en.wikipedia.org/wiki/Array_list