C language learned by suffering
C language learned by suffering
Exercise 19
fundamental knowledge
Q1-1
What do you call an array that can be freely created during program execution?
Q 1-2
What do you call a memory area of large size that is used over a long period of time?
program read-only
The following program has a major problem.
Briefly explain what kind of problem it is.
Briefly explain what kind of problem it is.
Q2-1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int* data;
data = (int*)malloc(sizeof(int) * 10);
return 0;
}
program writing
Q3-1
Exercise 16.
"Create a program that will enter and display the names, ages, and genders of three people.
The data should be stored in a structure.
In addition, dedicated functions should be created for data input and display, respectively."
Based on the problem "How many people can be inputted?
The input is terminated when -1 is entered for age.
Since the array number is an int type, it should be able to handle up to the maximum value of the int type.
"Create a program that will enter and display the names, ages, and genders of three people.
The data should be stored in a structure.
In addition, dedicated functions should be created for data input and display, respectively."
Based on the problem "How many people can be inputted?
The input is terminated when -1 is entered for age.
Since the array number is an int type, it should be able to handle up to the maximum value of the int type.
descriptive expression
Q4-1
You can store data using regular arrays, but you can't use arrays to store data.
Briefly explain why you dare to use dynamic arrays.
Briefly explain why you dare to use dynamic arrays.
Basic Knowledge (sample answers)
Solution 1-1
dynamic array
Solution 1-2
heap
Program reading (example solution)
Solution 2-1
Because the dynamic array is not deallocated (because you forgot to call the free function)
Memory space may remain wasted.
Memory space may remain wasted.
Program writing (example of solution)
Solution 3-1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[256];
int age;
int sex;
} People;
void InputPeople(People* data);
void ShowPeople(People data);
int main(void)
{
int i, count, datasize;
People* data;
datasize = 10;
data = (People*)malloc(sizeof(People) * datasize);
count = 0;
while (1) {
InputPeople(&data[count]);
if (data[count].age == -1)
break;
count++;
if (count >= datasize) {
datasize += 10;
data = (People*)realloc(data, sizeof(People) * datasize);
}
}
for (i = 0; i < count; i++) {
ShowPeople(data[i]);
}
free(data);
return 0;
}
void InputPeople(People* data)
{
printf("Name:");
scanf("%s", data->name);
printf("Age:");
scanf("%d", &data->age);
printf("Gender (1-male, 2-female):"); scanf("%d", &data->age); printf("%d", &data->age); scanf("%d", &data->age)
scanf("%d", &data->sex);
printf("\n");
}
void ShowPeople(People data)
{
char sex[16];
printf("Name:%s\n", data.name);
printf("Age:%d\n", data.age);
if (data.sex == 1) {
strcpy(sex, "male");
} else {
strcpy(sex, "female");
}
printf("Gender:%s\n", sex);
printf("\n");
}
*InputPeople and ShowPeople functions have not been changed, but
The InputPeople function can be changed to make it easier to input data.
*Increasing by 10 to reduce the number of calls to the realloc function.
If larger amounts of data are expected, it is more efficient to double the number of data.
*Major points are deducted if the FREEFREE function is not called.
Short Answer Type (Sample Answers)
Solution 4-1
The number of elements can be freely determined during program execution.
Because memory can be handled efficiently.
Because memory can be handled efficiently.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.