Q:

Reading pin level Raspberry Pi

I've been using the code from this example under the C gpio examples. I am able to setup and write to the pins without an issue with the defines:
// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))

#define GPIO_SET *(gpio+7)  // sets   bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0

and
  INP_GPIO(4); // must use INP_GPIO before we can use OUT_GPIO
  OUT_GPIO(4);
  GPIO_SET = 1<<4;

This works fine. However I'm not sure what to do if I want to read from a pin. I've tried to read it by returning gpio + thePin but I believe that is giving me the address and not the value. I tried returning a pointer but that gave me garbage (-232783856) as well.
Any ideas how to read value from a pin?

A:

#define GPIO_LEV *(gpio+13)                  // pin level
INP_GPIO(4);                                 // pin 4 initialization for reading
unsigned int value = GPIO_LEV;               // reading all 32 pins
bool pin4_value = ((value & (1 << 4)) != 0); // get pin 4 value

This code is based on function bcm2835_gpio_lev in bcm2835 library and is usable only for pins 0 to 31. If it does not work, look into this library - there is doubled reading of value.

