Thread: Test Prep Problem Help

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    6

    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));
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I'm assuming that you are not currently taking an exam, so why don't you try it?

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    6
    hello,

    i tried solving this problem, but i am having a hard time understanding it.

    help with this problem would be appreciated.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    6
    hello,

    i tried solving this problem, but i am having a hard time understanding it.

    help with this problem would be appreciated.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What I was asking was: Why don't you open your computer, type it into your favourite IDE, and then run it?
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    6
    hello,

    i have to understand the problem without running it in an IDE

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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...
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    6
    hello,

    thank you for this, i will try this method

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    6
    hello,

    i'm not currently in a test

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Test condition problem,Please help!!!
    By Shinzu911 in forum C Programming
    Replies: 9
    Last Post: 03-10-2013, 09:16 PM
  2. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  3. Problem: test if solution is integer
    By Kappie in forum C++ Programming
    Replies: 23
    Last Post: 05-01-2012, 12:00 AM
  4. Math Test Problem Disagreement
    By orbitz in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 02-28-2003, 04:27 PM
  5. some AP test prep help
    By kv2 in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2002, 10:34 AM

Tags for this Thread