pointers to arrays

This is a discussion on pointers to arrays within the C Programming forums, part of the General Programming Boards category; hi, one of the basics i still got problems with is the use of pointers to arrays. Code: void function ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    45

    pointers to arrays

    hi, one of the basics i still got problems with is the use of pointers to arrays.


    Code:
    void function (int *p){
      
       int i;
    
       for(i=0; i<10;i++,p++)
          *p=i;                           //fills array with numbers 0 to 9
    
    }
    
    int main (void){
       
       int array[10];
    
       function (&array[0]);      //adress of first element in array
    
    return 0;
    }
    is this a good way to use a pointer to an array?

    now, you can also create a pointer to array by defining a doing something like:

    Code:
    void function (int *tab[]){
      
          ....
    
       }
    
    int main (void){
       
       int *array[10];
    
       function (array);      //adress of first element in array
    
    return 0;
    }
    whats the difference, whats better and most of all how do i use last this pointer to fill my array?
    Last edited by breaka; 07-20-2006 at 08:08 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Well... I would pass the size of the array to the function, as well. That way you can use it for arrays with size other than 10.

    Hmm, you changed your code on me. The second one really just adds another level of indirection. Just understand that *array[10] is an array of pointers. I think you're looking for a pointer to an array (*array)[10]
    Last edited by SlyMaelstrom; 07-20-2006 at 08:21 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    thanx SlyMaelstrom, any suggestions for the secondpart of my question (edited post1)

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    thanx, i ll check it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 10:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 03:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 04:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM

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