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.
HOW TO PRINT THE CONTENTS OF A TWO-DIMENSIONAL ARRAY USING POINTER ARITHMETIC (USING FUNCTIONS) (C++) Fill...
HOW TO PRINT THE CONTENTS OF A TWO-DIMENSIONAL ARRAY USING POINTER ARITHMETIC (USING FUNCTIONS) (C++) Fill out the function definition for "void print_2darray_pointer(double *twoDD, int row, int col)". Should match the output from the function void print_2darray_subscript(double twoDD[][ARRAY_SIZE], int row, int col) # include using namespace std;    const int ARRAY_SIZE = 5;    const int DYNAMIC_SIZE = 15;    const int TIC_TAC_TOE_SIZE = 3;    // function definitions:    void print_2darray_subscript(double twoDD[][ARRAY_SIZE], int row, int col)        //...
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...
Code should be written in C Use pointer variables for parameter passing, where appropriate and pointer...
Code should be written in C Use pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays. 1) Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+)...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+) AND extend the code so that allocated memory is freed properly. Thank you struct foo { int a; char b; }; int main(void) { struct foo* arr[5]; int x; for(x = 0; x < 5; x++) { arr[x] = malloc(sizeof(struct foo)); arr[x]->a = 0; arr[x]->b = 'b'; } }
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...
I have to modify the following code to: 1. Use the unique algorithm to reduce the...
I have to modify the following code to: 1. Use the unique algorithm to reduce the array to unique values 2. Use the copy algorithm to display the unique results. #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() {     //creating an array of 20 integers     int array[20];     //creating an empty vector     vector<int> vec;     //input from end user to get 20 ints and a for loop to interate through 20     cout << "Enter 20 integers:"...
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; //...
How can i modify my c code so that each number stored in the array is...
How can i modify my c code so that each number stored in the array is not the array index but the value of the array index converted to radians. I have already made a function for this converion above main(). Below is the code: #include <stdio.h> #include <stdlib.h> #include <math.h> float Deg2Rad (float degrees) { // Calculate & return value float Result; Result = ((M_PI*degrees)/180.0); return (Result); } int main(void) { // Declare variables int Array[90]; int i; //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT