M Sharath Hegde
M Sharath Hegde

Reputation: 493

how to trace this stack and pointer

#include<stdio.h>
#include<stdlib.h>
struct test
{
    int *p;
};
typedef struct test * TESTP;
struct ex
{
TESTP *testpp;
};
typedef struct ex * EXP;
void main(void)
{
    int x=10;
    struct test t2; 
    TESTP t1=(struct test *)malloc(sizeof(struct test));
    EXP e1=(EXP)malloc(sizeof(struct ex));
    (e1->testpp)=&t1;
    t1->p=&x;
    printf("%d\n",**(e1->testpp));
}

I would like to trace back to value stored at pointer p(i.e., 10), by using e1. is that possible to trace that? This code edited accidentally, i am not sure this will work. if it works please show me how can i trace back to value in 'p' using 'e1'.

Upvotes: 0

Views: 170

Answers (1)

Brian Cain
Brian Cain

Reputation: 14619

You want to be able to dereference a chain starting with e1 getting to p eventually.

Here is how you do that:

printf("%d\n",*((*(e1->testpp))->p));

Upvotes: 2

Related Questions