试图使用联合索引结构中的数据

时间:2019-02-04 12:23:33

标签: c indexing struct segmentation-fault unions

我正在尝试从存储为a:b:c:d:e:f格式的文件中检索数据,并且我想使用union为索引中的每个字段建立索引struct。即,我应该如下定义unionstruct,但是当我尝试使用并集/结构时会遇到一系列警告,当然还有分段错误。

#define _XOPEN_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FIELDS 6

typedef union{
  char a[FIELDS];
  struct {
    char* a;
    char* b;
    char* c;
    char* d;
    char* e;
    char* f;
  } s;
} Elements;

int main(int argc, char const *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s data\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  const int COLONS = 5;
  const int MAXFIELDS = 5000;

  char *buffer = 0;
  char *token = 0;
  size_t len = 0;
  size_t field_length = 0;
  ssize_t nread;
  Elements* totalElements = malloc(MAXFIELDS);
  int total_elements = 0;

  FILE *fp;

  fp = fopen (argv[1], "r");
  if (fp == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }

  while ((nread = getline(&buffer, &len, fp)) != 1) {
    token = strtok(buffer, ":");

    field_length = snprintf( NULL, 0, "%s", token );
    totalElements[total_elements].a[0] = malloc( field_length );
    strcpy (totalElements[total_elements].a[0], token);

    for (int i = 1; i < COLONS - 1; i++){

      field_length = snprintf( NULL, 0, "%s", token );
      totalElements[total_elements].a[i] = malloc( field_length );
      strcpy (totalElements[total_elements].a[i], token);

      token = strtok(NULL, ":");

    }

    field_length = snprintf( NULL, 0, "%s", token );
    totalElements[total_elements].a[COLONS - 1] = malloc( field_length );
    strcpy (totalElements[total_elements].a[COLONS - 1], token);

    token = strtok(NULL, "\n");

    field_length = snprintf( NULL, 0, "%s", token );
    totalElements[total_elements].a[COLONS] = malloc( field_length );
    strcpy (totalElements[total_elements].a[COLONS], token);

    total_elements++;
  }

  fclose (fp);
  free (buffer);
  free (totalElements);

  exit(EXIT_SUCCESS);

  return 0;
}

编译器gcc显示的警告:

ex.c:51:40: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
     totalElements[total_elements].a[0] = malloc( field_length );
                                        ^ ex.c:52:13: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy (totalElements[total_elements].a[0], token);
             ^~~~~~~~~~~~~ In file included from ex.c:6:0: /usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘char’  extern char *strcpy (char *__restrict
__dest, const char *__restrict __src)
              ^~~~~~ ex.c:57:42: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
       totalElements[total_elements].a[i] = malloc( field_length );
                                          ^ ex.c:58:15: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
       strcpy (totalElements[total_elements].a[i], token);
               ^~~~~~~~~~~~~ In file included from ex.c:6:0: /usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘char’  extern char *strcpy (char *__restrict
__dest, const char *__restrict __src)
              ^~~~~~ ex.c:65:49: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
     totalElements[total_elements].a[COLONS - 1] = malloc( field_length );
                                                 ^ ex.c:66:13: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy (totalElements[total_elements].a[COLONS - 1], token);
             ^~~~~~~~~~~~~ In file included from ex.c:6:0: /usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘char’  extern char *strcpy (char *__restrict
__dest, const char *__restrict __src)
              ^~~~~~ ex.c:71:45: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
     totalElements[total_elements].a[COLONS] = malloc( field_length );
                                             ^ ex.c:72:13: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy (totalElements[total_elements].a[COLONS], token);
             ^~~~~~~~~~~~~ In file included from ex.c:6:0: /usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘char’  extern char *strcpy (char *__restrict
__dest, const char *__restrict __src)
              ^~~~~~

1 个答案:

答案 0 :(得分:1)

联合中的char a[FIELDS]字段应为char * a[FIELDS],因为结构是char *char a[]不是char *的数组而是{ {1}}。