Read Sams Teach Yourself C in 24 Hours Online
Authors: Tony. Zhang
{
int i;
for (i=0; i
printf(“%s\n”, str1[i]);
}
/* function definition */
void StrPrint2(char *str2)
{
printf(“%s\n”, str2);
}
Hour 17, “Allocating Memory”
Quiz
1. The answers are as follows:
• 200 bytes
• 200 bytes
• 200 bytes
• 0 bytes
2. The statement is
ptr = realloc(ptr, 150 * sizeof(int));
3. The final size is 120 bytes, provided the int data type is one byte long.
4. The final size is 0. In other words, all allocated memory blocks have been released by the last statement.
Exercises
1. The following is one possible solution:
/* 17A01.c */
#include
#include
/* main() function */
main()
{
33 067231861x AppxB 4.10.2000 11:05 AM Page 477
Answers to Quiz Questions and Exercises
477
int *ptr_int;
int i, sum;
int max = 0;
int termination = 0;
printf(“Enter the total number of integers:\n”);
scanf(“%d”, &max);
/* call malloc() */
ptr_int = malloc(max * sizeof(int));
if (ptr_int == NULL){
printf(“malloc() function failed.\n”);
termination = 1;
}
else{
for (i=0; i
ptr_int[i] = i + 1;
}
sum = 0;
for (i=0; i
sum += ptr_int[i];
printf(“The sum is %d.\n”, sum);
free(ptr_int);
return termination;
B
}
2. The following is one possible solution:
/* 17A02.c */
#include
#include
/* main() function */
main()
{
float *ptr_flt;
int termination = 0;
/* call calloc() */
ptr_flt = calloc(100, sizeof(float));
if (ptr_flt == NULL){
printf(“calloc() function failed.\n”);
termination = 1;
}
else{
ptr_flt = realloc(ptr_flt, 150 * sizeof(float));
if (ptr_flt == NULL){
printf(“realloc() function failed.\n”);
termination = 1;
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 478
478
Appendix B
else
free(ptr_flt);
}
printf(“Done!\n”);
return termination;
}
3. The following is one possible solution:
/* 17A03.c */
#include
#include
/* main() function */
main()
{
float *ptr1, *ptr2;
int i;
int termination = 1;
int max = 0;
printf(“Enter the total number:\n”);
scanf(“%d”, &max);
ptr1 = malloc(max * sizeof(float));
ptr2 = calloc(max, sizeof(float));
if (ptr1 == NULL)
printf(“malloc() failed.\n”);
else if (ptr2 == NULL)
printf(“calloc() failed.\n”);
else{
for (i=0; i
printf(“ptr1[%d]=%5.2f, ptr2[%d]=%5.2f\n”,
i, *(ptr1 + i), i, *(ptr2 + i));
free(ptr1);
free(ptr2);
termination = 0;
}
printf (“\nBye!\n”);
return termination;
}
4. The following is one possible solution:
/* 17A04.c: Use the realloc() function */
#include
#include
33 067231861x AppxB 4.10.2000 11:05 AM Page 479
Answers to Quiz Questions and Exercises
479
#include
/* function declaration */
void StrCopy(char *str1, char *str2);
/* main() function */
main()
{
char *str[4] = {“There’s music in the sighing of a reed;”,
“There’s music in the gushing of a rill;”,
“There’s music in all things if men had ears;”,
“There earth is but an echo of the spheres.\n”
};
char *ptr;
int i;
int termination = 0;
ptr = realloc(NULL, strlen((str[0]) + 1) * sizeof(char));
if (ptr == NULL){
printf(“realloc() failed.\n”);
termination = 1;
}
else{
StrCopy(str[0], ptr);
printf(“%s\n”, ptr);
B
for (i=1; i<4; i++){
ptr = realloc(ptr, (strlen(str[i]) + 1) * sizeof(char));
if (ptr == NULL){
printf(“realloc() failed.\n”);
termination = 1;
i = 4; /* break the for loop */
}
else{
StrCopy(str[i], ptr);
printf(“%s\n”, ptr);
}
}
}
realloc(ptr, 0);
return termination;
}
/* funciton definition */
void StrCopy(char *str1, char *str2)
{
int i;
for (i=0; str1[i]; i++)
str2[i] = str1[i];
str2[i] = ‘\0’;
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 480
480
Appendix B
Hour 18, “Using Special Data Types and
Functions”
Quiz
1. The enumerated names Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, and Dec represent the values of 0 to 11, respectively.
2. The values of 0, 10, 11, and 12 are represented by the enumerated names name1, name2, name3, and name4, respectively.
3. The typedef long int BYTE32; and BYTE32 x, y, z; statements are equivalent to long int x, y, z;.
typedef char *STRING[16]; and STRING str1, str2, str3; are equivalent to char *str1[16], *str2[16], *str3[16];
4. No. The void keyword in the main() function indicates that there is no argument passed to the function.
Exercises
1. The following is one possible solution:
/* 18A01.c */
#include
main(void)
{
enum tag {name1,
name2 = 10,
name3,
name4 };
printf(“The value represented by name1 is: %d\n”,
name1);
printf(“The value represented by name2 is: %d\n”,
name2);
printf(“The value represented by name3 is: %d\n”,
name3);
printf(“The value represented by name4 is: %d\n”,
name4);
return 0;
}
2. The following is one possible solution:
/* 18A02.c */
#include
33 067231861x AppxB 4.10.2000 11:05 AM Page 481
Answers to Quiz Questions and Exercises
481
main(void)
{
typedef char WORD;
typedef int SHORT;
typedef long LONG;
typedef float FLOAT;
typedef double DFLOAT;
printf(“The size of WORD is: %d-byte\n”, sizeof(WORD));
printf(“The size of SHORT is: %d-byte\n”, sizeof(SHORT));
printf(“The size of LONG is: %d-byte\n”, sizeof(LONG));
printf(“The size of FLOAT is: %d-byte\n”, sizeof(FLOAT));
printf(“The size of DFLOAT is: %d-byte\n”, sizeof(DFLOAT));
return 0;
}
3. The following is one possible solution:
/* 18A03.c */
#include
enum con{MIN_NUM = 0,
MAX_NUM = 100};
B
int fRecur(int n);
main()
{
int i, sum1, sum2;
sum1 = sum2 = 0;
for (i=1; i<=MAX_NUM; i++)
sum1 += i;
printf(“The value of sum1 is %d.\n”, sum1);
sum2 = fRecur(MIN_NUM);
printf(“The value returned by fRecur() is %d.\n”, sum2);
return 0;
}
int fRecur(int n)
{
if (n > MAX_NUM)
return 0;
return fRecur(n + 1) + n;
}
4. The following is one possible solution:
/* 18A04.c: Command-line arguments */
#include
main (int argc, char *argv[])
33 067231861x AppxB 4.10.2000 11:05 AM Page 482
482
Appendix B
{
int i;
if (argc < 2){
printf(“The usage of this program is:\n”);
printf(“18A04.EXE argument1 argument2 [...argumentN]\n”);
}
else {
printf(“The command-line arguments are:\n”);
for (i=1; i
printf(“%s “, argv[i]);
printf(“\n”);
}
return 0;
}
Hour 19, “Understanding Structures”
Quiz
1. The semicolon (;) should be included at the end of the structure declaration.
2. u, v, and w are three structure variables.
3. You can initialize the array of the automobile structure like this:
struct automobile {
int year;
char model[8]} car[2] = {
{1997, “Taurus”},
{1997, “Accord”}};
Exercises
1. The following is one possible solution:
/* 19A01.c */
#include
main(void)
{
struct automobile {
int year;
char model[10];
int engine_power;
double weight;
} sedan = {
1997,
“New Model”,
33 067231861x AppxB 4.10.2000 11:05 AM Page 483
Answers to Quiz Questions and Exercises
483
200,
2345.67};
printf(“year: %d\n”, sedan.year);
printf(“model: %s\n”, sedan.model);
printf(“engine_power: %d\n”, sedan.engine_power);
printf(“weight: %6.2f\n”, sedan.weight);
return 0;
}
2. The following is one possible solution:
/* 19A02.c */
#include
struct employee {
int id;
char name[32];
};
void Display(struct employee s);
main(void)
{
B
/* structure initialization */
struct employee info = {
0001,
“B. Smith”
};
printf(“Here is a sample:\n”);
Display(info);
printf(“What’s your name?\n”);
gets(info.name);
printf(“What’s your ID number?\n”);
scanf(“%d”, &info.id);
printf(“\nHere are what you entered:\n”);
Display(info);
return 0;
}
/* function definition */
void Display(struct employee s)
{
printf(“Employee Name: %s\n”, s.name);
printf(“Employee ID #: %04d\n\n”, s.id);
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 484
484
Appendix B
3. The following is one possible solution:
/* 19A03.c Use the -> operator */
#include
struct computer {
float cost;
int year;
int cpu_speed;
char cpu_type[16];
};
typedef struct computer SC;
void DataReceive(SC *ptr_s);
main(void)
{
SC model;
DataReceive(&model);
printf(“Here are what you entered:\n”);
printf(“Year: %d\n”, model.year);
printf(“Cost: %6.2f\n”, model.cost);
printf(“CPU type: %s\n”, model.cpu_type);
printf(“CPU speed: %d MHz\n”, model.cpu_speed);
return 0;
}
void DataReceive(SC *ptr_s)
{
printf(“The type of the CPU inside your computer?\n”);
gets(ptr_s->cpu_type);
printf(“The speed(MHz) of the CPU?\n”);
scanf(“%d”, &(ptr_s->cpu_speed));
printf(“The year your computer was made?\n”);
scanf(“%d”, &(ptr_s->year));
printf(“How much you paid for the computer?\n”);
scanf(“%f”, &(ptr_s->cost));
}
4. The following is one possible solution:
/* 19L04.c Arrays of structures */
#include
struct haiku {
int start_year;
int end_year;
33 067231861x AppxB 4.10.2000 11:05 AM Page 485
Answers to Quiz Questions and Exercises
485
char author[16];
char str1[32];
char str2[32];
char str3[32];
};
typedef struct haiku HK;
void DataDisplay(HK *ptr_s);
main(void)
{
HK poem[2] = {
{ 1641,
1716,
“Sodo”,
“Leading me along”,
“my shadow goes back home”,
“from looking at the moon.”
},
{ 1729,
1781,
“Chora”,
“A storm wind blows”,
B
“out from among the grasses”,
“the full moon grows.”
}
};
/* define an array of pointers with HK */
HK *ptr_poem[2] = {&poem[0], &poem[1]};
int i;
for (i=0; i<2; i++)
DataDisplay(ptr_poem[i]);
return 0;
}
void DataDisplay(HK *ptr_s)
{
printf(“%s\n”, ptr_s->str1);
printf(“%s\n”, ptr_s->str2);
printf(“%s\n”, ptr_s->str3);
printf(“--- %s\n”, ptr_s->author);
printf(“ (%d-%d)\n\n”, ptr_s->start_year, ptr_s->end_year);
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 486
486
Appendix B
Hour 20, “Understanding Unions”
Quiz
1. The first statement is the declaration of a union with the tag name of _union. The second statement defines two union variables, x and y, with the a_union data type.
2. The semicolon (;) is missed in two places: at the end of the declaration of char model[8] and at the end of the declaration of the union.
3. The two union members have the same value, 1997.
Exercises
1. The following is the modified version. The content of the name array is partially overwritten by the value assigned to the double variable price.
/* 20A01.c */
#include
#include
main(void)
{
union menu {
char name[23];
double price;
} dish;