Home Contents


/*      Introduction to program:
        Program - point3.c.
        Example of the use of pointers and strings.
        Francis O'Donovan 19-1-98. */

/* Files to be included. */
 
#include <stdio.h>

/*      Function: main(). */

main()
{
        /* Initialize variables. */

        char lastname[10] = {'O',' ','D','o','n','o','v','a','n','\0'} ;
                /* A string (character array) containing
                   my last name. */
        char s[16], t[16];      /* Strings for user-input. */
        char *p;                /* Pointer to a char. */

        /* Print introduction on screen. */

        printf( "Program: point3.c.\n" );
        printf( "Example of the use of pointers and strings.\n" );
        printf( "Francis O'Donovan 19-1-98.\n\n" );

        /* Print lastname to screen using printf and puts. */
 
        printf( "** Using printf and puts **\n\n" );
        printf( "%s\n", lastname );
        puts( lastname );

        /* Ask user to input a two-part name
                and print it to the screen. */

        printf( "\n\n** Using scanf and gets **\n\n" );

        printf( "Please type a two-part name, for example, Donald Duck.\n" );
        printf( "No more than 15 characters including space, please.\n\n" );
        scanf( "%s", s );

        printf( "\nPlease type the name again.\n\n" );
        gets( t );

        printf( "\nThe string s: %s.\n", s );
        printf( "The string t: %s.\n\n", t );

        /* Using a pointer. */

        p = lastname;
        p++;
        p++;
 
        printf( "** Using a pointer **" );
        printf( "\nThe pointer p points to %c.", *p );

        printf( "\n\nThe pointer p originally pointed to the O in O Donovan.\n" );
        printf( "After two p++ statements, it pointed to the D, which was printed.\n" );

}


Home Contents



© Francis O'Donovan 1999.