Pointers and Arrays

mojeaux
mojeaux used Ask the Experts™
on
Hi - This is a continuation of the a previous post.   I'm not sure I have clear understanding of the Pointer and Array Concept which brought on another question about my code and the pointer to the array...

#include <stdio.h>  
#include <stdlib.h>  

/*GLOBAL DECLARATIONS*/

int main(void)
{
/*FUNCTION DECLARATIONS*/
void get_randno(void);
/*LOCAL DECLARATIONS*/
/*STATEMENTS*/

  get_randno();
   
  return(0);
}

/*~~~~~~~~~~~~~~~~~  get_randno Function ~~~~~~~~~~~~~~~
Generates random number
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void get_randno(void)
{
/*FUNCTION DECLARATIONS*/
  void tableFmt(int *pTab, int count);
  void sigChk(int *pAry);
 
/* LOCAL DECLARATIONS */
  int count, i;
  int randmArray[100];
  char resp1;
 
/* STATEMENTS */
for(count=0;count<100;count++)
   {
    randmArray [count] = rand() % 1000;
      i = randmArray [count];            
      sigChk(&i);
      printf ("Your array element is %d.  Your random number is %d\n", count, i);
   }
      printf("You now have 100 random numbers in your array!\n\n");
      printf("Would you like to display your array in table format?\n");
                 printf("Please enter 'Y' for Yes and 'N' for No.\n\n");
                 scanf("%c", &resp1);
      if (resp1 == 'Y')
      {
            tableFmt(randmArray, count);      
            system("pause");
      }
      else if (resp1 == 'N')
      {
            printf("Sorry you do not wish to continue.  Program will end now.     Thank you.\n\n");
            system("pause");
            return;
      }
      else
      {
            printf("Invalid response - unable to continue with table format.  Thank you.\n");
            system("pause");
            return;
    }  
    return;
}/* counter loop*/

/*~~~~~~~~~~~~~~~~~  sigChk Function ~~~~~~~~~~~~~~~
Determines if random number evenly divisable by 3 or 7
and stores the random number as a negative number.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void sigChk(int *pAry)
{
/* LOCAL DECLARATIONS */
/* STATEMENTS */
if ((*pAry % 3 == 0) || (*pAry % 7 == 0))
      *pAry = -1 * *pAry;
else
    *pAry = 1 * *pAry;

return;
}/*changes sign on random number*/

/*~~~~~~~~~~~~~~~~~  tableFmt Function ~~~~~~~~~~~~~~~
Places random numbers in a 10 x 10 table.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void tableFmt(int *pTab, int count)
{
/*FUNCTION DECLARATIONS*/
  void evenCt(int *pEven, int count);
  void sumTot(int *pSum, int count);

/* LOCAL DECLARATIONS */
int j, rvalue;  
int tableCount;
int tableSize;
int *rValue;
      
/* STATEMENTS */
tableCount = 0;
tableSize = 100;
for (j = 0; j < tableSize; j++)
{
    printf("%5d", *(pTab+j));
      if (tableCount < 9)
      {
            tableCount ++;
      }
      else
      {
            printf("\n");
            tableCount = 0;
      }
      
}
system("pause");
return;
}/*creates table of random numbers*/

/*~~~~~~~~~~~~~~~~~  evenCt Function ~~~~~~~~~~~~~~~
Counts even random numbers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void evenCt(int *pEven, int count)
{
/* LOCAL DECLARATIONS */
int eCount;
int evenNo;
      
/* STATEMENTS */

eCount = 0;
evenNo = 0;

evenNo = count;
while (evenNo <= 100)
{
      if (*pEven % 2 == 0)
      {
            eCount = eCount + 1;
      }
}
printf("There are %d even numbers in the array.\n\n", eCount);
return;
}/*counts even random numbers*/

/*~~~~~~~~~~~~~~~~~  sumTot Function ~~~~~~~~~~~~~~~
Sums all values in the array
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void sumTot(int *pSum, int count)
{

/* LOCAL DECLARATIONS */
int eSum;
int sumNo;
      
/* STATEMENTS */
eSum = 0;
sumNo = 0;

sumNo = count;
while (sumNo <= 100)
{
      eSum = eSum + *pSum;
}
printf("The sum total of all numbers in the array is: %d", eSum);
 return;
}/*sums all numbers in the array*/

Question:  Why doesnt *pAry save the negative values back to the orginal array?  When it displays, it shows a negative number, but when I call or use the array in a second function (tableFmt), it goes back to the orginal values?   I thought *pAry was changing the original array value.

I would appreciate any clarification you could give me.    Thanks, M
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Yes, you do NOT understand!

The lines below:
 i = randmArray [count];            
      sigChk(&i);

make a COPY (in variable i) of the CONTENTS of the array element...
The address of variable i is passed down, so the value of i is changed, NOT the contents of the array!
If you want to change the original array, you need to either set it to i or pass a pointer to the array...

Choice 1:

 i = randmArray [count];            
      sigChk(&i);
randmArray [count]=i

Choice 2:
sigChk(&randmArray [count]);

Note: Choice 1 puts the changed value of i back into the array, while Choice 2 eliminates the need for i by passing the array element address to the subroutine... However, your subsequent printf will need to be changed as well:

printf ("Your array element is %d.  Your random number is %d\n", count, randmArray [count]);
mojeauxBusiness User

Author

Commented:
Ok, that makes more sense.   I understand why choice 1 works and why choice 2 would be a better option.   Thanks gain for your help!
mojeauxBusiness User

Author

Commented:
Excellent explaination.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial