Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The printf function also recognizes a 8 p format specifier, which expects a value of type pointer. However, unlike the 8 s format, 8 p

The printf function also recognizes a 8 p format specifier, which expects a value of type "pointer". However, unlike the 8 s format, 8 p tells printf to simply print the address as a hexadecimal number. (This can sometimes be useful in debugging, for getting a general idea where something is pointing.) Create the programs hello3[abc].c by editing hello2[abc].c and only changing the 8s format specifier to 8 p in each one. Compile and run each program. What do you observe about the addresses? How do they differ?
Now create hello4[abc].c by copying/editing hello2[abc].c to insert the following lines after the call to printf in each:
whomtogreet [3]=?'k';
whomtogreet [4]=073;
printf("Hello,%s!
", whomtogreet);
Compile and run hello4a.c, then hello4b.c. Do you see what is happening? Note that hex 73(decimal 115) is the ASCII code for 's'. You can change the individual elements of a character string stored in a declared array. Also, char variables store 8-bit values; the interpretation of those values is up to the software-in this case, printf.
Now compile and run hello4c.c. What is different? To find out where the problem is, recompile with debugging enabled by giving the -g flag to the compiler: This tells the compiler to include information the debugger can use. Now run the program under the debugger: gdb ./a.out and give the command run at the (gab) prompt. The debugger will show you which line of the program caused the "segmentation fault". There are two takeaways to note here:
First: We saw (above) that the name of an array variable can be interpreted as a value of type char *. In a similar way, a variable of type char *(as whomtogreet is declared in this program) can be used like the name of an array, with brackets to denote an offset. So whomtogreet [3] denotes a value of type char.
Second: string constants (literals) like "world" are stored in a read-only area of memory. When our hello4c program tries to change the contents of the string, it crashes. Bottom line: string literals cannot be modified at runtime.
End your script session by typing exit or control-D. Verify that the script file exists in your current directory using the 15 command, which, without any arguments, lists the files in the current directory. The output of 15 should show your hello*.c files, plus one a.out file, plus your script file. (Note: You should see a message of the form "Script done" when you end the script session. If you don't see it, wait a few seconds and type ????D a few more times. NEVER look at your script output file if the script_program might still be running-it will effectively disable your VM and you will probably have to get help from CS IT.)
Delete the a.out file with the rm("remove") command:
Go back to the parent directory of cs270labo (that is most likely your home directory). You should be able to get there by doing popd or cd ....(Also, cd with no arguments will always take you to your home directory.)
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions

Question

Where is the position?

Answered: 1 week ago