为结构成员赋值 [英] Assigning values to members of structures

查看:30
本文介绍了为结构成员赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为 C 编程课程设计一个项目,但遇到了一个问题.我对 C 语言还是很陌生,但有 Java 编程经验.

I am currently working on a project for a course in C programming and have run into a problem. I am still quite new to the language C but have experience programming Java.

当我尝试为 structchar * 变量成员赋值时,我的程序遇到了问题.有问题的代码如下:

My program runs into a problem when I try to assign a value to a char * variable member of a struct. The code in question follows below:

void ReadFile(ComponentType * Data, int * numEl, int * numNodes)
{
  size_t index;
  FILE * dataFile;
  char * data;
  float * value;

  numEl = malloc(sizeof(int *));
  numNodes = malloc(sizeof(int*));
  Data = malloc(6 * sizeof(ComponentType));

  * numEl = 0;
  * numNodes = 0;
  index  = 0;   

  if((dataFile = fopen("mydata.dat", "r")) == NULL)
      puts("Error - file \"mydata.dat\" could not be opened!");
  else
  { 

      while(!feof(dataFile)) 
      { 

          fscanf(dataFile, "%s", data);
          Data[index].name = (char *)malloc(strlen(data)+1);
          strcpy(Data[index].name, data);

          fscanf(dataFile, "%s", data);
          Data[index].node1 = (char *)malloc(strlen(data)+1);
          strcpy(Data[index].node1, data);

          fscanf(dataFile, "%s", data);
          Data[index].node2 = (char *)malloc(strlen(data)+1);
          strcpy(Data[index].node2, data);

          fscanf(dataFile, "%f", value);
          Data[index].value = * value;


          int i = char2int(CircuitData[index].node1);
          if(i > * numNodes)
              * numNodes = i;

          i = char2int(CircuitData[index].node2);
          if(i > * numNodes)
              * numNodes = i;

          (* numEl)++;
          (* numNodes)++;
          index++;
      }

      fclose(dataFile);
  }

  free(Data);
  free(numEl);
  free(numNodes);       
}

结构定义如下:

typedef struct
{
  char * name;
  char * node1;
  char * node2;
  float value;
} ComponentType;

我对程序进行了调试,基本上发生的是文件被正确读取并且存储在 data 中的值是正确的,但是打印出 Data[index].name 要么在格式化为 char * 时导致分段错误,要么在调试程序中打印出 0x0.简而言之,从文件中读取的值不会被复制到我想要它们的结构变量中.

I have run a debug on the program and what basically happens is that the file is read correctly and the value stored in data is correct however printing out Data[index].name either causes a segmentation fault when formatted as a char * or, in the debug program, prints out 0x0. In short, the values read from the file are not being copied into the structure variable I want them in.

任何建议/帮助将不胜感激.谢谢.

Any suggestions/help would be appreciated. Thank you.

我使用 Linux 64 位编码

I am using Linux 64-bit to code

编辑 2:我添加了发生错误的整个函数,并进行了下面建议的更改

EDIT 2: I added the entire function in which the error occurs with the changes suggested below

推荐答案

第一:当你调试你的程序时,检查sizeof(data)是什么.
你会惊讶地发现它是 8(指针的大小),而不是 6(你分配的大小)
这是因为 sizeof 测量类型的大小,而 data 是一个 char*,(又名指针).

指针是 8 字节.(在 64 位系统上.其他系统不同)
这会导致您为 Data[index].name 分配 8 个字节.

First: when you debug your program, check what sizeof(data) is.
You will be surprised to find it is 8 (the size of a pointer), and NOT 6 (the size you allocated)
This is because sizeof measures the size of the type, and data is a char*, (aka. a pointer).

Pointers are 8-bytes. (on a 64-bit system. Other systems are different)
This causes you to allocate 8 bytes for Data[index].name.

下一步:您不能使用 = 运算符将数据从一个数组复制到另一个数组,就像您尝试使用的那样:

Next: You cannot copy data from one array to another using the = operator, as you tried to do with:

Data[index].name = data;

<小时>

正确操作:

fscanf(dataFile, "%s", data);
// Measure the length of data, and allocate enough space (including the null-termiantor)
Data[index].name = malloc(strlen(data) + 1);  

// Copy from data into Data[index].name.   For greater safety, look into strcpy_s
strcpy(Data[index].name, data);

这篇关于为结构成员赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆