Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of C and C++ Programming Simple data structures Pointers.

Similar presentations


Presentation on theme: "Fundamentals of C and C++ Programming Simple data structures Pointers."— Presentation transcript:

1 Fundamentals of C and C++ Programming Simple data structures Pointers

2 Simple Data Structures Arrays Structures Unions

3 EEL 3801 – Lotzi Bölöni Simple Data Structures  It would be limiting to have to express all data as variables.  It would be desirable to be able to group data into sets of related data.  This can be done two main ways: –arrays (all data of the same type) –structures (data may be of different types).

4 EEL 3801 – Lotzi Bölöni Type Definitions  Special data types designed by the programmer can be defined through the typedef keyword.  For example, if we want to define a data type that is to be defined only once and then used thereafter: typedef unsigned long int Myvar

5 EEL 3801 – Lotzi Bölöni Type Definitions  So, Myvar can now be used to indicate an unsigned long int wherever used. Myvar n; is the same as: unsigned long int n;  But it can be used for far more. (later)

6 EEL 3801 – Lotzi Bölöni Arrays  Left-most symbol indicates the name of the array. This is common for all its elements.  Individual data identified by distance from first one in array.  Within square brackets is the cell number (how many cells away from the first one).  Individual cells can be used as regular variables.

7 EEL 3801 – Lotzi Bölöni Arrays -45 6 0 72 1543 -89 0 62 -3 c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] For array c:

8 EEL 3801 – Lotzi Bölöni Declaring Arrays  The declaration allows the compiler to set aside sufficient contiguous memory for the size of array  The type of data to be stored must be identified so that sufficient space is allocated.  Arrays allocated statically - remain the same size throughout program execution.

9 EEL 3801 – Lotzi Bölöni Declaring Arrays int c[12]; float a[100]; char b[15];  Can be automatic or external.  Size typically done through a macro. #define SIZE 10

10 EEL 3801 – Lotzi Bölöni Initializing Arrays  Not automatically initialized. Can be initialized during declaration or within the program in a loop. int n[10] = {32,27,64,18,95,14,90,70,60};  If more elements than initialized, others = 0.  If less elements than initialized - error. int n[] = {32,27,64,18,95,14,90,70,60,37};

11 EEL 3801 – Lotzi Bölöni Passing Arrays to Functions  Arrays passed by reference - actual variable address passed. The called function can modify the original array’s values.  Pass name without brackets.  Include the size of the array as a passed value.  Function header and prototype must indicate that an array is being passed.

12 EEL 3801 – Lotzi Bölöni Passing Arrays to Functions #define SIZE 5 void function1(int [],int); void function2(int); main() { int a[] = {0, 1, 2, 3, 4}; function1(a,SIZE); function2(a[3]); }

13 EEL 3801 – Lotzi Bölöni Multi-dimension Arrays  Arrays can have an arbitrary number of dimensions.  Indicated by multiple bracket pairs. int a[5][10]; int b[10][12][20];  Can be called in same way as vector arrays.  First bracket is the row script  Second is the column script.

14 EEL 3801 – Lotzi Bölöni Initializing Multi-dim. Arrays  Initialization by row in braces.  First brace equates to first row, 2 nd to 2 nd,. int c[2][2] = {{1,2} {3,4}};  Initializes b[0][0]=1, b[0][1]=2, b[1][0]=3, and b[1][1]=4.  But what if int c[2][2] = {{1} {3,4}};  Initializes b[0][0]=1, b[0][1]=0, b[1][0]=3, and b[1][1]=4.

15 EEL 3801 – Lotzi Bölöni Arrays and Strings  Strings are in reality arrays of characters  Each element contains one character.  Each cell is one byte in size.  More about strings and string operations later.

16 EEL 3801 – Lotzi Bölöni Structures  A collection of related, but dissimilar variables under one name.  Provides great flexibility that an array does not.  Used to define records to be stored in files.  Also used to form dynamic data types such as linked lists, linked stacks and linked queues.

17 EEL 3801 – Lotzi Bölöni Structure Definitions  Declared as follows: struct planet { char *name; int nummoons; double dist_from_sun; float dist_from_earth; };  This creates a definition of the structure.  planet is the structure tag.

18 EEL 3801 – Lotzi Bölöni Structures Components  The variables are called members.  They can be accessed individually using: –The structure member operator (also called the dot operator). –The structure pointer operator (also called the arrow operator).  See Figure 10.2, page 400 of textbook.

19 EEL 3801 – Lotzi Bölöni Structure Operators  The dot operator accesses the contents of the member using the member name and the structure variable name. planet.nummoons directly accesses the contents of the member nummoons  Can be used as a regular integer variable.

20 EEL 3801 – Lotzi Bölöni Structure Operators  The arrow operator accesses the contents of the member using a pointer to the structure variable and the member name. planet_ptr->nummoons directly points to the contents of the member nummoons  equal to (*planet_ptr).nummoons

21 EEL 3801 – Lotzi Bölöni Structure Variables  The struct keyword defines a “model” of the desired structure.  It is not a real variable per se.  A real variable is created by creating an instance of the structure model.  Also referred to as “instantiating”.

22 EEL 3801 – Lotzi Bölöni Structure Variables  To make instances of the definition: –Instance name(s) can be added after the definition. –Can be defined as a data type to be instantiated separately. –The struct keyword can be used along with the tag to instantiate.  See examples next.

23 EEL 3801 – Lotzi Bölöni Structure Variables  Instances added after the definition: struct planet { char *name; int nummoons; double dist_from_sun; float dist_from_earth; } earth, mars, solar[9], *ptr;  solar[9] is an array of 9 structures of type planet. ptr is a pointer to a planet type.

24 EEL 3801 – Lotzi Bölöni Structure Variables  The tag is optional. The following code is equivalent to the one in the last slide: struct { char *name; int nummoons; double dist_from_sun; float dist_from_earth; } earth, mars, solar[9], *ptr;  Only way to instantiate is in the definition.

25 EEL 3801 – Lotzi Bölöni Structure Variables  Defined as a datatype: typedef struct planet Planet; Planet earth, mars; Planet *ptr; Planet solar[9];  This assumes that the structure definition is as before.

26 EEL 3801 – Lotzi Bölöni Structure Variables  Can also be done directly in the definition: typedef struct planet { char *name; int nummoons; double dist_from_sun; float dist_from_earth; } Planet;  The planet tag is not necessary in this case.

27 EEL 3801 – Lotzi Bölöni Structure Variables  The struct keyword can also be used to instantiate. struct planet { char *name; int nummoons; double dist_from_sun; float dist_from_earth; }; struct planet earth;

28 EEL 3801 – Lotzi Bölöni Initializing Structure Members  Like in arrays.  Use values inside braces.  Only when variable being instantiated. struct planet earth = {earth,1,1.0e+6,0}  If less values than members, then only the first few are initialized. Others = 0.  Must be constant values or expressions.

29 EEL 3801 – Lotzi Bölöni Structures and Functions  Structures can be passed to functions as: –Individual structure members. –An entire structure variable. –Pointer to a structure variable.  Passed by value if the individual structure member or the entire structure is passed.  Passed by reference if a pointer to the structure is passed.

30 EEL 3801 – Lotzi Bölöni Structures  Arrays can be assigned to a structure member.  There can be arrays of structures.  Structure members can be other structures.  Structure members can be self- referencing structures - pointers that point to similar structures as itself.

31 EEL 3801 – Lotzi Bölöni Unions  Same as structures, except members share same storage space.  Saves space when some members are never used at the same time.  Space for a member must be large enough to accommodate the largest of the data types to be stored in that member.

32 EEL 3801 – Lotzi Bölöni Unions  Unions are declared and defined in a way similar to structures.  The keyword union replaces the keyword struct.  Not highly recommended except when memory management is critical.

33 EEL 3801 – Lotzi Bölöni Enumeration Constants  Allows a set of integer constants to be represented by identifiers.  Symbolic constants whose value can be set automatically.  Values start with 0 (unless otherwise noted by programmer) and are incremented by 1.  Uses the enum keyword for definition.

34 EEL 3801 – Lotzi Bölöni Enumeration Example #include enum months {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, ACT, NOV, DEC} main() { enum months month; char *monthName[] = {“”, “January”,..}; for(month=JAN;month<=DEC;month++) printf(……….monthName[month]; }

35 Pointers

36 EEL 3801 – Lotzi Bölöni Pointer Variables  Conventional variables contain values.  Pointer variable contains memory address of variable that contains values (or pointers)  Allows call by reference.  Permits creation of dynamic data structures.  Permits dynamic allocation of memory.  Difficult to understand and use.

37 EEL 3801 – Lotzi Bölöni Pointer Variables  Conventional variable names directly reference a value.  Pointer variables indirectly reference a value  Referencing a value through a pointer variable is called indirection.  Pointer variables = pointers

38 EEL 3801 – Lotzi Bölöni Declaration of Pointer Variables  Pointers must be declared like regular variables.  It must be stated which type of variable they point to.  Declarations use * to indicate “pointerhood” int *ptr;  pointer ptr points to an integer variable.

39 EEL 3801 – Lotzi Bölöni Pointer Variables  Pointers should be initialized.  The * does not distribute.  Can be set to NULL or to 0, but NULL is preferred.  NULL is a symbolic constant defined in  Pointers assigned a value of 0 actually have the value 0 and not an address.

40 EEL 3801 – Lotzi Bölöni Address-of Pointer Operator  Address-of operator ( & ) is a unary operator returning the address of its operand.  The basic operator used to assign values to pointers. int y = 5; int *ptr; ptr = &y;  ptr points to y (contains its address).

41 EEL 3801 – Lotzi Bölöni Indirection Pointer Operator  Indirection operator ( * ), or dereferencing operator is also unary and returns the value of the variable pointed at by the pointer.  In the previous example: y = 5 *ptr = 5  Not to be confused with the declaration operator - very confusing!!!.

42 EEL 3801 – Lotzi Bölöni Pointer Example main() { int a; int *aptr; a = 7; aptr = &a; printf(“The address of a =%d“,&a); printf(“The value of aptr =%d“, aptr); printf(“The value of a = %d“, *aptr); }

43 EEL 3801 – Lotzi Bölöni Call by Reference with Pointers  By passing a variable’s address to a function, we give that function the ability to modify the value of the original value.  This is a simulation of call by reference.

44 EEL 3801 – Lotzi Bölöni Call by Value - Example void value_funct1(int); main() { int number = 5; printf(“Original value =“, number); value_funct(number); printf(“New value =“, number); } void value_funct(int n); { n = n * n; }

45 EEL 3801 – Lotzi Bölöni Call by Value - Example Original value = 5 New value = 5  The call to function value_funct did not change the original variable number in main().

46 EEL 3801 – Lotzi Bölöni Call by Reference - Example void value_funct2(int *); main() { int number = 5; printf(“Original value =“, number); value_funct(&number); printf(“New value =“, number); } void value_funct(int *nptr); { (*nptr) = (*nptr) * (*nptr); }

47 EEL 3801 – Lotzi Bölöni Call by Reference - Example Original value = 5 New value = 25  The call to function value_funct changed the original variable number in main().  A similar effect can be obtained by value_funct returning a value to main()

48 EEL 3801 – Lotzi Bölöni Functions Returning Pointers  Functions can also return pointers to variables. int* function1(int, int); is the prototype for a function that returns a pointer to an integer variable.  Is easily done by simply returning the value of a pointer variable - an address.

49 EEL 3801 – Lotzi Bölöni The const and Pointer Passing  The const qualifier tells the compiler that the variable following it is not to be changed by any program statements.  Provides a measure of security when passing addresses of variables whose values are not to be modified (for example, arrays).  When passing pointers, 4 possibilities exist:

50 EEL 3801 – Lotzi Bölöni Pointer Passing  Non-constant pointer to non-constant data –Declaration does not include const in any way. –Data can be modified through the pointer. –Pointer can be modified to point to other data.  Highest level of data access to called function.  This is what we have been doing up to now.

51 EEL 3801 – Lotzi Bölöni Pointer Passing  Non-constant pointer to constant data: –Pointer can be modified to point to any data. –Data that it points to cannot be modified –May be used to protect the contents of a passed array. –Read as “a is a pointer to an integer constant” void funct(const int *a)

52 EEL 3801 – Lotzi Bölöni Pointer Passing  Constant pointer to non-constant data: –Pointer always points to same memory location. –Data that it points to can be modified. –Default value for a passed array. –Pointer must be initialized when declared. –Read “aptr is a constant pointer to an integer ” int x; int * const aptr = &x;

53 EEL 3801 – Lotzi Bölöni Pointer Passing  Constant pointer to constant data: –Pointer always points to same memory location. –Data that it points to cannot be modified. –Read “aptr is a constant pointer to an integer constant” - right to left int x = 5; const int* const aptr = &x;

54 EEL 3801 – Lotzi Bölöni Pointer Arithmetic  Pointers are valid operands in mathematical operations, assignment expressions and comparison operations.  But not all operators are valid with pointers.  Operators that are do not always work the same way.

55 EEL 3801 – Lotzi Bölöni Pointer Arithmetic  A pointer can be incremented (++).  A pointer can be decremented (--).  An integer may be added to, or subtracted from a pointer (+, +=, -, -=).  One pointer may be subtracted from another.  But this can be misleading.

56 EEL 3801 – Lotzi Bölöni Pointer Arithmetic  When adding integers to pointers, the value of the integer added is the number of memory elements to be moved.  The actual answer depends on the type of memory element being pointed to by the pointer.  Assuming int = 4 bytes (32 bits):

57 EEL 3801 – Lotzi Bölöni Pointer Arithmetic int *yptr = 3000; yptr += 2;  In reality, yptr = 3008, because 2*4=8 bytes.  In other words, the pointer moved two integer data “spaces” away from its original address.  Since an integer data space is 4 bytes, it moved 8 bytes.

58 EEL 3801 – Lotzi Bölöni Pointer Arithmetic  Since character variables are 1 byte in size, the arithmetic will be normal for pointers that point to characters.  The ++ and -- operators work the same way.  They add one data space to the address. int *ptr = 3000; ptr++;  ptr = 3004, assuming integer takes 4 bytes.

59 EEL 3801 – Lotzi Bölöni Pointer Arithmetic  Subtraction works the same way. int x; x = v1ptr - v2ptr ; where v1ptr=3008 and v2ptr=3000 ; ==> x = 2 if int is 4 bytes.

60 EEL 3801 – Lotzi Bölöni Pointers and Arrays  The name of an array is in reality a pointer to its first element.  Thus, for array a[] with, for instance, 10 elements, a = &(a[0]).  This is why when an array is passed to a function, its address is passed and it constitutes call by reference.

61 EEL 3801 – Lotzi Bölöni Pointers and Arrays  a[3] can be also referenced as *(a+3).  The 3 is called the offset to the pointer.  Parenthesis needed because precedence of * is higher than that of +.  Would be a[0]+3 otherwise.  a+3 could be written as &a[3].  See Fig. 7.20, page 284 in textbook.

62 EEL 3801 – Lotzi Bölöni Pointers and Arrays  The array name itself can be used directly in pointer arithmetic as seen before.  Pointer arithmetic is meaningless outside of arrays.  You cannot assume that a variable of the same type will be next to a variable in memory.

63 EEL 3801 – Lotzi Bölöni Pointers and Strings  Strings are really pointers to the first element of a character array.  Array is one character longer than the number of elements between the quotes.  The last element is “\0” (the character with the ASCII code zero).

64 EEL 3801 – Lotzi Bölöni Arrays of Pointers  Arrays may contain nearly any type of variable.  This includes pointers.  Could be used to store a set of strings. char *suit[4] = {“hearts”, “diamonds”, “spades”, “clubs”};  The char * says that the elements of the array are pointers to char.

65 EEL 3801 – Lotzi Bölöni Arrays of Pointers H e a r t s \0 D i a m o n d s \0 C l u b s \0 S p a d e s \0 Suit[0] Suit[1] Suit[2] Suit[3]

66 EEL 3801 – Lotzi Bölöni Pointers to Functions  Contains address of the function in memory.  This is now addressing the code segment.  Can be –passed to functions –returned from functions –stored in arrays –assigned to other function pointers

67 EEL 3801 – Lotzi Bölöni Pointers to Functions  Pointer contains the address of the first instruction that pertains to that function.  Commonly used in menu-driven systems, where the choice made can result in calling different functions.  Two examples follow:

68 EEL 3801 – Lotzi Bölöni Example 1  Writing a sorting program that orders an array of integers either in ascending or descending order.  main() asks the user whether ascending or descending order, then calls the sorting function with the array name, its size and the appropriate function (ascending or descending).  See Fig. 7-26, page 292 in textbook.

69 EEL 3801 – Lotzi Bölöni Example 1 - continued int ascending(int,int); int descending(int,int); void sort(int *, const int, int (*)(int,int)); main() {... sort(array,10,ascending); or sort(array,10,descending);... }

70 EEL 3801 – Lotzi Bölöni Example 1 - continued void sort(int *arr, const int size, int (*compare_func) (int, int)); { if ((*compare_func)(arr[i], arr[i+1])) do something; } int ascending(const int a, const int b) { return b < a; }

71 EEL 3801 – Lotzi Bölöni Example 1 - continued  main() calls sort() and passes to it the array, its size, and the function to be used.  sort() receives the function and calls it under a pointer variable compare_func, with two arguments.  The arguments are elements of the array, arr[i] and arr[i+1].  compare_func returns 1 if true,0 if false.

72 EEL 3801 – Lotzi Bölöni Example 2  Functions are sometimes represented as an array of pointers to functions.  The functions themselves are defined as they would normally.  An array of pointers is declared that contains the function names in its elements.  Functions can be called by dereferencing a pointer to the appropriate cell.

73 EEL 3801 – Lotzi Bölöni Example 2 - continued void function1(int); void function2(int); void function3(int); main() { void (*f[3])(int) = {function1, function2, function3}; }  “f is an array of 3 pointers to functions that take an int as an argument and return void”

74 EEL 3801 – Lotzi Bölöni Example 2 - continued  Such functions can be called as follows: (*f[choice]) (choice));  Can be interpreted as calling the contents of the address located in the choice cell of array f, with an argument equal to the value of the integer variable choice.  The parenthesis enforce the desired precedence.

75 EEL 3801 – Lotzi Bölöni Double Pointers  Double pointers are commonly used when a call by reference is desired, and the variable to be modified is itself a pointer.  A double pointer is a pointer to a pointer to a variable of a particular type.  Declared as int **ptr  Read as a pointer to a pointer to an integer.

76 EEL 3801 – Lotzi Bölöni Double Pointers int ptr

77 EEL 3801 – Lotzi Bölöni Double Pointers  Deferencing a double pointer results in an address.  Derefencing it again results in the value of the ultimate variable var = *(*dbl_ptr);

78 Memory Allocation

79 EEL 3801 – Lotzi Bölöni Static Memory Allocation  Variables declared at compile time (in the source code) are called static variables.  It is necessary to know how many of these variables will be necessary prior to compilation.  They cannot be undeclared (other than by functions exiting).  Easy to use but are not very flexible.

80 EEL 3801 – Lotzi Bölöni Static Memory Allocation  Advantages: –System does not run out of memory –Easy to keep track of them –Can be referenced directly by the variable name –Limited to size of data segment in machine  Disadvantages –Must know required amount at compile time –Cannot be generated at run time

81 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  Addresses the disadvantages of static memory. –Only limited by the amount of memory in machine, not by pre-determined size of array. –Can be created and deleted at runtime. –Can result in memory leaks. –Harder to keep track of the variables

82 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  The C function call malloc() creates a block of memory of the size and shape designated by the call.  Its argument is the size of the desired block.  Returns a pointer of type void * which points to the block of memory.  Returns a NULL pointer if memory not sufficient.

83 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  Uses the sizeof() operator to determine the size of the data type to be represented by the newly allocated block of memory.  The call to malloc() should be cast so as to force the pointer returned to be of the proper type - but not necessary.

84 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  Good idea to always use the sizeof() operator, even though technically, you can simply place a number there. This increases portability.  Memory block can be returned to the free memory heap by using the free() function free(ptr);

85 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  Dynamically allocated memory can only be “found” through its pointer.  Pointer must be cast to its correct data type.  Cannot be referenced any other way.  Typically used with structures and unions.  Thus, arrow operator becomes important in dynamically allocated structures.

86 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  Arrays are static variables.  But can also be dynamically created.  Use the calloc() function call for arrays.  Requires two arguments: –the number of elements in the array –the size of each element in the array

87 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  calloc() also returns a pointer to the first element of the array.  Can be scripted just like a regular array.  Must be deleted when no longer needed.  Cannot be extended at run time.  But its element size can be changed.

88 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  realloc() allows the modification of the size of a memory block previously allocated through malloc() or calloc().  Will keep data intact if size is larger.  Otherwise, it will become corrupted.  Requires two arguments: –Name of pointer to block to be re-allocated. –New size of the memory element.

89 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  In C++, it is somewhat easier: –the new operator takes as an argument the data type name - new –returns a pointer of the correct type to a block of memory of the correct size (does not need sizeof(datatype) ). –delete de-allocates the memory block and returns it to heap.

90 EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  new also used to dynamically allocate arrays.  delete also used to de-allocate arrays, but empty brackets are needed.  delete can only be used to delete memory allocated with new.  Do not mix and match malloc(), free(), new and delete.

91 EEL 3801 – Lotzi Bölöni Example typedef struct name_tag { int blah; float blah_blah; } Typename; Typename *ptr; ptr = (Typename *) malloc(sizeof(Typename)); ptr->blah = 2;. free(ptr);

92 EEL 3801 – Lotzi Bölöni Example typedef struct name_tag { int blah; float blah_blah; } Typename; Typename *ptr; ptr = (Typename *) calloc(20,sizeof(Typename)); ptr[12]->blah = 10; free(ptr);

93 EEL 3801 – Lotzi Bölöni Example typedef struct name_tag { int blah; float blah_blah; } Typename; Typename *ptr; ptr = new Typename; ptr->blah = 23;. delete ptr;

94 EEL 3801 – Lotzi Bölöni Example typedef struct name_tag { int blah; float blah_blah; } Typename; Typename *ptr; ptr = new Typename[20]; ptr[15]->blah = 19;. delete [] ptr;


Download ppt "Fundamentals of C and C++ Programming Simple data structures Pointers."

Similar presentations


Ads by Google