我正在尝试从以a:b:c:d:e:f格式存储数据的文件中检索数据,我想使用union
来索引struct
中的每个字段。也就是说,我应该定义union
和struct
如下,但是当我尝试使用联合/结构时,我遇到了一堆警告,当然还有分段错误错误。
#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 datan", 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)
^~~~~~
联合中的char a[FIELDS]
字段应该是char * a[FIELDS]
的,因为结构是char *
的,char a[]
不是char *
数组,而是char
数组。