Quick question (beginner)

This is a discussion on Quick question (beginner) within the C++ Programming forums, part of the General Programming Boards category; The following code fragment: Code: #include <iostream> #include <string> using namespace std; int main() { char input[300], *p, *q[300], **r ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    San Juan, PR
    Posts
    10

    Quick question (beginner)

    The following code fragment:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() 
    {
    char input[300], *p, *q[300], **r = q;
    
     cin.getline(input, 300);
    
     for (p = input; *p; p++)
       switch (tolower(*p)) {
       case 'e':
         *r++ = p;
         break;
       case '.':
       case '\'':
       case ' ':
         *p = '\0';
         break; }
     *r = 0;
    
     for (r = q; *r; r++)
       cout << *r << " ";
    
     cout << endl;
    }
    was given as an example exam for my introduction programming course. What i don't understand is the use of the *p and *r as the middle argument in the for loops. I've only used inequalities for that location, so i have no idea what that means for pointers. Any help is appreciated.
    Last edited by JMaxwell; 05-23-2013 at 02:02 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    You need to indent the code properly to see more clearly what it is doing. Also, just post the code as-is within the code tags; don't try to apply any formatting as the forum software will apply it for you.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    San Juan, PR
    Posts
    10
    Quote Originally Posted by laserlight View Post
    You need to indent the code properly to see more clearly what it is doing. Also, just post the code as-is within the code tags; don't try to apply any formatting as the forum software will apply it for you.
    ok, done.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by JMaxwell View Post
    What i don't understand is the use of the *p and *r as the middle argument in the for loops.
    *p and *r dereference the pointers thus getting the value of a character.
    C interprets any integral value 0 as false. Therfore the loop ends when the '\0' character is found and that is the end of string marker.
    Kurt

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    San Juan, PR
    Posts
    10
    Quote Originally Posted by ZuK View Post
    *p and *r dereference the pointers thus getting the value of a character.
    C interprets any integral value 0 as false. Therfore the loop ends when the '\0' character is found and that is the end of string marker.
    Kurt
    Thanks, i didn't realize that the null character was taken as false when used as a boolean expression.
    One last question though:
    I've seen *r=*p and r=p and understand the difference but what does the expression **r=q mean in this case?
    Last edited by JMaxwell; 05-23-2013 at 02:30 PM.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by JMaxwell View Post
    what does the expression **r=q mean in this case?
    This is an initialisation and initializes the pointer to pointer of char r with the value q and that is an array of 300 pointers to char and is compatible with a pointer to pointer to char.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question.
    By rosemary in forum C Programming
    Replies: 49
    Last Post: 09-13-2012, 07:31 PM
  2. Replies: 16
    Last Post: 06-16-2011, 04:39 PM
  3. beginner question
    By manxmog in forum C Programming
    Replies: 3
    Last Post: 05-08-2009, 11:02 PM
  4. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  5. GCC Beginner - Quick Question
    By samGwilliam in forum C Programming
    Replies: 5
    Last Post: 02-25-2006, 10:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21