C_Crib_Sheet_Exam_2 - Chapter 6 Arrays atoi-converts string...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 6 – Arrays atoi(#)- converts string to integer atof(#)- converts string to float float temps[100] creates 100 float variables represents the address of the 1st element of the array temps[1] would access the variable stored in the 2nd cell of the array. int id[4] = {45, 2, 800, 81} would define and initialize the array. => id[1] = 2 * if a definition includes fewer values than cells that were declared in the array, the rest of the elements are set to 0. temps = 5 /* ERROR */ because temps is a pointer constant and can not have its value changed. int *ptr defines a pointer to hold an address of a type int. stores the address of the 1st OR ptr = temps element of temps to the pointer variable ptr. ptr++ ptr would now point to the address of the 2nd element of temps. Character strings as arrays of characters * Strings must be represented as an array of characters. The array uses one cell for each character in the string and a final cell to hold the null character ‘\0’. 3 ways to define character arrays. 1. Individually, element by element char stooge1[4]; stooge1[0] = ‘M’; stooge1[1] = ‘o’; stooge1[2] = ‘e’; stooge1[3] = ‘\0’; * do not use “M” double quotes to store individual characters. 2. Using scanf or fscanf char stooge2[6]; printf(“enter name\n” scanf(“%s”,stooge2); *if Curly was typed, it would be stored one element at a time into stooge2 and when carriage return was typed the ‘\0’ would be inserted. 3. At time of declaration char stooge3[6] = “Larry”; the ‘\0’ is automatically inserted by C. To print strings: Printf(“%s %s %s”,stooge1,stooge2,stooge3 ) Arrays as Function Arguments int sum(int a[], int n) Function header, sum will receive an array (a) and the number of elements in a (n) and will sum the array. int b[10] = {1,2,3,4,5,6,7,8,9,0}; m=sizeof(b)/sizeof(int); x=sum(b,m); This would call the sum function and send it the array b and the number of elements in b. The sum calculated by the sum function would be returned and stored in x. x=sum(&b[0],m); would do the same thing. String-Handling Functions string.h library needed. strcat(string1,string2); the two strings are concatenated properly null terminated and stored in string1. strncat(string1,string2,num); like strcat except num specifies the max number of characters to be used from string2. char string1=”gladiator”,string2 =”gladiolus”; strcmp(string1,string2); strcmp and strncmp expect the addresses of 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
null terminated character strings. It compares them lexicographically & returns: - int if string1 is < string 2. +int if string1 is > string2. 0 if they are the same. strncmp is the same except it expects a 3 rd argument which is the max number of characters to compare. And strncmp returns a 0 if string1 is < string2 instead of a –int. strcpy(string1,string2);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 16

C_Crib_Sheet_Exam_2 - Chapter 6 Arrays atoi-converts string...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online