[英]How to assign pointer to element of array of some struct in this array(OpenCL)?
[英]How to assign value for element in array at struct pointer in C
我有這樣的結構:
struct temper_t {
unsigned char rom[8];
struct temper_t *next;
};
在這個主要代碼中,我想為rom [8]賦值,我該怎么做:
new_node = (struct temper_t *) malloc(
sizeof(struct temper_t));
new_node->next = NULL;
int m;
unsigned char rom_value[8];
//Luu thong tin vao node moi
for (m = 0; m < 8; m++) {
new_node->rom = rom_value[m]; // not working
}
感謝您閱讀我的問題。
為了方便理解,我創建了一個MCVE:/ * * test.c * *創建於:2015年9月29日*作者:phuongh1 * /
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
struct temper_t {
int rom[8];
struct temper_t *next;
};
int main(void) {
struct temper_t *new_node;
new_node = NULL;
int m;
int rom_value[8];
rom_value[0] = 10;
rom_value[1] = 13;
rom_value[2] = 15;
rom_value[3] = 16;
rom_value[4] = 18;
rom_value[5] = 21;
rom_value[6] = 25;
rom_value[7] = 27;
new_node = (struct temper_t *) malloc(sizeof(struct temper_t));
new_node->next = NULL;
//Luu thong tin vao node moi
for (m = 0; m < 8; m++) {
new_node->rom[m] = rom_value[m]; // not working
}
}
我找到了原因。 因為我的IDE用於固件編碼,並且在編譯時已優化,所以調試窗口中的信息顯示new_node-> rom的值和rom_value不匹配。 =>因為固件代碼,所以我無法打印值。 在我使用Visual Studio運行代碼之后,它們是匹配的。
您忘記在分配循環中指定rom
的索引:
//Luu thong tin vao node moi
for (m = 0; m < 8; m++) {
new_node->rom[m] = rom_value[m]; // not working
}
注意 :由於rom_value
尚未初始化,當前您正在分配給new_node->rom[m]
未知值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.