簡體   English   中英

將值分配給無符號的int *

[英]Assigning value to an unsigned int *

我定義了以下結構

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實例,例如

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));

您沒有分配任何內存,只是創建了一個指針。 您的指針指向哪里?

您可能想做的是:

random value;
random.abc = arr;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM