Reputation: 773
I've researched and I clearly understand that arrays are not pointers
But I've learned some things that I think, might be possible to declare an array-like variable using pointer declaration. Things that I'm aware:
int *x;
a[b]
➜ *(a+b)
a[b][c]
➜ *(*(a+b)+c)
a[3]
, it allocates 3 empty memory addresses on the stack.*(a+0)
, *(a+1)
, *(a+3)
is allocated on stack for use.
On this photo I've given the a
a scope of 3I want to do the same precedence of memory address allocation declaration using pointers
Can I do something like that using pointers?
Making a declaration of pointer point to pointers to fetch different addresses would be easy as **p
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
Example: I know int a[3];
using pointer declaration wouldn't be just int *(a+3)
If what I ask is impossible please answer why.
This is a simple code of a normal array's declaration and call: Ideone Code
#include <stdio.h>
char block[3]={1,2,3};// This line will be removed
//And would create a pointer declaration of that having array-like structure
int main() {
while(i < sizeof(block))
printf("%d", block[i++]);
//And that I still could call the contents of it even just using *(block+i)
return 0;
}
Output: 123
Upvotes: 2
Views: 349
Reputation: 1420
As per my understanding of your question .Normally array's would be decayed(treated) as pointers when you try to access the members of it as you explained in the first link you specified.
if you trying to allocate memory in run time and pointer to act as like array
you can do something like for your code using malloc
// 3 memory location's of size `int(4)` being allocated in heap who's starting address is pointed by `p` (returned to `p` by malloc)
x = 3;
int *p = malloc(x*sizeof(int)) // for 1 2 3
int i = 0;
p[i++] = 1; //*(p+ i++)
p[i++] = 2; //*(p+ i++)
p[i++] = 3; //*(p+ i++)
i = 0;
int main() {
while(i++ < x)
printf("%d", p[i++]); // printf("%d", *(p+i));
return 0;
}
or you want memory in stack you use alloca(3) as specified by others.
Upvotes: 1
Reputation: 10551
When declaring a[3], it allocates 3 empty memory addresses on the stack.
When declaring sometype a[3];
, it allocates 3 contiguous _cells_ with undefined content (not "empty") with automatic storage (not necessarily, but often, on a stack).
I want to do the same precedence of memory address allocation
Whatever that means, here is a stab in the dark where all elements are contiguous again:
char *line = "hello\0world\0goodbye\0world";
char *argv[] = {line+0, line+6, line+12, line+20};
Upvotes: 0
Reputation: 390
I am not that clear about what your question is exactly but is this the solution:
#include <stdio.h>
char block[]={1,2,3};
char *p=block;
int main() {
int i=0;
while(i < sizeof(block))
{
printf("%d", *(p+i));
i++;
}
return 0;
}
Upvotes: 0
Reputation: 1783
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).
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));
}
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.
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.
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.
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).
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.
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?
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];
Upvotes: 7
Reputation: 15121
Your terms are confusing me.
Anyway, if what you want to ask is whether you can allocate memory dynamically in the stack frame (of the caller)?
The answer is yes, you can do it by using alloca(3)
. This function is like malloc()
, but instead of allocating memory in the so-called heap, it allocates memory in the current stack frame.
Upvotes: 1