c - 双字符指针和strcpy()之间存在冲突

标签 c arrays pointers char

两个双字符指针 **lines 和 **splitted_line 之间存在奇怪的地址问题。在 strcpy 之后,一些 char* 参数消失。

Error readFile(FILE *file, ConfigValues *values)
{
  int filesize;
  char *buffer;
  size_t readchars;
  char **lines = malloc(sizeof(char*));
  char **splitted_line = malloc(sizeof(char*));
  char *buffer_line;
  char *buffer_token;
  int counter_lines = 0;
  int counter = 0; //TODO reneme
  int counter_argument = 0;
  int printf_counter = 0; //TODO to be deleten
  fseek(file, 0, SEEK_END);
  filesize = ftell(file);
  rewind(file);
  buffer = (char*) malloc(sizeof(char) * filesize);

  if (buffer == NULL)
  {
    printf(ERROR_OUT_OF_MEMORY);
    return OUT_OF_MEMORY;
  }  
  readchars = fread(buffer, sizeof(char), filesize, file);
  if (readchars != filesize)
  {
    printf("Reading error\n");
    return OTHER;
  }
  printf("%s\n", buffer);
  //unfortunately you cant nest strtok, cuz of the NULL-param in the while!
  buffer_line = strtok(buffer, "\n");
  while (buffer_line != NULL)
  {
    lines = realloc(lines, (counter_lines + 1) * sizeof(*lines));

    lines[counter_lines] = (char*) realloc(lines[counter_lines],
        strlen(buffer_line) * sizeof(char*));
    printf("Stelle lines: %p, %p\n", lines, lines[counter_lines]);
    strcpy(lines[counter_lines], buffer_line);

    counter_lines++;
    buffer_line = strtok(NULL, "\n");
  }

  printf("counterlines: %d\n", counter_lines);
  while (printf_counter < counter_lines)
  {
    printf("%d:  %s\n", printf_counter, lines[printf_counter]);
    printf_counter++;
  }

  //TODO repair from down here

  while (counter < counter_lines)
  {
    printf("%d::  %s\n", counter, lines[counter]);
    buffer_token = strtok(lines[counter], " ");
    printf("Erstes argument der zeile: %s\n", lines[counter++]);
    while (buffer_token != NULL)
    {
      splitted_line = realloc(splitted_line,
          (counter_argument + 1) * sizeof(*splitted_line));
          printf("Stelle splitted lines: lines: %p, %p\n", splitted_line, lines[counter]);

      splitted_line[counter_argument] = (char*) realloc
          (splitted_line[counter_argument],
           strlen(buffer_token) * sizeof(char*));

      printf("%d, counter_argument: %d\n", strlen(buffer_token), counter_argument);
      strcpy(splitted_line[counter_argument], buffer_token); //the evil line!!!

      printf("gespeichert: %s\n", splitted_line[counter_argument]);

      counter_argument++;
      buffer_token = strtok(NULL, " \n");
      printf("test2: %s\n", buffer_token);
    }

    printf("-----------\n");

    counter_argument = 0;
    counter++;
  }
  return SUCCESS;
}

开头的 char** 行:

0:分辨率 600 600 1:第 5 页 2:尼克斯 3:风力 180 10 4:引力6.5

最后:

0:分辨率 1:第 5 页 2:尼克斯 3:风力 180 10 4:引力

需要一些帮助!

最佳答案

使用 buffer = (char)malloc(...),您会将动态分配的内存指针的值从 4 或 8 字节(取决于您的平台)截断为 1 字节。

此后的代码会产生未定义的行为。

关于c - 双字符指针和strcpy()之间存在冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27204853/

相关文章:

c - 结构内部的结构 : to point or not to point?

c - 在 Visual C++ 中使用 char 表示固定字符串

javascript - 在 for 循环中创建对象语法错误

c++ - 有什么你可以做的,唯一的方法是在 C++ 中使用指针吗?

c - 在 C : square brackets vs. 指针中传递数组

c - 系统 ("cls") 是否更改了我的变量?

c - 我的指针错误地更改了地址

php - 更新函数内的全局数组

javascript - 具有 Angular 和 Angular Material 的图像列表

c - 第一个字节和第一个字节元素地址之间的差异