这个问题是一个更大计划的一部分,但我一开始就想不通这只是一小部分。最后,我把一切都搞定了。下面是实际程序的最终代码,底部是我在几个用户的帮助下编写的函数定义。再次谢谢你!
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
// defined constants
#define MAX_WIDTH 20
#define NUMELEMS 50
// typedefs and function prototypes
typedef unsigned char Byte;
void DispBytes(void *voidArray, int totalBytes);
int CountMatchBytes(void *voidArray, int totalBytes, int targetHex);
// ==== main ==================================================================
//
// ============================================================================
int main(void)
{
auto double myDoubles[NUMELEMS];
auto int myInts[NUMELEMS];
auto char myChars[NUMELEMS];
auto int target;
auto int numMatches;
// process the char array
puts("Here's the array of chars as bytes: ");
DispBytes(myChars, sizeof(myChars));
printf("\nEnter the target byte as a hex value: ");
scanf("%x", &target);
numMatches = CountMatchBytes(myChars, sizeof(myChars), target);
printf("There %s %d matching byte%s.\n\n", (1 == numMatches ? "is" : "are")
, numMatches
, (1 == numMatches ? "" : "s"));
// process the int array
puts("\nHere's the array of ints as bytes: ");
DispBytes(myInts, sizeof(myInts));
printf("Enter the target byte as a hex value: ");
scanf("%x", &target);
numMatches = CountMatchBytes(myInts, sizeof(myInts), target);
printf("There %s %d matching byte%s.\n\n", (1 == numMatches ? "is" : "are")
, numMatches
, (1 == numMatches ? "" : "s"));
// process the double array
puts("\nHere's the array of doubles as bytes: ");
DispBytes(myDoubles, sizeof(myDoubles));
printf("Enter the target byte as a hex value: ");
scanf("%x", &target);
numMatches = CountMatchBytes(myDoubles, sizeof(myDoubles), target);
printf("There %s %d matching byte%s.\n\n", (1 == numMatches ? "is" : "are")
, numMatches
, (1 == numMatches ? "" : "s"));
return 0;
} // end of "main"
void DispBytes(void *voidArray, int totalBytes)
{
// Sets startingPtr to the base address of the passed array
auto unsigned char *startingPtr = voidArray;
// Sets endingPtr to one address past the array
auto unsigned char *endPtr = voidArray + totalBytes;
auto int counter = 0;
// Loop while the address of startingPtr is less than endPtr
while (startingPtr < endPtr)
{
// Display the values inside the array
printf("%x ", *startingPtr);
counter++;
if (counter == MAX_WIDTH)
{
printf("\n");
counter = 0;
}
// Increment the address of startingPtr so it cycles through
// every value inside the array
startingPtr++;
}
}
int CountMatchBytes(void *voidArray, int totalBytes, int targetHex)
{
// Sets startingPtr to the base address of the passed array
auto unsigned char *startingPtr = voidArray;
// Sets endingPtr to one address past the array
auto unsigned char *endingPtr = voidArray + totalBytes;
auto int counter = 0;
// Loop while the address of startingPtr is less than endPtr
while (startingPtr < endingPtr)
{
// If the input value is the same as the value inside
// of that particular address inside the array,
// increment counter
if (targetHex == *startingPtr)
{
counter++;
}
// Increment the address of startingPtr so it cycles through
// every value inside the array
startingPtr++;
}
// Return the number of times the input value was found
// within the array
return counter;
}