2017-04-02 77 views
0

这取自:https://exploreembedded.com/wiki/AVR_C_Library DS1307_GetTime()方法,即时尝试了解此函数如何工作。所以我在下面做了一个简单的例子。函数中的无符号字符值

你能解释一下GetTime()函数中发生了什么,我应该传入什么值的孩子?

我的目标是获得int main()函数中的b值。

我的理解到目前为止是:

指针*一个= I2C_Read();指向无符号字符,但指针不能指向一个值,为什么不是错误?

#include <stdio.h> 
#include <stdlib.h> 

unsigned char I2C_Read() 
{ 
    unsigned char b = 0b11111111; 
    return b; 
} 

void GetTime(unsigned char *a) 
{ 
    *a = I2C_Read(); 
} 

int main() 
{ 
    unsigned char *a = 0; 
    GetTime(a);      // ? 

    printf("Value of b is: %d\n" , b); // ? 
} 
+3

代码deferences一个空指针。最好使用无符号字符变量的地址来调用GetTime,而不要使用空指针。 –

+1

“_...但一个指针不能指向一个值_”呃???但是,这恰恰是一个指针:指向一个值,可以是一个数组元素,一个结构或者像这里一样是一个无符号字符。 –

回答

1

要设置指针a0 - 不是有效值

您需要阅读了关于指针 - 但在此期间更改代码以

unsigned char a = 0; 
GetTime(&a); 
printf("Value of b is: %d\n" , a); 
0

更改主要功能为:

int main() 
{ 
    unsigned char a = 0; // 
    GetTime(&a); // call by reference concept 
    printf("Value of b is: %d\n" , a); 
} 

这将导致b = 255,如果要打印字符,请替换%d - >%c。 也许它会帮助你。

0

机管局现在我明白了,谢谢

1) GetTime(&a);     // pass in address of a. 
2) GetTime(unsigned char *a)  // takes in contents of a, at the moment = 0 
3) *a = read();     // set contents of a to unsigned char 0b11111111 
4) printf("Value of b: %d\n" , a); // call this from main func results in returned value b = 255