Question
Problem 1a Write a C++ program called hw_1a.cpp that prints the value of argc followed by the addresses of each argv argument as well the
Problem 1a
Write a C++ program called hw_1a.cpp
that prints the value of argc followed by the addresses of each argv argument as well the corresponding (C-style) strings. The program must contain everything needed to compile. The following is an example output:
unix> ./hw_1a This is great
Num args = 4
argv[0] = 0x7fffeb3fb1f8 ./hw_1a
argv[1] = 0x7fffeb3fb200 This
argv[2] = 0x7fffeb3fb208 is
argv[3] = 0x7fffeb3fb210 great
Problem1b
Copy hw_1a.cpp
into a new program called hw_1b.cpp
that computes and prints the length of each
command line argument. See an output example below. Do not use functions from
Instead, write your own function int strlen(char*) which is given a pointer to a C-style string, namely, argv[i], and returns the length thereof (number of characters).
Use a pointer to advance through the string.
Use pointer dereferencing to determine when to stop.
Hint: C-style strings are NULL-terminated meaning the last character equals \0; the condition *s==\0 is thus met when the end of the string has been reached.
Hint: See the pointer_handout for a related function that compares two C - style strings.
unix> ./hw_1b I learn so much!
Num args = 5
argv[0] = 0x7fff8b27e098 ./hw_b (strlen=6)
argv[1] = 0x7fff8b27e0a0 I (strlen=1)
argv[2] = 0x7fff8b27e0a8 learn (strlen=5)
argv[3] = 0x7fff8b27e0b0 so (strlen=2)
argv[4] = 0x7fff8b27e0b8 much! (strlen=5)
please help, thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started