Page 1 of 1

C POINTER TUTORIAL PART 1 C POINTER TUTORIAL PART 1 Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 14 June 2010 - 12:12 PM

C POINTER TUTORIAL

PART 1


CONTENTS
• I. INTRODUCTION
• II. A POINTER TO A POINTER TO A CHAR
• III. EXAMPLE PROGRAM
• IV. REFERENCES


I. INTRODUCTION

Hello; nice to meet you. Welcome to the “C Pointer Tutorial Part 1.”

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. 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. EXAMPLE PROGRAM

//*******************************************
//  C POINTER TUTORIAL PART 1
//*******************************************  
#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. 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).


Is This A Good Question/Topic? 2
  • +

Page 1 of 1