Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to access initialized values of global variables

11 views
Skip to first unread message

Swati Rathi

unread,
May 9, 2012, 10:52:07 AM5/9/12
to
I am trying to fetch the initialized values of global variables.
For eg : int i = 10;

I am able to extract the value using DECL_INITIAL.
But when I have a case like below,

struct node
{
int x;
int y;
};

struct node n1=
{
99,
88,
};

I get value as {.x=99, .y=88}

How should I iterate to fetch 99 and 88 individually?
In a way I want to access individual variables initialized values.
Can someone tell me the way to do it.

Pascal J. Bourguignon

unread,
May 14, 2012, 6:54:57 PM5/14/12
to
[pjb@kuiper :0 tmp]$ cat a.c
#include <stdio.h>

struct node
{
int x;
int y;
};
struct node n1=
{
99,
88,
};

void iterate(){
int* refs[]={&n1.x,&n1.y,0};
int i;
for(i=0;refs[i];i++){
printf("%d: %d\n",i,*(refs[i]));
}
}

int main(){
iterate();
fflush(stdout);
return(0);
}
[pjb@kuiper :0 tmp]$ gcc -o a a.c
[pjb@kuiper :0 tmp]$ ./a
0: 99
1: 88
[pjb@kuiper :0 tmp]$



--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
0 new messages