Thread: A question about Arrays and Pointers

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    69

    A question about Arrays and Pointers

    Hey,

    I am wondering what this is:

    Code:
    int* a[3]
    - Is it a array of int* or
    - is it a pointer to an array of ints?

    I am asking because you can actually deepcopy multidimensional arrays using memcopy, while you cannot using type**.

    After some research I experienced that an multidimensional array is stored as a coherent "object". There are no pointers to adresses stored.

    So I am wondering what exactly happens when I pass an array as an type**.
    Does C create pointers to actualy locations, because if not: How does the computer know the size of the stored arrays?

    Thank you!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    "a" is a static array of "int*" and of size "3".

    A multi-dimensional array in C is just 1-D in memory. For example, the following two arrays are equivalent:
    Code:
    #define COLUMNS 3
    #define ROWS 4
    
    int a[ROWS][COLUMNS];
    int b[ROWS*COLUMNS];
    
    a[2][1] = 123;
    b[2*COLUMNS + 1] = 123;
    Those are the so-called 2-D arrays. What you have here is an array of pointers. Sure, it can be used as a 2-D array, but don't assume that they are equivalent.

    because if not: How does the computer know the size of the stored arrays?
    It doesn't.
    Devoted my life to programming...

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This is a 3 element array of pointer to ints
    Code:
    int* a[3];
    Used like this...
    Code:
    int *a[3]; /* Notice that the * is usually put next to the variable name */
    int x,y,z;
    x = 1;
    y = 2;
    z = 3;
    a[0] = &x;
    a[1] = &y;
    a[2] = &z;
    If you want to create a pointer to an array of int
    Code:
    int a[3] = {0};
    int (*b)[3];
    b = &a;
    For more info on declarations...
    Reading C Declarations
    http://users.ece.utexas.edu/~ryerrab...eclPrimer.html
    How does the computer know the size of the stored arrays?
    If it is defined in scope you can use
    Code:
    int apple[3] = {0};
    size_t appleSize;
    appleSize = sizeof(apple)/sizeof(apple[0]);
    Otherwise you need to pass the size of the array into the functions that you wish to use, like the second argument in fgets.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    As everybody already answered, this:
    Code:
    int *a[3];
    Is an array of 3 pointers to int. And this:
    Code:
    int (*a)[3];
    Is a pointer to an array of 3 ints.

    This:
    Code:
    int **a;
    Is a pointer to a pointer which points to an int.

    The three declarations are completely different from each other.

    In the context of a function, main(), for exemple, the argument argv can be writen as one of these two:
    Code:
    int main( int argc, char *argv[] )
    int main( int argc, char **argv )
    They are equivalent because any array in the argument declaration is passed as a pointer. So, if you have something like this:
    Code:
    // array of 3 ints.
    int v[3] = { 0, 1, 2 };
    
    // array of 3 pointers to ints.
    int *a[3] = { &v[0], &v[1], &v[2] };
    
    // Prototype. accepts a pointer to a pointer to int.
    int f( int **pp );
    
    // Calls f() with a pointer which points to pointers to ints.
    f( a );
    Here a is converted to a pointer to the first element of the array (identified by the symbol a). Which is the same as:
    Code:
    f(&a[0]);
    In the context of memset(), for example, the pointer in the argument points to a buffer which will be filled. A pointer of type int ** will be a pointer which points to an array of pointers, so the pointers pointed will be filled. Let's say:
    Code:
    memset( ( void * )a, 0, sizeof a );
    This will fill the array a with zeroes, so a[0] = a[1] = a[2] = 0 (which is the same as NULL). Notice the array v remains untouched.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    Understood! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-03-2017, 02:54 AM
  2. A question about pointers and arrays
    By Daikon in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2016, 05:08 AM
  3. Question in arrays and pointers
    By ASophokleous in forum C Programming
    Replies: 3
    Last Post: 10-27-2013, 11:35 AM
  4. Pointers and multidimensional arrays question
    By d2rover in forum C Programming
    Replies: 10
    Last Post: 11-07-2010, 10:43 AM
  5. Replies: 7
    Last Post: 05-19-2010, 02:12 AM

Tags for this Thread