c - 为无符号整数赋值*

标签 c

我定义了以下结构

struct ComputeData{
  unsigned int * temperatureRawBuf;
  unsigned int * bloodPressRawBuf;
  unsigned int * pulseRateRawBuf;
  unsigned int * tempCorrectedBuf;
  unsigned int * bloodPressCorrectedBuf;
  unsigned int * prCorrectedBuf;
  unsigned short int * measurementSelection;
};
typedef struct ComputeData ComputeData;

在我的主文件中有以下内容

  unsigned int temperatureRaw [8] = {75, 75, 75, 75, 75, 75, 75, 75};

  unsigned int bloodPressRaw [16] = {80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};

  unsigned int pulseRateRaw [8] = {50, 50, 50, 50, 50, 50, 50, 50};

  unsigned int tempCorrected [8] = {0, 0, 0, 0, 0, 0, 0, 0};

  unsigned int bloodPressCorrected [16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ;

  unsigned int prCorrected [8] = {0, 0, 0, 0, 0, 0, 0, 0};

  //create the TCB for Compute
  ComputeData * computeDataPtr;
  computeDataPtr = (struct ComputeData *) malloc(sizeof(struct ComputeData));
  //assign compute data locals to point to the values declared at the top
  computeDataPtr->temperatureRawBuf = &temperatureRaw[0];
  computeDataPtr->bloodPressRawBuf = &bloodPressRaw[0];
  computeDataPtr->pulseRateRawBuf = &pulseRateRaw[0];
  print("Test2"); //pseudocode for print

  computeDataPtr->tempCorrectedBuf = &tempCorrected[0];
  computeDataPtr->bloodPressCorrectedBuf = &bloodPressCorrected[0];
  computeDataPtr->prCorrectedBuf = &prCorrected[0];
  print("Hello"); //pseudocode here for print

编辑:

我已经重新编辑了问题,并提供了更详细的代码

如果我在第一次打印后注释掉代码,则会打印“Test2”的值。但是,一旦我取消注释掉第一个打印程序下面的代码,就无法打印任何值。

最佳答案

value->abc 本身的赋值是有效代码,但为了使其成功,value 必须被赋值以指向 的有效实例>随机,例如

random storage;
random *value = &storage;
value->abc = arr;

random *value = malloc(sizeof(random));
value->abc = arr;

请注意,如果 arr 是本地数组,则在 arr 范围结束后使用指向它的指针本身将是未定义的行为。如果您计划延长 arr 的生命周期,例如,通过从函数返回 value,您还需要 malloc 数组:

random *value = malloc(sizeof(random));
value->abc = malloc(7 * sizeof(unsigned int));
memcpy(value->abc, arr, 7 * sizeof(unsigned int));

关于c - 为无符号整数赋值*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46880850/

相关文章:

c - x<<y>>z C 中的求值顺序

mysql - Eclipse中使用C程序连接MYSQL

c - 函数如何在没有 return 语句的情况下返回一些东西?

python - 在 MPI Python 程序中查找 SegFault 的一般建议

c - 优化轴对齐边界框检查

c - scanf 上的段错误并非在所有系统上都存在

c - ARM设备树文件中,三个中断值是什么意思

c++ - 为什么我的数组程序中不能使用空格?

c - valgrind 报告 "Invalid write of size 8"

c++ - 在 C++ 程序中调用 Python 函数