alloca()
As others here have noted, it's similar to malloc()
but allocates the space in the stack frame instead.
However, its use is discouraged, as noted in this question. If you overrun the stack you won't get a "nice" error message. Instead, you'll get all sorts of undefined behaviour. Note however that the GNU libc manual does list some advantages of alloca()
(with their corresponding disadvantages, though).
VLA (Variable-length array)
Variable-length arrays were a feature of C99 that was later relegated to a conditional feature in C11. Availability of VLA depends a lot on the C dialect you're using, and if you are willing to allow extensions (e.g, GNU C). It's main advantage over alloca()
is that it provides array-like semantics.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char* strrev(char* str)
{
size_t i, len = strlen(str);
char buffer[len]; /* Creates VLA from len variable */
for (i = 0; i < len; i++) {
buffer[len-i-1] = str[i];
}
return strncpy(str, buffer, len);
}
int main(void)
{
char text[] = "Hello world!";
printf("%s\n", strrev(text));
}
Decaying pointers
Note however, that arrays decay to simple pointers when they are passed on to a function as parameters.
int func(int array[128]);
int func(int *array);
No matter which prototype you choose, taking sizeof(buffer)
will yield the same value as sizeof(char*)
inside the function bodies.
Inaccuracies in the question
I feel there were some slight slips in the question. I'm addressing them because I believe they will help us both understand the problem better. Having a common vocabulary goes a long way towards communicating complex ideas.
"Declaring a pointer uses dereference operator : int *x;
"
Even if it looks the same, that's not the dereference operator. It says it in the very same page you linked:
Note that the asterisk (*) used when declaring a pointer only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign.
"When declaring a[3]
, it allocates 3 empty memory addresses on the stack."
Newly allocated memory is not empty (how would you define empty memory?). The contents are indeterminate unless an initialisation is specified for the object (see §6.2.4).
"On this photo I've given the a
a scope of 3."
3
is the length, the size or the number of elements. of 'a[]'.
Refer to §6.2.1 for the definition of a scope:
For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope.
"I want to do the same precedence of memory address allocation declaration using pointers"
If you speak of "precedence", the first thing that comes to mind is operator precedence
, which determines the order in which operations take place. Did you mean you wanted pointers allocated on the stack?
I'm expecting the possibility to create a pointer declaration that allocates memory from the current address and the addresses it follows so it would be array-like structure.
If you have a pointer to a valid block of memory
char* block = malloc(150);
You can already access it like an array:
block[6] = block[32];