Thursday, September 22, 2011

C A POINTER TO A POINTER TO A CHAR









REVISED: Sunday, March 15, 2015




CONTENTS:
I.   C A POINTER TO A POINTER TO A CHAR INTRODUCTION
II.  C A POINTER TO A POINTER TO A CHAR
III. C A POINTER TO A POINTER TO A CHAR EXAMPLE PROGRAM SOURCE CODE
IV. C A POINTER TO A POINTER TO A CHAR REFERENCES

YOU WILL LEARN:
1.  C pointers.
2.  C pointer to a pointer to a char.

I. C A POINTER TO A POINTER TO A CHAR INTRODUCTION


Welcome to the “C a Pointer to a Pointer to a Char Tutorial.”

A pointer is an address in memory.


To understand pointers you need to understand the two-part nature of a pointer. For example, as its first part, a pointer is a variable that contains the memory location address of another variable. And, as its second part, that memory location address points to the value of that other variable. This works because the computer is always “thinking” of memory in terms of addresses and values at those addresses.


Each example in this tutorial series is a complete program which you can cut and paste; and build and debug using your C compiler.

II. C A POINTER TO A POINTER TO A CHAR


The following example narratives explains a pointer to a pointer to a char.

#include<stdio.h>
//  standard I/O header file
#include<string.h>
//  string handling

int main(int argc, char* argv[])
{


A string in C is an array of characters, with the last character being a '\0'. When double quotes are used, as follows, the nul character ('\0') escape sequence is automatically appended to the end of each string. For example “Genesis” becomes Genesis\0 because by definition a string in C must be nul terminated.

char* GRACE[] = { "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy" };

printf("\n\n");


Each pointer in the array was initialized with the address of an array element, a string literal.


With an array of pointers of type char, each array element points to an independent string, and, as shown above, the lengths of each of the strings can be different.


As we proceed you will see GRACE [ i ] is defined using pointer arithmetic.

GRACE [ i ] == * (GRACE + i )


To point to an element of an array of elements, you need a pointer to the element type. To point to an element of an array of char *, you need a pointer to char *. In other words, you need a char **, a pointer to a pointer to a char, as follows:

char** p = GRACE;


When increment or decrement operators appear before an operand, the unary operators “++ and --“ are called "prefix" increment or decrement operators. The operand is incremented or decremented by the integer value 1, and its new value is the result of the expression. The type of the result is the same as the operand type. For example, an operand of pointer type is incremented or decremented by the size of the object it addresses. An incremented pointer, points to the next object; a decremented pointer, points to the previous object.

The prefix operator is evaluated before the assignment; and the postfix operator is evaluated after the assignment.

As shown below, the prefix operators ++ which appear before the operand n; i.e., ++n, are used to increment the operand by the integer value 1, and its new value is the result of the expression.

for( unsigned n = 0; n < 5; ++n )
{
    printf("  %s", *p );


As shown below, the prefix operators ++ which appear before the pointer p; i.e., ++p, are used to point to the next element in the array, resulting in the printing of each character string literal in the array, in array element sequence, to the screen. The compiler uses the size of the data type, which it knows from the pointer's type. This works because all pointers of a given type, have the same memory address size.

        ++p;
    }

    char c;

    c = getchar();

    return 0;
}

III. A POINTER TO A POINTER TO A CHAR EXAMPLE PROGRAM SOURCE CODE

//***********************************
//C A POINTER TO A POINTER
//TO A CHAR
//***********************************
#include<conio.h>
//  contains function prototypes
#include<stdio.h>
//  standard I/O header file
#include<string.h>
//  string handling
#include<windows.h>
//  keybd_event()
/**********************************/
int main( int argc, char* argv[] )
{
    char* GRACE[] = { "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy" };

    printf("\n\n  THE FIRST FIVE BOOKS OF THE BIBLE ARE:\n\n" );

    char** p = GRACE;

    for( unsigned n = 0; n < 5; ++n )
    {
        printf( "  %s", *p );

        ++p;
    }
    printf("\n\n\n\n  Please press any key to continue." );

    printf("\n\n  " );

    char c;

    c = getchar();

    return 0;
}

IV. C A POINTER TO A POINTER TO A CHAR REFERENCES


The C Programming Language by Brian Kernighan and Dennis Ritchie (Englewood Cliffs, N.J.: Prentice-Hall, 1978).


C++: The Complete Reference, Fourth Edition by Herbert Schildt (Berkeley, California: McGraw-Hill/Osborne, 2003).

YOU HAVE LEARNED:
1.  C pointers.
2.  C pointer to a pointer to a char.

Elcric Otto Circle





-->

-->

-->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment