Pergunta de entrevista da empresa Bloomberg

char *a = "hello"; char b[] = "hello"; char *c = malloc(12); What do these do, what's the difference in where the memory is stored?

Respostas da entrevista

Sigiloso

5 de abr. de 2013

char *a = "hello"; // pointer a on the stack, point to the "hello", which in in the static memory zone char b[] = "hello"; // char array b on the stack, make a copy of "hello“ to array b char *c = malloc(12); // allocate 12 bytes space on the heap, and pointer c on the stack pointing to the allocated space

3

Sigiloso

20 de dez. de 2014

warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] char* p = "hello"; error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive] char* c = malloc(12);