c programming lecture 14 instructor: wen, chih-yu department of electrical engineering national...

31
C Programming Lecture 14 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Upload: louisa-allen

Post on 16-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

C Programming

Lecture 14

Instructor: Wen, Chih-YuDepartment of Electrical Engineering

National Chung Hsing University

Pointer Notation vs. Array Notation

Topics Use pointer notation to access array elements and strings C allows array data to be accessed using pointer notation as

well as array notation.

Pointer Notation vs. Array Notation

How to use pointer notation to access the elements of a 1-D character array? Using either array or pointer notation Access the element indicated by aa[16]

putchar(aa[16]); putchar(*(aa+16));

Pointer Notation vs. Array Notation What logic underlies this notation?

Involves pointer arithmetic in the form of adding an integer to an address.

C performs this type of operation by first noting the type of address. aa + 16

aa is the address of the beginning of a 1-D array of characters. Since characters occupy 1 byte of memory, C adds 16 bytes to the

address indicated by aa. Gives us the address of the character indicated by aa[16], the 17th

character. Using the unary * operator, the expression *(aa+16) gives us the

value of the 17th character.

Pointer Notation vs. Array Notation

How to use pointer notation to access the elements of a 2-D array? Using either array or pointer notation Access the element indicated by bb[3][5]

putchar(bb[3][5]); putchar(*(*(bb+3)+5));

C regards bb[ ][ ] as an array of arrays Five 1-D arrays of size 40 The address expressed as bb is regarded an address of the

beginning of one of the rows. An individual element of that address is considered to have a size

40, not 1! When we add bb and 3, we add 3*40 = 120 bytes to the address

indicated by bb.

Pointer Notation vs. Array Notation

*(bb+3) represents the address (not the value, as occurs when working with 1-D arrays or single pointer variables) of the beginning of the forth row. char bb[5][40]={"We can", "use both", "array and pointer",

"notation to access", "one and two-dimensional arrays"}; putchar(*(*(bb+3)+5)); causes the character i to be printed.

With a 2-D array, need two unary * operators With a 3-D array, need three unary * operators

Pointer Notation vs. Array Notation

What does *(bb+2) represent? Equivalent to bb[2] Represents the address of the beginning of the third

row in the bb[ ][ ] array. puts(*(bb+2)) causes the string “array and pointer” to

be printed.

Pointer Notation vs. Array Notation

With the declaration char *dd, what does dd+125 indicates? The variable dd indicates an address. The 125 causes 125 bytes to be added to the address

indicated by dd. dd = &bb[0][0]; putchar(*(dd+125)); Print the character i, which is the 126th character in the

bb[ ][ ] array.

Pointer Notation vs. Array Notation

Can we use dd = bb, instead of dd = &bb[0][0]? No, because C considers it to be a type mismatch. dd is a pointer variable.

Cannot take on the characteristics of an array address bb is the address of a 2-D array.

Would putchar(*(bb+125)); print the 126th character of the bb[ ][ ] array? No; first, the statement would not compile since two unary *

operators are needed to point to a single character. Pointer arithmetic would not advance 125 bytes but 125x40 = 5000

bytes.

Pointer Notation vs. Array Notation

How to use pointer notation to access the element of a 3-D array? For instance,

putchar(cc[1][2][3]); putchar(*(*(*(cc+1)+2)+3));

C regards 3-D arrays as arrays of arrays, cc[2][3][20] is considered two 2-D arrays of size [3][20] The address expressed as cc is regarded an address of the

beginning of one of the 2-D arrays

Pointer Notation vs. Array Notation

*(cc+1) represents the address of the beginning of the second 2-D array.

*(cc+1)+2 The address is to the beginning of the next 2-D array using one

unary * operator The integer 2 causes the addition of 2*20 = 40 bytes.

*(*(cc+1)+2) represents the address of the beginning of the third row of the second 2-D array

*(*(*(cc+1)+2)+3) putchar(*(*(*(cc+1)+2)+3)); Print out a single character.

Pointer Notation vs. Array Notation

How to use putchar with a single unary * operator to print a character from the cc[ ][ ][ ] array? Assign the address of the first character of the cc[ ][ ][ ]

array to the single pointer variable, dd. Use a single unary * operator to access individual

values of the cc[ ][ ][ ] array. dd = &cc[0][0][0]; putchar(*(dd+80));

Pointer Notation vs. Array Notation

What do cc, *cc, **cc represent? All three represent the address of the first element of

the cc[ ][ ][ ] array. cc+1 = address of first element + 60 bytes *cc+1 = address of first element + 20 bytes **cc+1 = address of first element + 1 byte

Pointer Notation

Pointer Arithmetic

Have the following declarations for 1-D arrays. int aa[50]; float bb[100]; double cc[77];

Declaration Bytes per element

Operation Action

int aa[50] 2 aa+20 Adds 2*20 = 40 bytes

float bb[100] 4 bb+12 Adds 4*12 = 48 bytes

double cc[77] 8 cc+9 Adds 8*9 = 72 bytes

Direct Correspondence

Pointer notation Array notation

*(aa+20) aa[20]

*(bb+12) bb[12]

*(cc+9) cc[9]

/* Program for Lesson 7_9*/ #include <stdio.h> void main(void) { char aa[35]={"This is a one-dimensional array"}; char bb[5][40]={"We can","use both","array and pointer", "notation to access","one and two-dimensional arrays"}; char cc[2][3][20]={"A three ","dimensional ","array ","is ","shown ","also"}; char *dd;

printf("**************** Section 1 1-D array ****************\n"); putchar(aa[0]); putchar(*aa); putchar('\n');

putchar(aa[16]); putchar(*(aa+16)); putchar ('\n');

printf("**************** Section 2 2-D array ****************\n");

putchar(bb[3 ][5] ); putchar(*(*(bb+3)+5)); putchar('\n');

puts(*(bb+2));

dd=&bb[0][0 ]; putchar(*(dd+125)); putchar('\n');

printf("**************** Section 3 3-D array ****************\n");

putchar(cc[1][2 ][3]); putchar(*(*(*(cc+1)+2)+3)); putchar('\n');

puts(*(*(cc+1)+2));

dd=&cc[0][0][0]; putchar(*(dd+80)); putchar('\n'); puts(dd+80);

printf("Address of cc[0][0][0]=%p, cc+1=%p, *cc+1=%p, **cc+1=%p\n", &cc[0][0][0],cc+1,*cc+1,**cc+1); }

Dynamic Memory Allocation

Topics Reserving memory during execution Using calloc, malloc, and realloc Large and small amounts of memory

Reserve memory based on the size of problem Illustrate the functions (calloc, malloc, realloc, and free) Utilize dynamic memory features

A 1-D character array, aa[19], and a 1-D pointer array, bb[22] A fixed-size 2-D array, cc[22][19]

Specify how much memory is to be reserved. calloc() and malloc()

The Function calloc( )

What does the function calloc do? The function calloc() reserves memory during program

execution. The amount of memory is determined by the

arguments passed to calloc().

calloc (number_of_elements, bytes_per_element) The product of the integers number_of_elements and

bytes_per_element. calloc (xx, sizeof(char)); causes memory for xx elements of

sizeof(char) (which is 1 byte) to be reserved.

The Functions calloc( ) and malloc( )

The function calloc() returns the address of the beginning of the first element reserved. bb[0] = (char *) calloc (xx, sizeof(char)); Causes the address of the beginning of the memory reserved to be

stored in the first element of the pointer array, bb[ ]. The cast operator (int *) or (double *) can be used with calloc().

What does the function malloc( ) do? The amount of memory reserved is determined by the

single argument passed to malloc( ).

malloc (number_of_bytes);

The Function malloc( )

The amount of memory reserved is equal to number_of_bytes. yy = xx*sizeof(char); malloc (yy); bb[1] = (char *) malloc(yy); Causes the address of the beginning of the memory reserved to be

stored in the second element of the pointer array, bb[ ]. The cast operator (int *) or (double *) can be used with

malloc().

The Function realloc( )

The function realloc( ) modifies the amount of memory previously reserved by a call to either malloc or calloc.

realloc (pointer, number_of_bytes); The amount of memory reserved is equal to number_of_bytes. The location of the memory reserved is indicated by pointer. The argument pointer must have been returned by a

previous call to either calloc or malloc. realloc(bb[1], yy);

Reserve different block of memory bb[1] = (char *) realloc(bb[1], yy);

The function free( )

What does the function free( ) do? Cancels the reservation for memory previously made

by calloc, malloc, or realloc.

free (pointer); Cannot reserve the memory as requested?

Return a null pointer. Check the returned null pointer to make sure that

memory has been successfully allocated.

Conceptual Image of Regions of Memory Where in memory is the space reserved?

A region called the stack. A region called the heap. A region called the variables. A region called the instructions.

A fixed-size 2-D Array Storage

Array Storage Using Dynamic Method

Using strlen and calloc to create dynamic storage

strcpy (aa, cc[0]); xx = strlen(aa); bb[0] = (char *) calloc (xx, sizeof(char)); strcpy (bb[0], aa);

Thus, we have been able to use C’s dynamic memory management capabilities.

/* Program for Lesson 7_10*/ #include <stdio.h> #include <stdlib.h> #include <string.h> void main(void) { char aa[19]; char *bb[22]; char cc[22][19]={"Example", " String 1 ", "Words"}; int xx, yy;

printf("*********** Section 1 - Using calloc *************\n");

strcpy(aa,cc[0]); xx=strlen(aa); bb[0]=(char *)calloc(xx,sizeof(char));

strcpy(bb[0],aa); puts(bb[0]);

printf("*********** Section 2 - Using malloc *************\n");

strcpy(aa,cc[1]) ; xx=strlen(aa); yy=xx*sizeof(char); bb[1]=(char *)malloc(yy) ;

strcpy(bb[1],aa); puts(bb[1]) ;

printf("*********** Section 3 - Using realloc *************\n"); strcpy(aa,cc[2]); xx=strlen(aa); yy=xx*sizeof(char);

bb[1]=(char *)realloc(bb[1],yy); strcpy(bb[1],aa); puts(bb[1]) ; free(bb[1]); }

Program Development Methodology

Assemble relevant equations and background information.

Work a specific example. Decide on the major data structure to be used. Develop an algorithm, structure charts, and data

flow diagrams. Write source code.