CIS 2500 Lecture Notes - Lecture 6: C String Handling, C Dynamic Memory Allocation, Subroutine

62 views3 pages

Document Summary

Passing variables by references requires the use of the address (&) and pointer (*) operators. Dr. deborah stacey: to be able to change a the variable, we must pass it to the subroutine by reference, i. e. send the pointer value to the subroutine. *p = 10; int main() int x = 7; printf(%dn, x); /* prints 7 */ fn(&x); printf(%dn,x); /* prints 10 */ Passing arrays: arrays are automatically passed by reference since they are pointers. The issue with strlen() char str[1000]; char *ptr = malloc (sizeof(char) * strlen(str) + 1)); Why add +1 to strlen(str) ? strlen reports the length of a string up to, but not including, the null terminator. You still need to allocate space for this character though! String operations: concatenate two strings, glue them together, strcat(s1_target, s2_additin); /* returns pointer to s1_target *, note s1_target must be long enough to accomodate both strings and a null terminator.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents