Alina
Alina

Reputation: 1

unsigned char value in a function

This is taken from: https://exploreembedded.com/wiki/AVR_C_Library DS1307_GetTime() method, im trying to understand how this function works. So i made a simplified example below.

Can you explain what is happening in the GetTime() function and what kid of value should I pass into it?

My goal is to obtain b value inside int main() function.

My understanding so far is:

pointer * a = I2C_Read(); points to unsigned char, but a pointer can not point to a value, why isnt it erroring?

#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); // ?
}

Upvotes: 0

Views: 1099

Answers (3)

Alina
Alina

Reputation: 1

Aa I get it now, thank you

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

Upvotes: 0

Minion
Minion

Reputation: 1064

Change the main function as :

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

This would result into b = 255 , if you want to print the character then replace the %d --> %c . Maybe it will help you.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60037

You are setting the pointer a to 0 - not a valid value

You need to read up about pointers - but in the meantime change the code to

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

Upvotes: 1

Related Questions