Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Arrays and pointers in C

  1. Jul 12, 2008 #1
    Code (Text):

    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?
     
  2. jcsd
  3. Jul 12, 2008 #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 (Text):

    *(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*)).
     
  4. Jul 12, 2008 #3
    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?
     
  5. Jul 12, 2008 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Try it with other types of pointers....
     
  6. Jul 12, 2008 #5

    Dale

    Staff: Mentor

    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.
     
  7. Jul 12, 2008 #6

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
     
  8. Jul 12, 2008 #7

    Dale

    Staff: Mentor

    Yeah, there is that :redface:
     
  9. Jul 12, 2008 #8
    I tell you my answer. a1,a2,a3 are all arrays. But they are allocated memory in heap not stack.
     
  10. Jul 12, 2008 #9

    rcgldr

    User Avatar
    Homework Helper

    They are pointers, not arrays.

    Here's an example of the difference:

    Code (Text):

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

        p1 = p2;    // copies a pointer
        a1 = a2;    // copies an entire array
     
     
  11. Jul 12, 2008 #10

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Actually, that one is just a syntax error.
     
  12. Jul 15, 2008 #11

    rcgldr

    User Avatar
    Homework Helper

    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 (Text):

    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 (Text):

    #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: Jul 15, 2008
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?