Page 1 of 1

C POINTER TUTORIAL PART 2 C POINTER TUTORIAL PART 2 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 23 June 2010 - 01:00 PM

C POINTER TUTORIAL PART 2


CONTENTS
• I. INTRODUCTION
• II. MULTIPLE INDIRECTION
• III. EXAMPLE PROGRAM
• IV. REFERENCES


I. INTRODUCTION

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

This tutorial will demonstrate the use of pointers to pointers.


II. MULTIPLE INDIRECTION

First a quick review:

When C passes arguments to functions it passes them by value; except for arrays, which are passed by reference. When you are passing a variable to a function you are passing a copy of the variable. When you are passing an array to a function you are passing a pointer to the first element of the array.

All functions have the following form:


type function name(arguments passed to the function)
variable declaration of the arguments
{
local variable declarations
statements
}



A. Call by value

The function works with a copy of the variable.

Changes made to the variable in the function will not effect the value of the variable in the calling function.


B. Call by reference

The address of the variable is passed to the function.

Changes made to the variable in the function will effect the value of the variable in the calling function.

A function can only have one return value. However, to modify the value of variables in a function, pass the addresses of those variables as function parameters. The reference which locates a variable address in memory can be obtained by preceding the identifier of the variable in the calling function arguments with an ampersand sign (&),"address of" reference operator.

A pointer is a special type of variable that holds the reference, the address, of the value of another variable. Pointers "point to" the variable whose reference they store. A variable points to a value. The value stored in the variable it points to, can be directly accessed by preceding the pointer's identifier with an asterisk (*),"value pointed by", dereference operator.

As shown in the first tutorial of this series, the following two dimensional character array:


char GRACE[4][11]= { "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy";



Can also be coded as:

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



Also, 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 type char *, you need a pointer to type char *. In other words, you need a type char **, a pointer to a pointer to a char, as follows:

	char** p = GRACE;



Lets distinguish between the pointer, and what it points to; and what the pointer that it points to, points to.

The pointer *p, points to *GRACE[0]; and the pointer *GRACE[0], points to "Genesis”. Therefore, **p is a pointer that points to a pointer that points to “Genesis”.

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.


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



After the first ++p in the “for loop iteration” shown above:

The pointer *p, points to *GRACE[1]; and the pointer *GRACE[1], points to "Exodus". Therefore, **p is now a pointer that points to a pointer that points to "Exodus".

After the second ++p “for loop iteration”:

The pointer *p, points to *GRACE[2]; and the pointer *GRACE[2], points to "Leviticus". Therefore, **p is now a pointer that points to a pointer that points to "Leviticus".

After the third ++p “for loop iteration”:

The pointer *p, points to *GRACE[3]; and the pointer *GRACE[3], points to "Numbers". Therefore, **p is now a pointer that points to a pointer that points to "Numbers".

After the fourth ++p “for loop iteration”:

The pointer *p, points to *GRACE[4]; and the pointer *GRACE[4], points to "Deuteronomy". Therefore, **p is now a pointer that points to a pointer that points to "Deuteronomy".

In general, the & operator adds asterisks, increasing pointer level. And, the *, ->, and []operators remove asterisks, decreasing pointer level.


III. EXAMPLE PROGRAM

/***************************************
  C POINTER TUTORIAL PART 2
***************************************/
#include<stdio.h>     //  standard I/O header file
#include<string.h>    //  string handling

int main(int argc, char* argv[])
{
	char* GRACE[] = { "Genesis", "Exodus", "Leviticus", "Numbers", 						"Deuteronomy" };

	char** p = GRACE;

	printf("\n\n              C POINTER TUTORIAL PART 2:\n\n\n");
	printf("\n\n  THE FIRST FIVE BOOKS OF THE BIBLE ARE:\n\n\n");
	for( unsigned n = 0; n < 5; ++n )
	{
		printf("  %s", *p );
		++p;
	}
	
	printf("\n\n\n\n\n\n\n\n  Please press any key to EXIT.");
	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? 1
  • +

Replies To: C POINTER TUTORIAL PART 2

#2 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

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

Posted 24 June 2010 - 11:15 AM

Hello Everyone! :wub:

I forgot to include Example 2, sorry I must be going blind! :oops:

/***************************************
  C POINTER TUTORIAL PART 2
  EXAMPLE 2
***************************************/
#include<stdio.h>     //  standard I/O header file
#include<string.h>    //  string handling

int main( int argc, char* argv[] )
{
		
        char* GRACE[] = { "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy" };

		/*

			GRACE[] is an array of size 5 of pointer to a char.

		*/
		
		char** p = GRACE;

		/*
			p stores the base address of an array of five pointers to a char   
			or char array.
		*/

        printf("\n\n   C POINTER TUTORIAL PART 2: EXAMPLE 2");
		printf("\n\n   THE FIRST FIVE BOOKS OF THE BIBLE ARE:\n\n");
                
        int i;  
		
		for( i = 0; i < 5; i++ )
		{
			printf("\n   p[%d] = %s\n", i, p[i]);
		}

		/*
			Now lets distinguish between the pointer, and what it points to;
			and what the pointer that it points to, points to.
			**p is a pointer to the first element of the array GRACE.
			GRACE[] is an array of size 5 of pointer to a char.
			After we assigned GRACE to **p, **p became a pointer that points to
			a pointer, *GRACE[0] that points to “Genesis”.
			The "for loop" uses the int i to iterate through
			GRACE[] the array of size 5 of pointer to a char.
			Therefore, on the first iteration **p is a pointer
			that points to a pointer
			that points to “Genesis”.
		*/

        printf( "\n\n\n  Please press any key to EXIT." );
        char c;
        c = getchar();

        return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1