User Tools

Site Tools


opus:fall2012:jcavalu3:datapart1

data Keyword 1

passing by address (function parameter passing)

Definition

Passing by address is just one of the ways that you pass a variable into a function. This is done not by passing the variable itself, but by passing the variable address. The only uses for this type of variable passing that I have encountered are arrays.

References

Phase 2

pointer assignment

Definition

The pointer assignment statement causes a pointer to become associated with a target or causes the pointer's association status to become disassociated or undefined. this means that a pointer or (*) can force a pointer or word or number to be paired with another or not depending on how it is used.

References

List any sites, books, or sources utilized when researching information on this topic. (Remove any filler text).

  • google.com

Demonstration

Demonstration of the indicated keyword.

The following is the demonstration of pointer assignment by using the code and the resulting output:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 int main()
  5 {
  6     char *array1, *array2;
  7     int i = 0;
  8 
  9     array1 = (char *)malloc( sizeof(char) * 4 );
 10     array2 = (char *)malloc( sizeof(char) * 4 );
 11 
 12     printf("Please input four values for the first array\n");
 13 
 14     for( i; i < 4; i++ )
 15     {
 16         scanf("%hhu", &*(array1 + i));
 17         *(array2 + i) = 0;
 18     }
 19 
 20     printf("Here are the values in both arrays:\nArray1: ");
 21 
 22     for( i = 0; i < 4; i++)
 23     {
 24         printf("%hhu ", *(array1 + i));
 25     }
 26     printf("\nArray2: ");
 27 
 28     for( i = 0; i < 4; i++)
 29     {
 30         printf("%hhu ", *(array2 + i));
 31     }
 32 
 33     array2 = array1; // Example of Pointer Assignment
 34 
 35     printf("\nHere are the results after setting 'array2' = to 'array1':\nArray1: ");
 36 
 37     for( i = 0; i < 4; i++)
 38     {
 39         printf("%hhu ", *(array1 + i));
 40     }
 41     printf("\nArray2: ");
 42 
 43     for( i = 0; i < 4; i++)
 44     {
 45         printf("%hhu ", *(array2 + i));
 46     }
 47 
 48     printf("\n\nThank you for your time.\n\n");
 49     return(0);
 50 }

The code makes two arrays, sets one equal to whatever input the user decides to go with, and the other being equal to 0. The program will then print the arrays, then set the second array equal to the first, which is the example of pointer assignment. The program is assigning the second array to the data in the first array. The result is then this:

lab46:~/src/opus/opus1$ ./pa
Please input four values for the first array
1 2 3 4
Here are the values in both arrays:
Array1: 1 2 3 4 
Array2: 0 0 0 0 
Here are the results after setting 'array2' = to 'array1':
Array1: 1 2 3 4 
Array2: 1 2 3 4 

Thank you for your time.

lab46:~/src/opus/opus1$ 
opus/fall2012/jcavalu3/datapart1.txt · Last modified: 2012/09/30 01:07 by jcavalu3