-
Test Prep Problem Help
Hi guys, I was wondering if someone could help me understand this problem
1. What is the output?
Code:
void question(void)
{
char str[] = "Coaxial flutter";
char *p1, *p2;
short *p3 = str;
p1 = &str[7 % 8];
p2 = str + 3;
printf("1) [%c]\n", *(p3 + 4));
printf("2) [%s]\n", &p2[5]);
printf("3) [%c]\n", *p1++);
printf("4) [%c]\n", (*p1)++);
p2[6] += 6;p2[8] -= 6;
printf("5) [%s]\n", (char *)(p3 + 4));
}
-
I'm assuming that you are not currently taking an exam, so why don't you try it?
-
hello,
i tried solving this problem, but i am having a hard time understanding it.
help with this problem would be appreciated.
-
hello,
i tried solving this problem, but i am having a hard time understanding it.
help with this problem would be appreciated.
-
What I was asking was: Why don't you open your computer, type it into your favourite IDE, and then run it?
-
hello,
i have to understand the problem without running it in an IDE
-
I suggest:
- Examine the program line by line, while keeping a sheet of paper in front of you with the names and types of the variables that were declared in this function.
- Whenever the value of a variable changes, note down its new value. For the pointers, what you can do is to work with byte offsets from the address of the first element of str, e.g., the "value" of a pointer to the first element of str would be 0 (even though its actual value will be the address of the first element of str).
- When you reach the printf statements, make use of your chart of variable names/types/values to do the computations, then interpret what is to be printed according to the printf format specifier, writing this down.
- Compile and run the program to check.
- If you got something wrong, run the program through a debugger to see where you went wrong: the variable watch window of your debugger is effectively what you would have done with the chart of variable names/types/values.
- If after doing all this you still don't understand why you're wrong, post here with the details of all the previous steps.
-
You are currently not doing your exam, right?
...So run the programme and look carefully at what is happening.
I'm getting the feeling that you are currently doing a test and you are looking for someone to answer the question for you...
-
hello,
thank you for this, i will try this method
-
hello,
i'm not currently in a test
-
> 1. What is the output?
$ gcc -Wall bar.c
bar.c: In function ‘question’:
bar.c:7:15: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
short *p3 = str;
^
You can tell whoever set the question that the code is broken.
If str starts out at an odd address, then this is your future.
Bus error - Wikipedia
> printf("1) [%c]\n", *(p3 + 4)); // a short integer composed of "fl"
Another train wreck.
Assuming the dereferencing doesn't draw a bus error, you now have to contend with endianness.
On a little endian machine, you get "f"
On a big endian machine, you get "l"
That is, providing the implementation of %c just does modulo arithmetic on the value supplied to get one or other character.
If it range checks it properly, who knows.
> p2[6] += 6;p2[8] -= 6;
And if your character set isn't ASCII, then expect more fun and surprise.
That also applies to one of the attempts at ++ as well.
If this is emblematic of the standard of education you're receiving, then you're better off failing / dropping the course, and finding someone who knows what they're talking about.
Nobody in the real world writes such awful code and expects to get away with it. You're not learning how to program.
At best, you're learning a few parlour tricks.