Question

In: Computer Science

Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops...

Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops to do the same thing this code does. Be sure that you understand how the code works and how the pointer arithmetic relates to the array expression form. Provide liberal comments to explain what your pointer arithmetic is computing.

#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{ int arg_count = 0;
for (arg_count = 0; arg_count < argc; arg_count++)
printf("%s\n", argv[arg_count]);
}


Solutions

Expert Solution

Here, We have used the double pointer to give the command line arguments. so instead of array expression which is shown in printf line, we can use pointer expression to do the same thing.

Here what the code does : when you are running the file and entering your executable file with command line arguments, It will print all the arguments including filename also.

Eg. If your executable file name is MyFile and you are running that from CMD like this:

INPUT : MyFile Argument1 Argument2 and after enter you will see the following output like:

OUTPUT:

MyFIle

Argument1

Argument 2

So, Here I am writing the same code without array expression:

#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{

.// argc contain the number of argument provided by the user. Here above has 3 arguments. So argc will be 3. and It has been decresing till it reaches to 0. so in our case this loop will execute 3 times.
   while(argc-- > 0){

// Now here we have **argv. which contains the address of first command line argument. while *argv will contain the value at that address. which is first command line. so now *argv++ will be increment in address(**).

so FIrst It will print the first argument after that all the arguments.
       printf("%s\n",*argv++);
   }
}


Related Solutions

In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
This assignment will acquaint you with the use of while loops and boolean expressions. You will...
This assignment will acquaint you with the use of while loops and boolean expressions. You will create a program that acts as a score keeper of a racquet ball game between two players. The program will continually ask the user who the winner of every point is as the game is played. It will maintain the scores and declare the match is over, using these rules: (1) A game is over when a. one of the players wins 7 points...
1.Note: The following code is written only in main -- you assume there is an array...
1.Note: The following code is written only in main -- you assume there is an array bag and you are not adding any methods to that bag. Write the main program to create two array bags of type string. Place 5 strings of your choice in the first bag and 5 strings (of your choice) in the second bag. Your program must: a) determine if there are any strings in the first bag that appears in the second bag as...
Modify the following java code, utilizing a loop mechanism to enable the user to use the...
Modify the following java code, utilizing a loop mechanism to enable the user to use the calculator more than once. The program does the following:    It prompts the user to enter 2 numbers.    It prompts the user to choose an operation to perform on those numbers:    Operation 1: Addition.    Operation 2: Subtraction.    Operation 3: Multiplication.    Operation 4: Division.    It outputs the result of the operation.    It asks the user if they want...
Modify the following code to use 'envp' instead of 'environ'. Be sure that you understand how...
Modify the following code to use 'envp' instead of 'environ'. Be sure that you understand how the code works. Provide liberal comments to explain what your pointer arithmetic is computing. Also, answer the following questions and explain your answers: i) WHERE in process memory is it most likely that each of the values exist at run time? ii) WHERE in process memory is it most likely the actual strings containing environment variables are stored? #include #include extern char **environ; //...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
Question 6 Which of the following for loops will find the largest element in the array...
Question 6 Which of the following for loops will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values? Question 6 options: largest = None for i in range(len(numbers)): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(numbers): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(len(numbers)): if largest is None or numbers[i]...
Solve in C++ program. Modify the use of queue data structure such that the array used...
Solve in C++ program. Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire...
c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire assignment. There are no more directions to it. Create an array of struct “employee” Fill the array with information read from standard input using C++ style I/O Shuffle the array Select 5 employees from the shuffled array Sort the shuffled array of employees by the alphabetical order of their last Name Print this array using C++ style I/O Random Number Seeding We will make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT