Programming STM 32- Address of Variable

Address of variables

&myData

An & sign put before the variable name actually gives the address of the data myData variable holds

The format specifier used to print the address of a variable is %p

Example

#include <stdio.h>

int main()

{

   char a1 = ‘A’;   

   printf(“The address of variable a1 is %pn”,&a1);

   return 0;

}

The result for the code is:

The address of variable a1 is 0x7fffa52293b1

While printing values of all the variables

#include <stdio.h>

int main()

{

   char a1 = ‘A’;

   char a2 = ‘p’;

   char a3 = ‘p’;

   char a4 = ‘l’;

   char a5 = ‘e’;

   char a6 = ‘:’;

   char a7 = ‘)’;

   

   printf(“The address of variable a1 is %pn”,&a1);

   printf(“The address of variable a2 is %pn”,&a2);

   printf(“The address of variable a3 is %pn”,&a3);

   printf(“The address of variable a4 is %pn”,&a4);

   printf(“The address of variable a5 is %pn”,&a5);

   printf(“The address of variable a6 is %pn”,&a6);

   printf(“The address of variable a7 is %pn”,&a7);

   return 0;

}

Output

The address of variable a1 is 0x7ffe379d6c11

The address of variable a2 is 0x7ffe379d6c12

The address of variable a3 is 0x7ffe379d6c13

The address of variable a4 is 0x7ffe379d6c14

The address of variable a5 is 0x7ffe379d6c15

The address of variable a6 is 0x7ffe379d6c16

The address of variable a7 is 0x7ffe379d6c17

Here we can observe that the address for the values are consecutive.

Here the value of variable a1, which is 1000001(binary for 65 ASCII value of A) is stored at location 0x7fffa52293b1

Storing address of a variable

You can store the address of a variable using the code

#include <stdio.h>

int main()

{

   char a1 = ‘A’;

   unsigned long int add_a1 = &a1;

   printf(“The address of a1 is %pn”,add_a1);

   return 0;

}

The code may run but you may receive 2 warnings. The warnings may not be visible is you are using the onlineGDB compiler. If you are using the eclipse(ie.STM32 Cube IDE) compiler you may receive the following warnings.

a.      format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘long unsigned int’ [-Wformat=]             address.c            /address/src     line 18  C/C++ Problem

b.      initialization of ‘long unsigned int’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion] address.c            /address/src     line 17  C/C++ Problem

This is because the address of a variable is not a number but a pointer which is a different data type which is char* . We can resolve this warning by converting pointer data type to type casting.

Type casting

We can use typecasting in the previous example by

unsigned long int add_a1 = (unsigned long int)&a1;

printf(“The address of a1 is %lun”,add_a1);

Here we are using %lu instead of %p as format specifier since %p is for pointer and %ul is for unsigned long int. %ld is used for signed long int. and %l64d / %lld or %l64u / %llu depending up on compiler for long long int.

Leave a comment

Your email address will not be published. Required fields are marked *