Can you explain the size difference between arrays and pointers in C?

  • Thread starter Thread starter ladesidude
  • Start date Start date
  • Tags Tags
    Arrays Pointers
AI Thread Summary
In C, a1, a2, and a3 are pointers to the first elements of dynamically allocated memory, not true arrays. While they can be used similarly to arrays, they do not have the same properties; for instance, their sizes are determined by the pointer type, which is consistent across all three (sizeof(int*) returns the same value). The actual sizes of the allocated memory blocks differ, as a1, a2, and a3 point to different amounts of memory. When using sizeof on these pointers, it will always return the size of the pointer itself, not the size of the allocated memory. Therefore, a1, a2, and a3 are pointers that reference arrays, but they are not arrays themselves.
ladesidude
Messages
4
Reaction score
0
Code:
int* a1 = malloc(4 * sizeof(int));
int* a2 = malloc(3 * sizeof(int));
int* a3 = malloc(5 * sizeof(int));
a1[0] = 1; a1[1] = 2; a1[2] = 3; a1[3] = 4;
a2[0] = 9; a2[1] = 8; a2[3] = 7;
a3[0] = -1; a3[1] = -2; a3[2] = -3; a3[3] = -4; a3[4] = -5;

My question:

Are a1, a2 and a3 arrays? I think they are pointers that point to an array right?

Secondly, If a1, a2, and a3 are considered to be arrays, are they of the same size?
 
Technology news on Phys.org
>Are a1, a2 and a3 arrays? I think they are pointers that point to an array right?

Well, pointers that point to the first element in an array. You can do pointer arithmetic to accomplish the same thing the subscript operator does; e.g.:
Code:
*(a1+1) = 2;

>Secondly, If a1, a2, and a3 are considered to be arrays, are they of the same size?

If you consider them as arrays, then I would say no; you obviously shouldn't access a1[4] but you can access a3[4]. On the other hand, if you use sizeof on them, you'll get the same thing (sizeof(int*)).
 
JaWiB said:
>Are a1, a2 and a3 arrays? I think they are pointers that point to an array right?

Well, pointers that point to the first element in an array. You can do pointer arithmetic to accomplish the same thing the subscript operator does; e.g.:
Code:
*(a1+1) = 2;

>Secondly, If a1, a2, and a3 are considered to be arrays, are they of the same size?

If you consider them as arrays, then I would say no; you obviously shouldn't access a1[4] but you can access a3[4]. On the other hand, if you use sizeof on them, you'll get the same thing (sizeof(int*)).

i did use sizeof() and they all came back as 4, since the size of an int is 4 bytes and since its pointing to the first element in the array, I think. So its safe to say that they are not of the same size?
 
ladesidude said:
i did use sizeof() and they all came back as 4, since the size of an int is 4 bytes and since its pointing to the first element in the array, I think.
Try it with other types of pointers...
 
The name of an array is a constant pointer to the first element of the array. In your example a1, a2, and a3 are not constant, so they are not exactly the same as the name of an array. If you had declared them constant then, for all practical purposes, they would be the names of arrays.
 
DaleSpam said:
If you had declared them constant then, for all practical purposes, they would be the names of arrays.
Except, of course, for the eminently practical fact that they will need to be freed. That they won't be allocated on the stack could be relevant. And, of course, the fact that sizeof a1 will return the size in bytes of a pointer rather than the array that was allocated.
 
Hurkyl said:
Except, of course, for the eminently practical fact that they will need to be freed.
Yeah, there is that :redface:
 
I tell you my answer. a1,a2,a3 are all arrays. But they are allocated memory in heap not stack.
 
They are pointers, not arrays.

Here's an example of the difference:

Code:
int *p1, *p2;
int a1[5], a2[5];

    p1 = p2;    // copies a pointer
    a1 = a2;    // copies an entire array
 
  • #10
Jeff Reid said:
Code:
    a1 = a2;    // copies an entire array
Actually, that one is just a syntax error.
 
  • #11
Hurkyl said:
Actually, that one is just a syntax error.
That used to work with older C (not C++) compilers. An alternate example using structure instead of array (this works with Visual Studio Express):

Code:
typedef struct {
    int i[5];
}SI5, *PSI5;

static SI5 si0;
static PSI5 psi0;

int main(int argc, char **argv)
{
SI5 si1;
PSI5 psi1;

    si1  =  si0;    // copy a structure
    psi1 = psi0;    // copy a pointer
    return(0);
}

Getting back to the array example, it can be treated similar to pointer, when passed to a function. As Hurkly pointed out, an array name can't be used for assignment. The sizeof an array is different than the size of a pointer to an array.

Code:
#include <stdio.h>
void x(int *, int *);

int main(int argc, char **argv)
{
int ai0[5];
int *pai0 = ai0;
    printf ("%d %d\n", sizeof(ai0), sizeof(pai0));
    x(ai0, pai0);
    return(0);
}

void x(int *pi0, int*pi1)
{
    printf ("%x %x\n", pi0, pi1);
}

// on a 32 bit system, first line output is 20 4
// 2nd line output will show the same address for both values
 
Last edited:
Back
Top