How to point a pointer to an array

This is a discussion on How to point a pointer to an array within the C Programming forums, part of the General Programming Boards category; Hmmm, I“ve been trying to get this work but no luck. I have declared a pointer to an int array[5] ...

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    How to point a pointer to an array

    Hmmm, I“ve been trying to get this work but no luck. I have declared a pointer to an int array[5] but when I assign the pointer I get a warning ( differs in levels of indirection from 'int *').

    Code:
    #include <stdio.h>
    #define SIZE 5
    
    int main(void)
    {
    
    	int array1[SIZE] ={5,4,3,2,1};
                    int array2[10] = {1,2,3,4,5,6,7,8,9,0};
                    
    	int (*point) [SIZE];
                    /* Should work??? */ 
    	point = array1;
                    /* This shouln“t work 
                        point = array2 
                        or does it ??? */
    return 0;
    }
    I just want to create a pointer that can point to an int-array of 5 element, how do i do that???

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: How to point a pointer to an array

    Code:
    #include <stdio.h>
    #define SIZE 5
    
    int main(void)
    {
    
    	int array1[SIZE] ={5,4,3,2,1};
                    int array2[10] = {1,2,3,4,5,6,7,8,9,0};
                    
    	int *point;
                    /* works */ 
    	point = array1;
                    /* works */
                    point = array2;
                    /* works */
    return 0;
    }
    hello, internet!

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    A pointer to an array contains the address of the array's first element. Simple as that.

    Code:
    point = array1;
    this piece of code really expands to: point = &array[0];
    Therefore, just declare it as
    int *point;

    And make sure you don't go out of bounds when incrementing the pointer.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Hmm, thats absolutly right. Maybe I asked the question wrong??
    Could you explain for me the difference between

    Code:
    int (*point)[SIZE];
    int *point[SIZE];
    I know that the second declaration does but do not have a clue on the first one???

    Is it a pointer to an array of SIZE int elements????

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by ripper079
    Hmm, thats absolutly right. Maybe I asked the question wrong??
    Could you explain for me the difference between

    Code:
    int (*point)[SIZE];
    int *point[SIZE];
    I know that the second declaration does but do not have a clue on the first one???

    Is it a pointer to an array of SIZE int elements????
    they both have meaning (in fact the same meaning i believe) but neither is what you want here. i'm sorry but i'm no good at explaining these things; but a pointer doesn't refer to an array of SIZE, a pointer merely refers to the first element; you then put what ever you want in the subscript and make sure you dont out of bounds what you're pointing too. what you typed just above me is an array of pointers.
    hello, internet!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    int *ptr[SIZE];


    This is an array of pointers to integers. The size of the array (ie: the number of pointers you have) is defined as 'SIZE'.

    Read from right to left:

    [SIZE] = an array of SIZE size.
    ptr = named ptr
    * = of pointers
    int = to type 'int'

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Yup. What moi said. If you want to point to an array, just use a pointer. Because the pointer would point only to a single element which is an int. Therefore you only need to declare an int pointer.

    int *point[SIZE];
    This is an array of pointers. You will use it if you need to point to multiple objects. If you want that to point to every single array element you can do this:
    Code:
    int i;
    int *point[SIZE];
    int array[SIZE];
    
    for(i = 0; i < SIZE; i++)
       point[i] = &array[i];
    This will cause each pointer in your array to point to each int in the int array.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  8. #8
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    k, got it. Everthing is clear now exept one thing. What does

    Code:
    int (*point)[SIZE];
    mean????

  9. #9
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    >>int (*point)[SIZE];<<

    This would be used if you were using a pointer to a function (in this case that line would be included in the function header). Remembering that a pointer to a function contains the address of the function in memory. "Pointers to functions can be passed to funcitins, returned from functions, and assigned to other function pointers" (Deitel & Deitel, 2nd Edition, C: How to Program, p291).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer of array of class
    By 11moshiko11 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2008, 10:58 AM
  2. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 12:07 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21