What is wrong with the following code fragment?
char *src = "hello";
char *dst = (char *) malloc(strlen(src)); //too small!
strcpy (dst, src); //work properly
char *src = "hello"; char *dst = (char *) malloc(strlen(src)); //too small! <- this line is the issue. strcpy (dst, src); //work properly strlen(src) gives a value of 5. because there are 5 characters in string "hello" but there is also a hidden character of '\0', which indicates the end of a string. so, to copy the string src into dst properly, we need to allocate one extra character to hold the end of the string character. so, line 2 should be changed to the code below. char *dst = (char *) malloc(1+strlen(src)); Modified code: --------------- char *src = "hello"; char *dst = (char *) malloc(1 + strlen(src)); strcpy (dst, src);
What is wrong with the following code fragment? char *src = "hello"; char *dst = (char...
What is wrong with the following code fragment? AND Rewrite it so that it produces correct output. Java Code: if (total == MAX) if (total < sum) System.out.println(“total == MAX and < sum.”); else System.out.println(“total is not equal to MAX”);
What does this code do? On the flipside of this paper, write the same code in Java. #include <stdio.h> #include <stdlib.h> int main ) ( char *str: (char *) malloc (15) ; str strcpy (str, "Iron"); printf("String 8u\n", str, str); Address %s, (char ) realloc (str, 25); str strcat (str, "Maiden"); Su\n", str, str): Address %s, printf ("String free (str) return (0); What does this code do? On the flipside of this paper, write the same code in Java. #include...
Given the struct xx with the following members: int x-3; char namell-"hello"; What would be the output of the following program? 1. void main) 2. 3. struct xx "s-malloc(sizeof(struct xx)); 4, printf("%d",s->x); 5. printf("%s",s->name); 3 hello Garbage value hello 3 Garbage Value Compiler error None of the above
Consider the following matrix transpose routines: typedef int array[4][4]; void transpose (array dst, array src) { int i, j; for (i=0; i<4; i++) { for (j=0; j<4; j++) { dst[i][j] = src[i][j]; } } } void transpose2 (array dst, array src) { int i, j; for (i=0; i<4; i++) { for (j=0; j<4; j++) { dst[j][i] = src[j][i]; } } } Assume this code runs on...
Programming In C A) What will print after the following statements execute? char s1[50] = "jack"; char s2[50] = "jill"; char s3[50]; printf("%s", strcat(strcat(strcpy(s3, s1), " and "), s2)); B) What will print after the following statements execute? char s1[50] = "jack"; char s2[50] = "jill"; char s3[50]; printf("%u", strlen(s3));
What is wrong with the following code snippet? print("Hello") print("World!") The print function cannot be called twice The print function is missing an argument Nothing, the program prints Hello World on the same line The second line should not be indented
The following lines of code all have problems. Identify what is wrong with each line of code The following lines of code all have problems. Identify what is wrong with each line of code. a) for(j=0; j<= 10; j++) cout << prices[j]; b) int array = {1,2,3,4}; c) int arr[3]; for (arr = 0; arr < = 10; arr++) d) char k; for (k=0; k<= 10; k++)
What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;
What security flaw(s) might exist in the following C software? void chopUserInput( char *mon, char *day, char *input ) { char *m, *d; m = strtok( input, “/”); d = strtok( NULL, “/” ); strcpy( mon, m ); strcpy( day, d ); }
In the following code, what is the first line that introduces a memory leak into the program? (Type the line number into the box below) 1: #include <stdio.h> 2: #include <stdlib.h> 3: #include <string.h> 4: int main() { 5: char *word1 = NULL; 6: word1 = malloc(sizeof(char) * 11); 7: word1 = "bramble"; 8: char *word2 = NULL: 9: word2 = malloc(sizeof(char) * 11); 10: word2 = word1; 11: return 0; 12: }