Question

In: Computer Science

The following code takes in a command line argument and copies it into a buffer before...

The following code takes in a command line argument and copies it into a buffer before working on it. Explain in detail any problems that you see in the following C code and how you would fix it. Note only describe how you would fix it, do not actually rewrite or give me another version of the code.

void bad_function(char *input)
{
char dest_buffer[32];
char input_len = strlen(input);
if (input_len < 32) {
strcpy(dest_buffer, input_len);
printf(“Command line argument is %s.\n”,dest_buffer);
}
else {
printf(“Error - input is too long for buffer.\n”);
}
// Works with the dest_buffer here
}

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for Question . Be safe .

Answer :

strcpy(dest_buffer, input_len); //error line

strcpy(dest_buffer, input);    //this will fix program

Explanation :

strcpy function copy data of source char array to destination array .

strcpy(destination array ,source array)

both argument should be array not some not variable

but in above program we are passing a char variable instead of char array .

strcpy(dest_buffer,input) //source and destination argument are char array .

now code will copy input array or command line array to dest_buffer array if length of input array is less than 32 .

Fixed Code :


void bad_function(char *input)
{
char dest_buffer[32];
char input_len = strlen(input);
if (input_len < 32) {
strcpy(dest_buffer, input);
printf("Command line argument is %s.\n",dest_buffer);
}
else {
printf("Error - input is too long for buffer.\n");
}
// Works with the dest_buffer here
}

Please up vote ,comment if any query .


Related Solutions

Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
UNIX ONLY Write a bash script that will accept a filename as a command line argument....
UNIX ONLY Write a bash script that will accept a filename as a command line argument. Your script should first check whether the filename has been passed as argument or not (hint: at least one argument has to be provided). If no argument has been provided your script should display appropriate message and exit. If a file name has been provided, your script should check if the file exists in the current directory and display the contents of the file....
Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form: GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control...
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control the depth of the recursion.
Write a Java application that accepts a bar code as a command line parameter and prints...
Write a Java application that accepts a bar code as a command line parameter and prints out the ZIP code. Assume that the bar code uses the symbols "|" and ":" for the long and short bars, respectively. Provide warnings on errors in the bar code specifying what exactly is wrong. The bar code input should be in the format specified in Problem 1, including the pair of the full bars at the beginning and at the end. Important: The...
Write a java code that: 1) Takes as an argument an integer number, say N 2)...
Write a java code that: 1) Takes as an argument an integer number, say N 2) Creates an array of size N 3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N. 4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT