0

这是我当前的代码:

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

int main()
{
    int target;
    char array[] = {'AC', 'EE', '88', '0D', '87'};

    printf("Enter the target byte as a hex value: \n");
    scanf("%x", &target);

    int i = 0;
    for (i; i < 5; i++)
    {
        printf("Value of array[%d] is %x \n", i, array[i]);
    }

    int i = 0;
    for (i; i < 5; i++)
    {
        if (memcmp(target, array, sizeof(target) == 0))
        {
            printf("Found a match!");
        }
    }
}

(1) 我希望能够在标准输出中显示我的 char 数组 [] 中的确切值。我试过的东西不起作用:将数组显示为%c, %x, %d.
(2) 从用户那里得到输入后,我希望能够将该输入与数组内的字符进行比较——认为 memcmp 将是可行的方法,但我如何遍历整个数组?我尝试这样做,if (memcmp(target, array[i], sizeof(target) == 0))但由于该array[i]部分而出现运行时错误,但如果我不添加该部分,它将如何通过整个数组将每个数组内存位置中存储的值与target变量中存储的值进行比较? 我基本上想将每个数组位置内的字节与用户输入的字节进行比较。

4

2 回答 2

1
  1. 您应该将十六进制值存储在数组中0xAC,而不是'AC'(这是一个多字符常量,根本不是您想要的)。
  2. 存储您的十六进制值,unsigned char因为在char默认情况下signed您将获得一些值(负值)被转换为int并相应地填充符号位,然后显示。
  3. memcmp的电话是错误的,无论如何都没有必要使用它。
  4. 您的示例代码将无法编译,因为您int i在同一范围内有两个声明。如果你不使用 C89,为什么不写你的 for 循环for(int i = 0;...)呢?
  5. 不要忘记 main 的返回值。

例子:

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

int main(void)
{
  int target;
  const unsigned char array[] = { 0xAC, 0xEE, 0x88, 0x0D, 0x87 };
  const int len = sizeof array;

  printf("Enter the target byte as a hex value: \n");
  scanf("%x", &target);

  for (int i = 0; i < 5; ++i)
  {
    printf("Value of array[%d] is 0x%x\n", i, array[i]);
  }

  for (int i = 0; i < len; ++i)
  {
    if (target == array[i])
    {
      printf("Found a match!\n");
    }
  }

  return EXIT_SUCCESS;
}
于 2013-10-09T22:28:09.930 回答
1

这个问题是一个更大计划的一部分,但我一开始就想不通这只是一小部分。最后,我把一切都搞定了。下面是实际程序的最终代码,底部是我在几个用户的帮助下编写的函数定义。再次谢谢你!

#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;
}
于 2013-10-09T22:50:03.823 回答