Thread: Help with a pointer exercise in the C programming language

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    Help with a pointer exercise in the C programming language

    I have the following exercise to do from Tim Ward's online C tutorial:

    Define a character array and use "strcpy" to copy a string into it. Print the string out by using
    a loop with a pointer to print out one character at a time. Initialize the pointer to the first element
    and use the double plus sign to increment the pointer. Use a separate integer variable to count
    the characters to print.

    I've been able to make a loop and have the pointer point to each character in the array using the loop variable as the index for the array and displaying them. Here is my code:

    Code:
    # include <stdio.h>
    
    main()
    {
           char x[13], *pt;
           int index;
    
        strcpy(x,"Hello world!");
    
              for(index=0;index<13;index++)
              {
                 pt = x + index;
                      printf("%c",*pt);
    
              }
    
    getch();
    
    }
    The exercise is asking me to actually increment the pointer each loop, which I'm guessing is increasing the memory address by 1, But I'm not too sure. Anyway, I've tried this code, which doesn't work.
    Code:
    #include <stdio.h>
    
    main()
    {
                char x[13], *pt;
                 int index;
    
          strcpy(x,"Hello world!");
    
               for(index=0;index<13;index++)
               {
                        pt = x;
                        printf("%c",*pt);
                             pt++;
    
                }
    
    getch();
    
    }
    Now that I look at this code for a few minutes, I realize that it doesn't make sense. I'm dumbfounded on how I would increment this pointer to display each character in my char array and printing them out. Perhaps I'm confused on what this exercise is asking me to do.

    Help would be appreciated.

    Thanks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try something like this...
    Code:
          strcpy(x,"Hello world!");
          pt = x;
    
         for(index=0;index<13;index++)
               {
                       printf("%c",*pt);
                             pt++;
    
                }
    With your pt=x inside the loop you are resetting it back to the beginning of the array every time through the loop... you obviously don't want to do that.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should also
    #include <string.h>
    to prototype strcpy properly.
    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. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  4. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM