0

Whats the difference between Struct tag and Pointer to Struct? Are the same? Or Here is my example, Is obj and *var two different memory location?

#include <stdio.h>
#include <stdlib.h>

struct alloc {

    char data;
};

int main(int argc, char** argv) {

    struct alloc obj;
    struct alloc *var = calloc(5,sizeof(struct alloc));
    (var -> data) = 'P';
    printf("Data:%d",obj.data);

    return (EXIT_SUCCESS);
}
Dr.pK
  • 37
  • 7
  • 2
    Possible duplicate of [Difference between static memory allocation and dynamic memory allocation](https://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation) – Fantastic Mr Fox Oct 20 '17 at 18:22

1 Answers1

1

Yes, they are. Memory for obj is allocated statically (on stack), whereas for var dynamically (on heap). The main practical difference is, that statically allocated objects get destroyed at the end of the block, but you have to free the memory for dynamically allocated objects manually (in order to prevent memory leaks).

You can find more information on this topic here.

Honza Dejdar
  • 947
  • 7
  • 19