- You are given the following declarations and statements:
int *num_array;
int **char_array;
num_array = (int *)malloc(sizeof(int) * 15);
char_array = (char **)malloc(sizeof(char *) * 10);
char_array[3] = (char *)malloc(sizeof(char) * 30);
malloc returns the decimal addresses of 100, 300, and 500
respectively.
- What is the value of num_array?
- What is the value of num_array+4?
- What is the value of &num_array[6]?
- What is the value of char_array?
- What is the value of char_array[3]?
- What is the value of &char_array[3][2]?
- Is the following set of statements valid? Why or why not?
char **array;
array = (char **)malloc(sizeof(char *) * 20);
strcpy(array[5], "brad");
- Is the following set of statements valid if I want to create
a dynamic array of 30 strings, each of which is 10 characters long?
Why or why not?
char *array;
array = (char *)malloc(sizeof(char) * 300);
If you think it is valid, write two statements that:
- assign the character string "brad" to the 5th array element.
- access the 3rd character in the 5th array element and
assign that character to a variable
declared as "char letter". You may not say:
letter = 'a';
I want to see the array notation you would use to access the third
character.
If you do not think that the above set of statements is the proper
way to declare the desired array, then write a set of statements that
will do so and then write the two statements requested above.
- The following problem will give you practice with typedefs:
- Write a typedef statement that declares a struct with the following
three fields:
- a field of type double named "gross_pay,"
- a string capable of holding a string of up to 20 characters
named "last_name," and
- a field of type int named age.
You may call the struct whatever you like or you may make it be
anonymous. The name of the type should be "student.".
- Write a declaration that creates a variable named "new_student"
that is a pointer to a "student.".
- Write a statement that mallocs a student and assigns the
result to new_student.
- Write a printf statement that prints the following string in
a left-justified field of 20 spaces:
char *name;
- You are given the following declarations:
struct employee {
int age;
char last_name[20];
}
struct employee *a;
int *b;
char name[20];
char *save_name;
void *void_ptr;
Place a check mark next to any line you think might cause
a compiler error and explain why. Place an X next to any line
that you think could prove problematic at runtime and explain why.
You may assume that all variables that are assigned to another
variable have been given malloc'ed pieces of memory.
void_ptr = (void *)b;
void_ptr = (void *)name; save_name = (char *)void_ptr;
void_ptr = (void *)a; void_ptr->age = 30;
void_ptr = (void *)b; name = (char *)void_ptr;
void_ptr = a;
- Write a program
that formats a file so that all lines contain 80 characters or less.
If a word is the string "<p>" then you will output a blank line and
start a new paragraph. In effect you are writing an extremely simple
html formatter. Basically you will read words using the token library
presented in class and keep printing them on the current line until
you encounter a word
whose length would cause the line to exceed 80 characters. When you
encounter such a word you output a newline character and start a
new line. The words on a line should be separated with a single space.
You do not need to compile or run this program. Just make a good
faith effort to try to write it. If you do want to try to compile and
run the program you can find the tg.h and tg.c files
in ~cs140/www-home/spring-2005/notes/MalField/.