Thread: Quick question

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    38

    Quick question

    I'm doing some exam revision and I'm stuck at this question. If some one could explain to me how this answer is obtained, it would be much appreciated:
    Code:
    char alpha[] = "abcdefghi";
    char ch[80], *pch = &ch[0];
    what does the following code print?

    Code:
    strcpy(ch, alpha);
    putchar(*pch++);
    putchar((*pch)++);
    putchar(*++pch);
    putchar(++(*pch));
    printf("\n%s", ch);
    ANSWER:

    Code:
    a
    b
    c
    d
    
    acddefghi

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Write out what you think each line does.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    38
    strcpy(ch, alpha);

    this line copies the string array alpha to array ch;
    Code:
    putchar(*pch++);
    prints the character at ch[0] which is a, and the pch is incremented by +1 and is now pointing to ch[1]
    Code:
    putchar((*pch)++)
    prints the character at ch[1] which is a, and the pch is incremented by +1 and is now pointing to ch[2]

    Its now at these lines that i get confused:

    Code:
    putchar(*++pch);
    this line prints c, but wouldn't it print d since ++ is a prefix. so if pch is pointing to ch[2], wouldnt it increment ch to ch[3] and then print d.

    From here I'm lost....

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by spikestar
    prints the character at ch[1] which is a, and the pch is incremented by +1 and is now pointing to ch[2]
    Notice that the ++ is applied to the result of (*pch), not to pch.
    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

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    38
    would any one be able to give me full explanation of the code and how it works? It's quiet frustrating me now ...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    putchar(*pch++);
    can be written as:
    Code:
    putchar(*pch);
    pch++;
    This:
    Code:
    putchar((*pch)++);
    can be written as:
    Code:
    putchar(*pch);
    (*pch)++;
    This:
    Code:
    putchar(*++pch);
    can be written as:
    Code:
    ++pch;
    putchar(*pch);
    This:
    Code:
    putchar(++(*pch));
    can be written as:
    Code:
    ++(*pch);
    putchar(*pch);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM