向struct添加值,在C中添加错误

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (68)

我是编程的新手,所以我正在尝试编写一个小程序,我可以显示汽车信息,还可以将汽车添加到我的“库”中,现在我出来了1.Show汽车看起来像这样:

  ID          BRAND         PICS 
  bbb188     BMW    1   2   3
  AAA-999     VOLVO    4   5   6
  CCC-999     CITROEN    1   2   3

但在我添加新车后,PICS没有显示。所以,如果我要添加AAA-111 VOLVO 1.这是结果:

 bbb188     BMW    1   2   3
 AAA-999     VOLVO    4   5   6
 CCC-999     CITROEN    1   2   3
 AAA-111     VOLVO    -398253632   3   3

我只是得到照片的随机数字,总是3个值。任何人都可以帮我这个,请告诉我如何做到这一点。

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

#define MAX 1000
#define IDSIZE 20
#define BRANDSIZE 50
#define PICSIZE 10

typedef struct{
  char id[IDSIZE+1];
  char brand[BRANDSIZE+1];
  int *pic;
} Car;


void printCar(Car *pCar,int imagecount)
{
  printf(" %s ",pCar->id);
  printf("    %s ",pCar->brand);
  for(int i=0; i<imagecount; i++){
    printf("   %d",pCar->pic[i]);
  }
  printf("\n");
}

Car initCar(char itsId[],char itsBrand[],int *itsPic, int imagecount)
{
  Car newCar;
  strcpy(newCar.id, itsId);
  strcpy(newCar.brand, itsBrand);
  newCar.pic = itsPic;

  return newCar;
}

void PrintList(Car aLista[],int imagecount, int carcount)
{
  for(int i = 0; i<imagecount; i++)
    printCar(&aLista[i],carcount);
}

void AddCar(Car aList[], int *pAt, Car theCar) 
{
  aList[(*pAt)++]=theCar;    
}

Car NewCar(Car minapatienter[], int patientCount)
{
  Car newCar;

  gets(newCar.id);
  printf("type in  ID \n"); 
  gets(newCar.id); 
  printf("type in brand\n");
  gets(newCar.brand);

  bool imageInputDone = false; 
  int imageCount=0;
  while(imageInputDone == false)
  {
    printf("type in image reference \n");
    int newImage; 
    scanf("%d",&newImage);

    newCar.pic = &newImage; 
    imageCount++;
    printf("vill du \n1.Add another image reference \n2.exit\n");
    int input;
    scanf("%d", &input);
    printf("input: %i\n",input);
    switch(input)
    {
        case 1: 
            printf("Adding one more image\n");
            break;
        case 2: 
            printf("Leaving loop\n");
            imageInputDone = true; 
            break;
        default:
            while (input<1 || input<2)
              ;
            printf("Input correct number\n");
            break;
    }

    return newCar; 
  }
}

int main(void)
{
  int carCount=0;
  int imagecount=0;
  Car myCar[MAX]; 
  int input;

  int test[3]={1,2,3};
  int test2[3]={4,5,6};

  myCar[0]= initCar("bbb188","BMW", test, 3);
  myCar[1] = initCar("AAA-999","VOLVO", test2, 3);
  myCar[2] = initCar("CCC-999", "CITROEN", test,3);

  carCount=3;
  imagecount=3;

  do {
    printf("1. Show cars \n2. Add car \n");
    scanf("%d",&input);
    switch(input)
    {
      case 1:
        printf("ID          BRAND         PICS \n");
        PrintList(myCar,carCount, imagecount);
        break; 
      case 2: 
        AddCar(myCar,&carCount,NewCar(myCar,carCount));
        printf("ID          BRAND         PICS \n");
        PrintList(myCar,carCount, imagecount);
    }    //break; 
  } while (input < '1'|| input < '2');

  return 0;
}
提问于
用户回答回答于

您可以获得为每辆车打印的相同数量的图像,因为您只有一个全局计数器。每个图像需要一个计数器:

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

#define MAX       1000
#define IDSIZE    20
#define BRANDSIZE 50
#define PICSIZE   10

typedef struct Car
{
  char  id[IDSIZE+1];
  char  brand[BRANDSIZE+1];
  int  *pic;
  int   imagecount;
} Car;

通过此更改,无需传递打印计数:

void printCar(Car *pCar)
{
  printf(" %s ", pCar->id);
  printf("    %s ", pCar->brand);
  for(int i=0; i<pCar->imagecount; i++)
  {
    printf("   %d",pCar->pic[i]);
  }
  printf("\n");
}

计数器需要在初始化期间存储:

Car initCar(char itsId[], char itsBrand[], int *itsPic, int imagecount)
{
  Car newCar;
  strcpy(newCar.id, itsId);
  strcpy(newCar.brand, itsBrand);
  newCar.pic = itsPic;
  newCar.imagecount = imagecount;

  return newCar;
}

打印列表时,会混淆图像数量和汽车数量:

void PrintList(Car aLista[], int imagecount, int carcount)
{
  for(int i = 0; i<imagecount; i++)
    printCar(&aLista[i],carcount);
}

这必须是:

void PrintList(Car aLista[], int carcount)
{
  for (int i = 0; i < carcount; i++)
    printCar(&aLista[i]);
}

将汽车添加到您的阵列基本上没问题,但您可能会检查您是否到达MAX汽车。

void AddCar(Car aList[], int *pAt, Car theCar) 
{
  aList[(*pAt)++]=theCar;    
}

现在最大的问题。这个函数存在内存使用和怪异循环的问题。

Car NewCar(void)
{
  Car newCar = {0};  // initialze with empty strings and NULL pointers.

  // TODO: Replace gets with fgets!
  // gets(newCar.id); // WHY read before you prompt??
  printf("type in  ID \n"); 
  gets(newCar.id); 
  printf("type in brand\n");
  gets(newCar.brand);

  bool imageInputDone = false; 
  int imageCount=0;

  while(imageInputDone == false)
  {
    printf("type in image reference \n");
    int newImage; 
    scanf("%d",&newImage);

    imageCount++;
    int *newpics = realloc(newCar.pic, (imageCount) * sizeof(int));
    newpics[imageCount-1] = newImage; 
    newCar.pic = newpics;
    // TODO: Check for NULL

    printf("vill du \n1.Add another image reference \n2.exit\n");
    int input;
    scanf("%d", &input);
    printf("input: %i\n",input);
    while (input < 1 || input > 2)
    switch(input)
    {
        case 1: 
            printf("Adding one more image\n");
            break;
        case 2: 
            printf("Leaving loop\n");
            imageInputDone = true; 
            break;
        default:
            printf("Input correct number\n");
            break;
    }

    newCar.imagecount = imageCount;
    return newCar; 
  }
}

最后......

int main(void)
{
  int carCount=0;
  Car myCar[MAX]; 
  int input;

  int test[3]  = {1,2,3};
  int test2[3] = {4,5,6};

  myCar[0] = initCar("bbb188", "BMW", test, 3);
  myCar[1] = initCar("AAA-999", "VOLVO", test2, 3);
  myCar[2] = initCar("CCC-999", "CITROEN", test, 3);

  carCount=3;

  do
  {
    printf("1. Show cars \n2. Add car \n");
    scanf("%d", &input);

    switch(input)
    {
      case 1:
        printf("ID          BRAND         PICS \n");
        PrintList(myCar, carCount);
        break; 
      case 2: 
        AddCar(myCar, &carCount, NewCar());
        printf("ID          BRAND         PICS \n");
        PrintList(myCar, carCount);
    }    //break; 
  } while (input < 1 || input > 2); // compare as integers, not characters. Don't use < operator

  return 0;
}
用户回答回答于

你的NewCar功能有一些问题。的newImage是在堆栈存储器。当您执行赋值时newCar.pic = &newImage;newCar.pic将指向未定义的内存区域,因为newImage它超出了其范围。更好的方法,我们只使用它的值,不要在这里使用地址运算符。还有一件事,newCar.pic是一个指针(int数组)。所以你需要在使用前分配它。添加更多图像项时,需要重新分配它。并初始化pic为NULL指针。

这是我修改你的NewCar功能:

Car NewCar(Car minapatienter[], int patientCount)
{
    Car newCar;

    gets(newCar.id);
    printf("type in  ID \n"); 
    gets(newCar.id); 
    printf("type in brand\n");
    gets(newCar.brand);
    newCar.pic = NULL;

    bool imageInputDone = false; 
    int imageCount=0;
    while(imageInputDone == false)
    {
      printf("type in image reference \n");
      int newImage; 
      scanf("%d",&newImage);

      // Rellocation
      newCar.pic = realloc(newCar.pic, (imageCount+1)*sizeof(int));
      newCar.pic[imageCount] = newImage; 
      imageCount++;

      printf("vill du \n1.Add another image reference \n2.exit\n");
      int input;
      scanf("%d", &input);
      printf("input: %i\n",input);
      switch(input)
      {
          case 1: 
              printf("Adding one more image\n");
              break;
          case 2: 
              printf("Leaving loop\n");
              imageInputDone = true; 
              break;
          default:
              while (input<1 || input<2)
                ;
              printf("Input correct number\n");
              break;
      }

      return newCar; 
    }
}

扫码关注云+社区

领取腾讯云代金券