Thread: Pointer to Array Example

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    8

    Pointer to Array Example

    in the example that i took from the deitels book, i could not understand why do we print the sentence[1] can you help me about that dont we have to use sentence[0] instead of sentence[0]?
    thanks in advance.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    {
      /* initialize 4 arrays of char pointers */
      char *article[] = { "the", "a", "one", "some", "any" };
      char *noun[] = { "boy", "girl", "dog", "town", "car" };
      char *verb[] = { "drove", "jumped", "ran", "walked", "skipped" };
      char *preposition[] = { "to", "from", "over", "under", "on" };
      char sentence[100] = "";      /* completed sentence */
    
      srand(time(NULL));
    
      /* create 20 sentences */
      for (int i = 1; i <= 20; i++) {
    
        /* randomly choose pieces of sentence */
        strcat(sentence, article[rand() % 5]);
        strcat(sentence, " ");
    
        strcat(sentence, noun[rand() % 5]);
        strcat(sentence, " ");
    
        strcat(sentence, verb[rand() % 5]);
        strcat(sentence, " ");
        strcat(sentence, preposition[rand() % 5]);
        strcat(sentence, " ");
    
        strcat(sentence, article[rand() % 5]);
        strcat(sentence, " ");
    
        strcat(sentence, noun[rand() % 5]);
    
        /* capitalize first letter and print sentence */
        putchar(toupper(sentence[0]));
        printf("%s.\n", &sentence[1]);
        sentence[0] = '\0';
      }                             /* end for */
    
      return 0;                     /* indicate successful termination */
    }
    Last edited by Salem; 11-04-2020 at 11:30 PM. Reason: Removed crayola - use copy/paste as text

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice the putchar before that, i.e., the first character is printed separately after being transformed to uppercase.
    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

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Notice the putchar before that, i.e., the first character is printed separately after being transformed to uppercase.
    everything is just clear now thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-05-2018, 12:14 AM
  2. Passing 2D array to function via pointer of pointer
    By sean_cantab in forum C Programming
    Replies: 4
    Last Post: 05-09-2016, 10:15 AM
  3. Replies: 1
    Last Post: 07-16-2013, 07:21 AM
  4. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  5. Replies: 1
    Last Post: 03-24-2008, 10:16 AM

Tags for this Thread