mca ii dfs u-2 array records and pointer

Download Mca ii dfs u-2 array records and pointer

Post on 21-Jul-2015




0 download

Embed Size (px)



Course: MCASubject: Data and File StructureUnit-2String, Record and PointerDefinition of a RecordA data type created/customisable by the programmer consisting of a set of fields/multiple data items which can be of different data types

RecordsArrays can only hold data items with the same typeRecords can contain different types of dataThis makes them more complex to manipulateAssociated with databasesRecord Example[1]FieldTypeSurnameStringFirstNameStringGenderCharacterAddressStringPostCodeStringCustomer NumberStringA record can be considered as a two dimensional array with different data types.Record Example[2]SurnameFirstNameSexAddress 1PostCodephoneTodAndyM35 Brookside DrTY7 8UK225 3625BoydMaryF27 The GrangeOB7 RF1335 2901BellCharlesM2 Larch RdHT5 WA3213 1157Records[3]In the computers memory, records are stored asRecord 1*Record 2*Record 3*Record 4*Record 5*EOFEach record is terminated by a CR, LF (indicated by *) and EOF control codes.Assigning valuesBookInfo.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 valuesListBox1.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. RecordsArrays (1D & 2D) all data must be of same type (Integer, string,..)Records fields of different typesArrays simple to implementRecords more complex to implement


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.10Arrays and Lists[4]You can see the difference between arrays and lists when you delete items.

11What is a pointerIn a generic sense, a pointer is anything that tells us where something can be found.Addresses in the phone bookURLs for webpagesRoad signs

Java ReferenceIn 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 ); 23 );ford.turn (LEFT);

What is a pointer ?In C, a pointer variable (or just pointer) is similar to a reference in Java except thatA pointer can contain the memory address of any variable type (Java references only refer to objects)A primitive (int, char, float)An arrayA struct or unionDynamically allocated memoryAnother pointerA functionTheres a lot of syntax required to create and use pointers

14Why Pointers?They allow you to refer to large data structures in a compact wayThey facilitate sharing between different parts of programsThey make it possible to get new memory dynamically as your program is runningThey make it easy to represent relationships among data items. 15Pointer CautionThey are a powerful low-level device.Undisciplined use can be confusing and thus the source of subtle, hard-to-find bugs. Program crashesMemory leaksUnpredictable results16C Pointer VariablesTo declare a pointer variable, we must do two thingsUse 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 pointertype *nameOfPointer;17Pointer DeclarationThe declarationint *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 asintPtr 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 definitionint *intPtr, intPtr2;

intPtr is a pointer to an int, but intPtr2 is not!18Pointer OperatorsThe 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( )

19Pointer and Variable typesThe 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 */

20More Pointer CodeUse ampersand ( & ) to obtain the address of the pointeeUse star ( * ) to get / change the value of the pointeeUse %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) ;

21ReferencesAn introduction to Datastructure with application by jean Trembley and sorrensonData structures by schaums and series seymour lipschutz

LIST OF IMAGES1. An introduction to Datastructure with application by jean Trembley and sorrenson4.


View more >