What is wrong with this code:int cp(void){ char *s; s = malloc(12); strcpy(s, "Best School"); return (0);}malloc can fail so we should check its return value all the time before using the pointers returned by the function.You can’t call strcpy with a string literalThere is no commentYou don’t have enough space to store the copy of the string “Best School”
Question
What is wrong with this code:int cp(void){ char *s; s = malloc(12); strcpy(s, "Best School"); return (0);}malloc can fail so we should check its return value all the time before using the pointers returned by the function.You can’t call strcpy with a string literalThere is no commentYou don’t have enough space to store the copy of the string “Best School”
Solution
The code has several issues:
-
malloccan fail: It's always a good practice to check ifmallochas successfully allocated memory or not. Ifmallocfails, it returns a NULL pointer. So, you should always check ifsis not NULL before using it. -
Size of allocated memory: The string "Best School" has 11 characters, but you also need space for the null terminator (
\0), which makes it 12 characters long. However,malloc(12)only allocates 12 bytes, which might not be enough depending on the system and the compiler. It's safer to calculate the size dynamically usingstrlenor to allocate a bit more than needed. -
strcpywith a string literal: There's nothing wrong with usingstrcpywith a string literal. The problem would be if the destination string (in this cases) doesn't have enough space for the source string (in this case "Best School"). -
Lack of comments: While it's good practice to comment your code, not having comments doesn't cause the code to fail. It just makes it harder for others (or future you) to understand what the code is doing.
-
Return type of the function: The function is declared to return an
int, but it doesn't return anything. This will likely cause a warning or an error, depending on the compiler settings. If you don't need to return anything, you should declare the function asvoid.
Here's a corrected version of the code:
#include <stdlib.h>
#include <string.h>
void cp(void){
char *s;
s = malloc(13); // Allocate enough space for "Best School" and the null terminator
if (s == NULL) {
// Handle the error, e.g., by exiting the program
exit(1);
}
strcpy(s, "Best School");
// Do something with s
// ...
// Don't forget to free the allocated memory
free(s);
}
Similar Questions
In the following code, which of the variables, a, b, c, d and e are strings/pointer-to-strings when code is at /* A */?#include <string.h>#include <stdio.h>void f(char *p) { p = "hello";}int main() { char a[16], c[16], *d, e[16]; char b[16] = {'h', 'e', 'l', 'l', 'o'}; strcpy(a, "hello"); f(c); d = "hello"; scanf("%s", e); // assume that the end-user entered hello\n /* A */ return 0;}
What is the output of the following code ?#include <stdio.h>#include <string.h>int main() { char str1[] = "Hello"; char str2[] = "World"; char str3[20]; strcpy(str3, strrev(strcat(str1, str2))); printf("%s", str3); return 0;}
#ifndef main.h#define main.h#include <stddef.h>int _putchar(char c);int _islower(int c);int _isalpha(int c);int _abs(int n);int _isupper(int c);int _isdigit(int c);int _strlen(char *s);void _puts(char *s);char *_strcpy(char *dest, char *src);int _atoi(char *s);char *_strcat(char *dest, char *src);char *_strncat(char *dest, char *src, int n);char *_strncpy(char *dest, char *src, int n);int _strcmp(char *s1, char *s2);char *_memset(char *s, char b, unsigned int n);char *_memcpy(char *dest, char *src, unsigned int n);char *_strchr(char *s, char c);unsigned int _strspn(char *s, char *accept);char *_strpbrk(char *s, char *accept);char *_strstr(char *haystack, char *needle);#endif main.h~ ~ ~
Consider these two statements: char v[6]; strcpy(v, "string");The best description of the situation after these two statements is that:Group of answer choicesThe call to strcpy() will corrupt memory and may result in a program run-time error at some later point in the program's executionA run-time array overflow will be detected, and the program will be halted and an error message printedThe call to strcpy() will be truncated, and only "tring" will be copied into vThe call to strcpy() is completely fine, and v will have the desired value of "string"The call to strcpy() will be truncated, and only "strin" will be copied into v
#include<stdio.h>#include<string.h>struct player { char pname[20];}pl;char* play(struct player *temp_pl){ strcpy(temp_pl->pname, "kohli"); return temp_pl->pname;}int main(){ strcpy(pl.pname, "dhoni"); printf("%s %s", pl.pname, play(&pl)); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.