我是C初學者。我在C中實現了一些代碼,它將調用一個接口函數,參數爲uint8_t *value
pass作爲緩衝區和int32_t *length
作爲緩衝區的長度傳遞。如何將uint8_t *作爲緩衝區傳遞給函數
/*interface function declaration in header file
* this interface implementation is not under my
* control. I just call the following function.
* The return value is 0(successful) or 1(fail)
*/
int get_parameter(uint8_t *value, uint32_t *length);
/*My .c File */
/*can it be passed as buffer? because the function will
*assign some memory located value to this parameter.
*uint8_t cant have value greater than 255 but in my
*case the memory located value can be larger than 255
*/
uint8_t value;
/*Is it correct ?
*This should be the length of the buffer declared above*/
uint32_t length = sizeof(value);
/* interface function call in my code */
int result = get_parameter(&value, &length);
if(result == 0)
{
char *data;
int32_t *myValue; /*want to assign what value parameter pointing to*/
memcpy(data + sizeof(int32_t), &value, length); /*perhaps something like this ?*/
}
值和長度/輸出參數。該接口函數將分配值,可以是<或> 255,以賦值參數並將該值的長度以字節分配給長度參數。
我的問題是,如何調用接口函數int get_parameter(uint8_t *value, uint32_t *length)
,以便它可以爲值參數分配一些值,而與大小無關。我有點困惑值的參數,因爲uint8_t只能有最大值255,但在我的情況下,它可以大於255.我期待解決方案是這樣的。
char *value;
uint32_t length = sizeof(value);
/* interface function call in my code */
int result = get_parameter(value, &length);
if(result == 0)
{
uint32_t *myValue;
*myValue = atoi(value);
printf("%d", *myValue); /*it should print whatever the value assigned by the get_parameter function*/
}
@SouravGhosh對不起,我沒有任何例子。我必須執行它。如果我有一個例子,我不需要問這個問題。我可以簡單地遵循示例代碼。 – Shawn
「那個價值」是指什麼? – kaylum
@shan我沒有要求例子的代碼,功能的例子輸入/輸出是我所要求的。請參閱[遊覽](http://stackoverflow.com/tour)並閱讀[如何提問](http://stackoverflow.com/help/how-to-ask),以便了解我們對問題的期望。 :) –