Question
Recall that Strings in C are arrays of ASCII character terminated by null character (i.e,'0' - the character with ASCII value 0). THe C library
Recall that Strings in C are arrays of ASCII character terminated by null character (i.e,'\0' - the character with ASCII value 0). THe C library function strcpy(char[] p, char[]q) copies string q to string p and is usually implemented as follows:
void strcpy(char[]a, char[]b) {
while(*a++ =*b++);
}
This code relies on C's pointer arithmetic, and has the effect of assinging the character of q into the array p until the null character that ends q is assigned to a slot in p.
a) Suppose that a C program defines three strings as follows:
char [] a1 = " Hello, world!", a2 = "Good morning, world!";
...
//code omitted
char[] a3 = "wonderful, the world!"
Assuming that a1 begins at memory location 1000, with a2 immediately following, sketch how a1 and a2 will appear in memory. Assume that each memory location takes one byte (enough for a single ASCII character).
b) Now, assuming that a3 occupies a section of memory that is different from a1 and a2 ( and distant from them), show that values of a1 and a2 after the following call.
strcpy(a1,a3);
c) What features of C does this code illustrate? How do these features affected realiability?
d) What features of JAVA and the JAVA API were desinged to address the problem illustrated above?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started