English 中文(简体)
1. 结构的缘由
原标题:Passing a string to a structure

我在一份档案中有一个汽车清单,其中线形形形色。

car_1_name [car_1_id] & car_2_name [car_2_id]

我想将每辆汽车的姓名和胎盘存放在一列。 为此,我设立了两个机构,称为“Car_”和“_Car_List_”。 然而,我为上述形式下限而写的“间隔”职能似乎并不恰当地摘取汽车名称(我用斜体分解)。 然而,当我用人工方式在名单上添加一辆汽车时,便能正确展示地毯。 下面你可以找到我的最低工作榜样:

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

struct _Car_
{
  char* name_;
  int id_;
  struct _Car_* parent_1_;
  struct _Car_* parent_2_;
};

struct _Car_List_
{
  struct _Car_* car_list[100];
  int number_of_cars;
};


struct _Car_* createCar(char* name, int id)
{
  struct _Car_* car = malloc(sizeof(struct _Car_));

  if (car == NULL)
    return NULL;

  car -> name_ = name;
  car -> id_ = id;

  return car;
}

void addCar(struct _Car_List_* my_list, struct _Car_* car)
{
  my_list -> car_list[my_list -> number_of_cars] = car;
  my_list -> number_of_cars = my_list -> number_of_cars + 1;

}

int parse(struct _Car_List_* my_list)
{
  char string[] = "Ferrari [3] & Porsche [6]
";

  char* car_1_string = strtok(string, "&");
  char* car_2_string = strtok(NULL, "&");

  char* car_1_name = strtok(car_1_string, "[");
  int car_1_id = atoi(strtok(NULL, "]"));

  char* car_2_name = strtok(car_2_string, "[");
  int car_2_id = atoi(strtok(NULL, "]"));

  struct _Car_* car_1 = createCar(car_1_name, car_1_id);
  struct _Car_* car_2 = createCar(car_2_name, car_2_id);

  addCar(my_list, car_1);
  addCar(my_list, car_2);

  return 0;
}

void printList(struct _Car_List_* my_list)
{
  int i = 0;
  struct _Car_* car = my_list -> car_list[i];

  while (car != NULL)
  {
    printf("- %s [%d]
", car -> name_, car -> id_);
    i = i + 1;
    car = my_list -> car_list[i];
  }
}

int testAdd(struct _Car_List_* my_list)
{
  struct _Car_* car = createCar("Renault", 1);
  addCar(my_list, car);

  return 0;
}

int main()
{
  struct _Car_List_* my_list = malloc(sizeof(struct _Car_List_));
  my_list -> number_of_cars = 0;
  for (int i = 0; i < 100; i = i + 1)
    my_list -> car_list[i] = NULL;

  testAdd(my_list);
  parse(my_list);

  printf("Below the printed list:
");
  printList(my_list);

  return 0;
}

请告诉我我我我我,我是错了吗?

问题回答

strtok does not allocate any new memory. car_1_name and car_2_name both point at memory inside string. string is a local variable and will be deallocated when parse exits. That memory may then be overwritten leaving car_1_name and car_2_name pointing at garbage.

您必须把浮点器复制到新的记忆中,可能还有strdup





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签