P. 1
Data Structures c

Data Structures c

|Views: 10|Likes:
Published by Vel Mani

More info:

Published by: Vel Mani on Apr 26, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Define two integer arrays, each 10 elements long, called array1 and
array2. Using a loop, put some kind of nonsense data in each and add
them term for term into another 10 element array named arrays. Finally,
print all results in a table with an index number, for example


2 + 10 = 12


4 + 20 = 24


6 + 30 = 36 etc.

Hint: the print statement will be similar to this:

printf("%4d %4d + %4d = %4d\n", index, array1[index],
array2[index], arrays[index]);

Module 815

Data Structures Using C

Page 813-12

Objective 3After working through this module you should be able to create
manipulate and manage C pointers to data elements.


Simply stated, a pointer is an address. Instead of being a variable, it is a
pointer to a variable stored somewhere in the address space of the
program. It is always best to use an example, so examine the next
program [POINTER.C] which has some pointers in it.

#include "stdio.h"
/* illustration of pointer use


void main( )
int index, *pt1, *pt2;
index = 39;

/* any numerical value


pt1 = &index;

/* the address of index


pt2 = pt1;
printf("The value is %d %d %d\n", index, *pt1, *pt2);
*pt1 = 13;

/* this changes the value of index */

printf("The value is %d %d %d\n", index, *pt1, *pt2);


Ignore for the moment the data declaration statement where we define
index and two other fields beginning with an star. (It is properly called
an asterisk, but for reasons we will see later, let’s agree to call it a star.)
If you observe the first statement, it should be clear that we assign the
value of 39 to the variable index; this is no surprise. The next statement,
however, says to assign to pt1 a strange looking value, namely the
variable index with an ampersand (&) in front of it. In this example, pt1
and pt2 are pointers, and the variable index is a simple variable.

Now we have a problem. We need to learn how to use pointers in a
program, but to do so requires that first we define the means of using
the pointers in the program!

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->