Given the following code in C: -
-
char *s[] = {"program","test","load","frame","stack",NULL};
-
char **p = s + 2;
-
-
Questions:
a) printf{"%s",p[-2] + 3);
b) printf("%c%s",* (s[1]),*p+1);
c) printf("%c%c%c% c", **s+1, *(*(p-1)+1), *(*s+3)+2, *(p[-1]+strlen(p[-1])-1));
d) *(*p+2) = '\0';
strncat(*p,p[2],2);
printf("%s",*p) ;
My Answers:
a) frame
b) toad
c) rea_ (the last character I don't know)
d) lost
Can someone verify if my answers are correct, if not please help me!
THANK YOU!!
4 2271 horace1 1,510
Recognized Expert Top Contributor
I make the answers
(a) gram
(b) toad
(c) qeit
(d) causes a segmentation fault as the expression
attempts to change the 'a' in "load" to '\0' - "load" is a constant
a) is gram because p[-2] is a char* pointing to the 'p' in 'program'. Incrementing this pointer by 3 brings us to 'g'. printf will print all characters until the null character so we get 'gram'
b) you are correct
c) **s is the character 'p' and p + 1 is 'q'
So we have 'q'
(p - 1) is 'test'
*(p - 1) is the char * pointing to 't'
(*(p - 1)+1) increments the pointer by one character to 'e'
*(*(p-1)+1) = 'e'
So we have 'qe'
*s is a char* pointing to 'p'
(*s + 3) increments this pointer to point to 'g'
*(*s + 3) is the char 'g'
'g' + 2 = 'i' therefore *(*s+3)+2 = 'i'
So we have 'qei'
strlen(p[-1]) is the length of 'test' which is 4
strlent(p[-1]) -1 is 4 - 1 = 3
p[-1] is a char* pointing to 't'
p[-1]+strlen(p[-1])-1 increments this char* by 3 to point to the last char in 'test' which is also 't'
*(p[-1]+strlen(p[-1])-1) is the character 't'
So we have 'qeit'
Thank you for all your help!
I now understand a-c, but still uncertain about part d.
I don't understand how there is a segmentation fault, if that is the case.
Can someone please explain what's going on on part d?
Thanks again!
You were correct in theory about the answer being 'lost' which is why I did not mention it. However, as Horace1 has pointed out, the code for part d raises an error. Perhaps you can get extra credits for pointing this out :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: teachtiro |
last post by:
Hi,
'C' says \ is the escape character to be used when characters are
to be interpreted in an uncommon sense, e.g. \t usage in printf(),
but for printing % through printf(), i have read that %% should be used.
Wouldn't it have been better (from design perspective) if the same
escape character had been used in this case too.
Forgive me for posting without verfying things with any standard
compiler, i don't have the means for now.
|
by: whatluo |
last post by:
Hi, c.l.cs
I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout\n"); while the others does't. So
can someone tell me whether there any gains in adding
(void) to printf.
Thanks,
Whatluo.
|
by: nandh_dp |
last post by:
When the below program is compiled and executed,
#include <stdio.h>
#define MASK 0xFFFFFFULL
main() {
unsigned long long a;
unsigned long b, c;
|
by: Lathe_Biosas |
last post by:
Hi
I would like to print on a win cmd console a register value, the value
is an "unsigned long" and have some output like these:
Register: 0x00000000
Tryed with
printf("Register: %#010lx \n", register);
|
by: Gregc. |
last post by:
Hi
Sorry to bother you gusy with such a basic question, but I am working
on a conversion program. I've got most of it down, but I am having
trouble with the output:
*Dollar Conversion Problem*/
/*--------------------------------------------------------------------------------------------------------------------*/
/*Head Files*/...
| |
by: pai |
last post by:
Hi ,
Can any one tell me how this statement of printf is behaving . how
the last digit is printed
int a=2,b=4,c=7;
printf("%d",printf("%d %d:",a,b));
//answer to this was 2 4:3
|
by: Debaser |
last post by:
I've recently read in one of my old C books that puts() is a better
function call with regard to performance than printf() in the
following situation:
puts("Some random text");
vs.
printf("Some random text\n");
|
by: Yevgen Muntyan |
last post by:
Hey,
It was mentioned elsewhere that printf("%d", INT_MAX);
is implementation-defined. Why? Is it because INT_MAX
value is implementation-defined so output depends
on implementation, or is it something more subtle so
that output of the following is also implementation-defined:
if (INT_MAX == 65535)
printf("%d", INT_MAX);
|
by: linq936 |
last post by:
Hi,
I read in many places that the string to be outputted by printf()
must be ending with newline, for example, it should be
printf("Hello World.\n");
instead of
printf("Hello World.");
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |