Question

What is wrong with the following code segment, assumming it compiles correctly? char *a = "Hello";...

What is wrong with the following code segment, assumming it compiles correctly?

char *a = "Hello";

char *b = " Mom";

while( *b != '\0' ) {

*a = *b;

a++;

b++;

}

Homework Answers

Answer #1

If your code compiles correctly then it is your logical error.

Here you can not write *a = *b like this as normal variable.

As you are assigning a value of pointer b to pointer a you must write like *a == *b;

*a = *b will give you segmentation fault.

while *a == *b works correctlly.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The funciton my_strcpy(char dest, const char cource) is to copy "hello world" from source to dest....
The funciton my_strcpy(char dest, const char cource) is to copy "hello world" from source to dest. Do not use the library. The copy process will only stop at the last "\0". Make sure dest is large enough to any lenght of sentence inputted. The following is my code. The program copy the first letter of "hello world" which is "h", but the requirement is to copy the whole sentence "hello world". Thank you for your helps #include <iostream> using namespace...
What is the difference between the following two declarations? char array[] = “Hello World”; char *array...
What is the difference between the following two declarations? char array[] = “Hello World”; char *array = “Hello World”;
What is wrong with this code? (5 pts) <html> <body> <h1> Hello world </h1> </body> <head>...
What is wrong with this code? (5 pts) <html> <body> <h1> Hello world </h1> </body> <head> <title>My First Script </title> </head> </html>
Convert the following C code to assembler: a) unsigned char A, B, C; if (A >...
Convert the following C code to assembler: a) unsigned char A, B, C; if (A > B) C = A; elseif (A == B) C = 0; else C = B; b) unsigned char A, B; A = 228; B = A/4; c) unsigned char A, B; A = 228; B = A/5;
What will be the output of the following code? #include <iostream> using namespace std; int main()...
What will be the output of the following code? #include <iostream> using namespace std; int main() {    char *s = "hello";    char *p = s;    cout << *(p+3) << " " << s[1] << endl; }
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
What is the time complexity of the following segment of code assuming that the function temp...
What is the time complexity of the following segment of code assuming that the function temp takes time O(n)? ... ... x = n y = n while y > 1:      print(x)      temp(x)      y = y / 2      x = x – 1 temp(x) … …
Show what is written by the following segment of code, given that element1, element2, and element3...
Show what is written by the following segment of code, given that element1, element2, and element3 are int variables, and queue is an object that fits the ADT of a queue. element1 = 1; element2 = 0; element3 = 4; queue.enqueue(element2); queue.enqueue(element1); queue.enqueue(element1 + element3); element2 = queue.dequeue( ); queue.enqueue(element3 * element3); queue.enqueue(element2); queue.enqueue(3); element1 = queue.dequeue( ); System.out.println(element1 + “ “ + element2 + “ “ + element3); while (!queue.isEmpty( )) {    element1 = queue.dequeue( );    System.out.println(element1)...
The following code should print X is greater than 0. However, the code has errors. Fix...
The following code should print X is greater than 0. However, the code has errors. Fix the code so that it compiles and runs correctly. (Rewrite the program with all the fixes) public class Test1 { public static void main(String[] args) { int x = 3; if (x > 0 System.out.println("x is greater than 0") else System.out.println(x is less than or equal 0"); } } Must be in java and easy to understand grade 11 course
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT