DOC PREVIEW
UNCC ECGR 4101 - C Programming Language Review and Dissection II

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C Programming Language Review and Dissection III5-1Embedded SystemsIIILecture 5TodayPointersStringsFormatted Text OutputReading Assignment:– Patt & Patel “Pointers and Arrays”•Chapter 16 in 2ndeditionEmbedded Systems 5-2•Chapter 16 in 2ndedition• Chapter 17 in 1stedition and online notesPointersA pointer variable holds the address of the data, rather than the data itselfTo make a pointer point to variable a, we can specify the address of a– address operator &The data is accessed by dereferencing (following) the pointer– indirection operator * works for reads and writesAssigning a new value to a pointer variable changes void main ( ) {int i, j;int *p1, *p2;i = 4;j = 3;p1 = &i;p2 = &j;*p1 = *p1+*p2;p2 = p1;123456Embedded Systems 5-3Assigning a new value to a pointer variable changes where the variable points, not the datap2 = p1;}6ijp1p21&243Adx600602604606ijp1p2343600ijp1p2443600602ijp1p2573600602ijp1p2673600600More about PointersIncrementing and decrementing pointers to array elements– Increment operator ++ makes pointer advance to next element (next larger address)– Decrement operator -- makes pointer move to previous element (next smaller address)– These use the size of the variable’s base type (e.g. int, char, float) to determine what to add• p1++ corresponds to p1 = p1 + sizeof(int);• sizeof is C macro which returns size of type in bytesint a[18];int * p;p = &a[5];*p = 5; /* a[5]=5 */p++;*p = 7; /* a[6]=7 */p--;*p = 3; /* a[5]=3 */Embedded Systems 5-4in bytesPre and post– Putting the ++/-- before the pointer causes inc/dec before pointer is used• int *p=100, *p2;– p2 = ++p; assigns 102 to integer pointer p2, and p is 102 afterwards– Putting the ++/-- after the pointer causes inc/dec after pointer is used• char *q=200, *q2;– q2 = q--; assigns 200 to character pointer q2, and q is 199 afterwardsWhat else are pointers used for?Data structures which reference each other– lists– trees– etc.Exchanging information between procedures – Passing arguments (e.g. a structure) quickly – just pass a pointer –Returning a structureEmbedded Systems 5-5–Returning a structureAccessing elements within arrays (e.g. string)Pointers and the M16C ISAAddress space of M16C is 1 megabyte– Need 20 bits to address thisThis space is divided into two areas– Near: 64 kilobytes from 00000h to 0FFFFh can be addressed with a 16-bit pointer (top 4 bits of 20-bit address are 0)• Pointer is shorter (2 bytes)• Pointer operations are fasterEmbedded Systems 5-6• Note: internal RAM and SFRs are in this space– Far: Entire 1 megabyte area from 00000h to FFFFFh can be addressed with a 20-bit pointer• Pointer is longer (4 bytes used (1.5 bytes wasted!))• Pointer operations are slower, since ALU operates on 16 bits at a timeDetails in section 2.3 of M16C C Programming ManualSpecifying AreasBy default, RAM data is near, ROM data is farEmbedded Systems 5-7Pointers and the M16C ISA (II)Default locations– Near area: RAM data• data, bss– Far area: ROM data• rom, program• const dataEmbedded Systems 5-8Pointer sizes chosen by compiler based on area holding type of data– Near pointer (16 bits) used for near data– Far pointer (32 bits) used for far dataNC30 doesn’t recognize near/far keywords?– int * near near_data; does not compile– int * far far_data; does not compile– int near near_data; does not compile– int far far_data; does not compileStringsSee Section 16.3.4 of Patt & Patel.There is no “string” type in C. Instead an array of characters is used - char a[44]The string is terminated by a NULL character (value of 0, represented in C by \0).– Need an extra array element to store this nullExampleEmbedded Systems 5-9Example– char str[10] = “testing”;t e s t i n g \0str[0]str[1]strstr[2]Formatted String CreationCommon family of functions defined in stdio.h– printf: print to standard output– sprintf: print to a string– fprintf: print to a fileSyntax: sprintf(char *str, char * frmt, arg1, arg2, arg3 .. );– str: destination –fmt: format specifying what to print and how to interpret argumentsEmbedded Systems 5-10–fmt: format specifying what to print and how to interpret arguments• %d: signed decimal integer• %f: floating point• %x: unsigned hexadecimal integer• %c: one character• %s: null-terminated string– arg1, etc: arguments to be converted according to format stringsprintf Examples – strings and integerschar s1[30], s2[30];int a=5, b=10, c=-30;char ch=‘$’;sprintf(s1, “Testing”);sprintf(s2, “a=%d, b=%d”, a, b);Testings1a=5, b=10s2b=a, c=-30s1Embedded Systems 5-11sprintf(s1, “b=%x, c=%d”, b, c);sprintf(s1, “b=0x%x”, b);sprintf(s2, “s1=%s”, s1);sprintf(s1, “%c %c”, ch, s2);b=a, c=-30b=0xas1s1=b=0xas2$ ss1sprintf Examples – floating-pointVariation on %f format specifier– %-w.pf• - = left-justify. Optional• w = minimum field width (# of symbols)• p = precision (digits after decimal point)ExamplesEmbedded Systems 5-123.140000s1float f1=3.14, f2=9.991, f3=-19110.331;char s1[30], s2[30];sprintf(s1, “%f”, f1);sprintf(s1, “%f”, f3);sprintf(s1, “%4.1f”, f2);-19110.3s110.0s1sprintf Examples – More IntegersVariation on %d format specifier for integers (d/i/o/x/u)– %-w.pd• - = left justify. Optional• w = minimum field width (# of symbols)• p = precision (digits). Zero pad as neededExampless1int a=442, b=1, c=-11;Embedded Systems 5-13442s1int a=442, b=1, c=-11;char s1[30], s2[30];sprintf(s1, “%5d”, a);sprintf(s1, “%-4d”, b);sprintf(s1, “%4d”, b);sprintf(s1, “%-5.4d”, c);1 s11s1-011 s1String Operations in string.hCopy ct to s including terminating null character. Returns a pointer to s. – char* strcpy(char* s, const char* ct); s1 = “cheese”;s2 = “limburger”;strcpy(s1, s2); /* s1 = limburger */Concatenate the characters of ct to s. Terminate s with the null character and return a pointer to it.Embedded Systems 5-14return a pointer to it.– char* strcat(char* s, const char* ct);s1 = “cheese”;s2 = “ puffs”;strcat(s1, s2); /* s1 = cheese puffs */More String OperationsConcatenate at most n characters of ct to s. Terminate s with the null character and return a pointer to it.– char* strncat(char* s, const char* ct, int n);s1 = “cheese”;s2 = “ puffs”;strncat(s1, s2, 4); /* cheese puf */Compares two strings. The comparison stops on reaching a nullEmbedded Systems 5-15terminator. Returns a 0 if the two strings


View Full Document

UNCC ECGR 4101 - C Programming Language Review and Dissection II

Documents in this Course
Load more
Download C Programming Language Review and Dissection II
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Programming Language Review and Dissection II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Programming Language Review and Dissection II 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?