I am writing a demo program to print address of the variable. To make the code look cleaner I have created a header file in which the function is declared(ex.pointer.h) and a C file for calling that function(ex. main.c). I want to print the address of the variable with the variable name called in main.c main.c:
#include "pointer.h"
int main(int argc, char *argv[]){
int var=40;
printAddress(var);
}
pointer.h
#include<stdio.h>
int *p;
#define getName(var) #var
void printAddress(int a){
p=&a;
printf("Address of variable %s is %p\n",getName(a),p);
}
I tried using macros but it printsAddress of variable a is 0x7fff2424407c which is not the expected outputAddress of variable var is 0x7fff2424407c. Can anyone please tell me what changes I should make and please explain.