Start structure pointer with an address?

Asked

Viewed 219 times

7

I wonder if it is possible to start this structure type pointer with an address, without needing to point to another variable, or allocate memory.

typedef struct{

    char nome[20];
    int idade;

}pessoa;


pessoa leonardo;
pessoa *p;

p = &leonardo;
p->idade = 23;
printf("%d", p->idade);

Or allocating memory.

pessoa *p = malloc(sizeof(pessoa));
  • 1

    It is possible yes, but it only makes sense if this address is mapping some device, whose data layout is the same as struct

  • A second struct?

  • Look at the answer that has already been given below. You can do this, but it is assumed that at 1234 there are 20 bytes representing the name plus 4 representing age as a int. If not, anything can happen.

1 answer

6


Can:

p = (pessoa*)1234;

Now, you gotta know what you’re gonna do with it...

  • Here gave error..

  • Here did not give......

  • Says invalid type of argument *, has int

  • show the code

  • You have to declare "p" as pointer, as you asked in your question: person p; p = (person) 1234**

  • Yes, I already changed it, it worked. but from the error in the execution, I saw no use.

  • @user56294: That’s what I tried to explain earlier. You only do things like this when you’re programming a driver for a device, or programming for an embedded device. In such cases there are fixed memory addresses to communicate with the hardware.

  • 1

    It’s like I said in the reply, you have to know what to do with it...the comment from @C. E. Gesser above makes sense, sometimes there are hardware devices that appear as a memory address, so it makes sense. For example, in DOS the video is a memory area, so by tapping a pointer to that memory you can write to the video by changing the memory via a pointer.

  • opa, how cool in.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.