the address of the array is contained in the array name that is a pointer to the array it means when we are declaring an integer array a[5] we are getting 10 bytes for the array elements and two bytes for the pointer a?
means 12 bytes are occupied in fact
is it so and i just want to confirm to store a address(pointer variable) how many bytes we require is it 2 bytes?

Dani AI

Generated

Short answer: declaring int a[5] allocates space for five int elements only. There is no extra hidden pointer stored alongside the array just because you can use a like an address. was right to correct that. The array identifier can "decay" to a pointer to the first element in many expressions, but an array object and a pointer variable are different things.

A small experiment you can run shows the difference clearly:

#include <stdio.h>

int main(void) {
    int a[5];
    int *p = a;
    printf("sizeof a     = %zu\n", sizeof a);   /* total bytes for the array */
    printf("sizeof p     = %zu\n", sizeof p);   /* size of a pointer on this platform */
    printf("sizeof a[0]  = %zu\n", sizeof a[0]);/* size of a single element */
    return 0;
}

On a typical modern 32‑bit system where int is 4 bytes you will see sizeof a == 20 and sizeof p == 4. On a 64‑bit system sizeof p is usually 8. Pointer size is platform/compiler dependent (historically some 16‑bit compilers used 2‑byte pointers).

Two practical points that clear up common confusions: the type of a when used in most expressions is int * (pointer to first element), but &a has type int (*)[5] (pointer to whole array). That means (a + 1) advances by one int, whereas (&a + 1) advances by the entire array size (5 ints). Also, an array of pointers (as noted) stores pointer values for each element; sizeof that array gives you the total bytes occupied by those pointer values, not by the objects they point to.

Last gotcha: when you pass an array to a function it decays to a pointer, so sizeof inside the function will report the pointer size, not the original array size. Pass lengths explicitly or use compile‑time/static sizing when you need the true array size.

Recommended Answers

All 6 Replies

> means 12 bytes are occupied in fact
Nope, guess again.
Arrays are not pointers.

if you have a[10]

a refers to the address of the first element of the array
so you will have 10 * sizeof(int) bytes occupied by the array.

to be clear:
a = &a[0]
(a+1) = &a[1]

and
*a = a[0]
*(a+1) = a[1]
and so on..

There can be array of ponters.Since a pointer variable always contain an address,an array
of pointers would be nothing but a collection of addresses.The addresses present in the array of pointers can be addresses of isolated variables or addresses of array elements
or any other address.All rules that apply to an ordinary array of pointers as well.I think a program would clarify the concept.

main()
{
int *arr[4];    /*array of integer pointers*/
int i=31,j=5,k=19,l=71,m;


arr[0]=&i;
arr[1]=&j;
arr[2]=&k;
arr[3]=&l;


for(m=0;m<=3;m++)
printf("%d",*(arr[m]));
}

boyz is banned. I thought this thread would be closed.

boyz is banned. I thought this thread would be closed.

We don't close threads just because the thread starter gets banned.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.