'C' has it's own particular way of dealing with 'strings'. In discussing 'strings', we become aware of the data structure of an 'array'. One of our resource links discusses 'arrays' in detail: (from cppreference.com)
This study of 'strings' (an ARRAY of characters), only scratches the surface of how we can incorporate arrays in our program. "struct stock_items *si_storage[200];" declares an array (with 200 elements) of 'pointers' to the 'stock_items' structure. Now THAT'S exciting code! |
We should keep in mind the fact 'strings' are NULL TERMINATED. See the graphic below for a visual |
/* pgm6 source */
#include <stdio.h>
#include <string.h> // provides prototype for functions strcpy() "string copy" and strlen() "length of string"
void prnt_blank_line(void); // our own defined function, NOT a library function such as printf()
int main(void) {
char fn_initial; // one character - first name
char mn_initial; // one character - middle name
char ln_initial; // one character - last name
char full_name[60]; // 'string' is an ARRAY ([]) of individual characters, here, 60 chars
int size_of_full_name;
prnt_blank_line(); prnt_blank_line();
fn_initial = 'C'; mn_initial = 'P'; ln_initial = 'G'; // yes, it's OK to have multiple statements on one line
strcpy(full_name, "Charles Paula Greenwood"); // there are MANY string-handling library functions
size_of_full_name = strlen(full_name); // 'strlen()' determines # of characters up to the NULL ('\0')
printf("The name %s has initials %c. %c. %c. \n\n", full_name, fn_initial, mn_initial, ln_initial);
printf("By the way, the full name had %d characters.\n\n", size_of_full_name);
prnt_blank_line(); prnt_blank_line();
return(0);
}
void prnt_blank_line() {
printf("\n");
}
Did you notice we created our first "non-library" function? "non-library" means a routine that we create that's NOT part of the function calls in our 'C' library. Our function 'prnt_blank_line()' does a trivial thing. In future, we'll see how useful functions BESIDES 'main()' are to our program's creation.