使用指针和 strcmp 比较两个字符串

comparing two string using a pointer and strcmp

我正在尝试解决这个问题,但它不起作用我认为 'if' 中的语句是错误的,我不知道我是否可以像这样在 strcmp 中放置一个指针!!

#include <string.h>
#include <studio.h>

struct PersonCar {
  char pname[20];
  char cModel[20];
  float price;
};
struct PersonCar pc[4];

float calculatePrice(struct PersonCar *p, char *name) {
  p = malloc(sizeof(pc));
  float s = 0;
  for (int i = 0; i < 4; i++) {
    if ((strcmp((p[i].pname), name)) == 0)  //(p+i)->pname
      s += (p + i)->price;                  //(p+i)->price; }
  return s;
}

int main() {
  // entering the element of the array from the user
  char A[20];
  printf("Enter a name : ");
  fgets(A, sizeof(A), stdin);
  printf("the total price of the registered cars for %s =%f\n", A,
         calculatePrice(&pc, A));
}

我想你正在寻找类似的东西:

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

struct PersonCar {
    char pname[20];
    char cModel[20];
    float price;
};

struct PersonCar pc[4] = {
    {"abc", "", 1.0},
    {"def", "", 2.0},
    {"abc", "", 3.0},
    {"jkl", "", 4.0},
};

float
calculatePrice(struct PersonCar *p, char *name)
{
    float s = 0;
    for( struct PersonCar *e = p + 4; p < e; p++ ){
        if( strcmp((p->pname), name) == 0 ){
            s += p->price;
        }
    }
    return s;
}

int
main(void)
{
    char A[20];
    printf("Enter a name : ");
    fgets(A, sizeof(A), stdin);
    printf("the total price of the registered cars for %s =%f\n", A,
        calculatePrice(pc, A));
}

这里一个明显的问题是您没有处理输入中的换行符,但由于我不知道您实际上是如何初始化数据的,因此不清楚您要如何处理它。

  • 首先你需要一些数据(我在我的版本中包含了一些静态初始化的数据)

  • 其次,需要去掉函数开头的malloc()语句,修改传入的数据指针,使其指向一个未初始化的数据,即找到任何匹配的寄存器的可能性很小。

  • 你最好知道数据数组的大小,并传递其中的条目数。

  • 您需要从fgets()读取的数组中删除最后一个'\n'。如果不这样做,它会将 "smith""smith\n" 进行比较,这永远不会相等。我在下面建议一种方法,但你必须小心并阅读 strtok 的手册页,因为它修改了源字符串,这可能不是你想要的(而在这种情况下正是我们想要的)

为了说明,我已经编写了这个示例代码(但是您可以编译和执行的完整程序)来查看逻辑。

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

struct PersonCar {
  char *pname;
  char *cModel;
  float price;
};

/* initialized data */
struct PersonCar pc[] = {
    { .pname = "smith",
      .cModel = "foo",
      .price = 10.50F,
    }, {
      .pname = "montgomery",
      .cModel = "bar",
      .price = 20.50F,
    }, {
      .pname = "mckormack",
      .cModel = "blah",
      .price = 35.50F,
    }, {
      .pname = "smith",
      .cModel = "pong",
      .price = 55.50F,
    }, {
      .pname = "phelps",
      .cModel = "ping",
      .price = 75.50F,
    },
};

size_t pc_count = sizeof pc / sizeof pc[0];

float calculatePrice(
        struct PersonCar *p,
        size_t p_count,
        char *name)
{
  float total = 0.0;
  for (int i = 0; i < p_count; i++) {
    if (strcmp(p[i].pname, name) == 0)
        total += p[i].price; /* found */
  }
  /* not found */
  return total;
}

int main()
{
  // entering the element of the array from the user
  char A[80];

  printf("Enter a name : ");
  fgets(A, sizeof(A), stdin);
  char *p = strtok(A, "\n"); /* chop the last \n if present */
  printf("the total price of the registered cars for %s =%f\n",
         p, calculatePrice(pc, pc_count, p));
}