Class Notes (836,128)
Canada (509,645)
CSC209H1 (41)
Karen Reid (20)
Lecture

feb01c.docx

1 Page
131 Views
Unlock Document

Department
Computer Science
Course
CSC209H1
Professor
Karen Reid
Semester
Winter

Description
 Structs  The compiler reserves space for the number of characters in the o A collection of related data items, primitive object b/c no local var string plus one to store the null character char *name = "csc209h"; o Ex. printf("This is a string literal\n"); struct record { name[1] = 'c'; /* Error */ char name[MAXNAME]; int count; o String variables }; /* semicolon terminates the declaration. */  Arrays are used to store strings struct record rec1; /* static allocate rec1 */  Strings are terminated by the null character \0 strncpy(rec1.name, ".exe", MAXNAME); struct record *rec2; /* pointer to rec2 */  That’s how we know a string’s length rec2 = malloc(sizeof(struct record)); /* 4 bytes */  Initializing strings, can modify: rec2->name = malloc(MAXNAME+1); /* as an array of chars*/ strncpy(rec2->name, ".gif", MAXNAME) char course[8] = "csc209h"; /* statically /* also valid: (*rec2).name = “etc”; */ initialize w/ a string o Notice to modifty a struct field a value literal */ char course[8] = {„c‟, „s‟, „c‟, „2‟, „0‟,  Directly to a struct, all fields already initialized strncpy(rec1.name, ".exe", MAXNAME) „9‟, „h‟, „\0‟}; /* equvailent to above rec1.count = 10; */ /* as a pointer to a string literal */  Through a pointer-to-struct char *s = “csc209h”; strncpy(rec2->name, ".gif", MAXNAME) o Difference btwn a string’s length and size rec2->name[4] = „\0‟; /* needed*/ rec2->int = 10; /* no need to malloc int */  Length is the number of non-null characters currently present in the string o Structs as arguments  Size is the amount of memory allocated for storing the string  Ex. printing struct, both implementations correct 1. #include  A char array of size 10 can only fit a string of length 9 2. #include  Exchar s[10] = “abc”;  Length of s = 3 3.  sizeof(s) = 10 4. struct test { 5. char name[10];  Ensure length + 1 ≤ size b/c need /0 6. int count; o String functions 7. }; 8.  The library provides a bunch of string functions which you should 9. void print_record(struct test r) { use  see $ man string 10. printf("Name = %s\n", r.name); 11. printf("Count=%d\n", r.count); int strlen(char *str) 12. }  Returns the length of the string. Remember that the storage 13. 14. int main(int argc, char* argv[]) { needed for a string is 1 + its length 15. struct test t1; 1.strlen (char *s) {
More Less

Related notes for CSC209H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit