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

In summary,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; a
  • #1
ladesidude
4
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
  • #2
>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*)).
 
  • #3
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?
 
  • #4
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...
 
  • #5
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.
 
  • #6
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.
 
  • #7
Hurkyl said:
Except, of course, for the eminently practical fact that they will need to be freed.
Yeah, there is that :redface:
 
  • #8
I tell you my answer. a1,a2,a3 are all arrays. But they are allocated memory in heap not stack.
 
  • #9
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:

1. What is an array in C?

An array in C is a data structure that allows for the storage of multiple values of the same data type in a single variable. It can be accessed using an index, which represents the position of the value in the array. Arrays are declared using square brackets and can have a fixed or dynamic size.

2. How are arrays and pointers related in C?

Arrays and pointers are closely related in C because arrays are actually a type of pointer. When an array is declared, it is automatically converted to a pointer to the first element in the array. This allows for efficient memory management and manipulation of array elements.

3. What is the difference between an array and a pointer in C?

Although arrays and pointers are closely related, they have some key differences. Arrays have a fixed size, while pointers can be dynamically allocated and resized. Additionally, arrays cannot be reassigned to point to a different location, while pointers can be reassigned.

4. How do you access elements in an array using pointers?

In order to access elements in an array using pointers, you can use pointer arithmetic. This involves incrementing or decrementing the pointer to the desired element based on its index in the array. For example, to access the third element in an array, you can use the pointer arithmetic expression: *(arrayPtr + 2).

5. Can you pass arrays as arguments to functions in C?

Yes, you can pass arrays as arguments to functions in C. When an array is passed to a function, it is automatically converted to a pointer to the first element in the array. This allows the function to access and manipulate the elements in the array. However, the size of the array must also be passed as an argument in order for the function to know the length of the array.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
6
Views
154
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
886
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
753
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
1
Views
942
  • Programming and Computer Science
Replies
5
Views
881
Back
Top