Thread: passing arrays and passing pointers

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    passing arrays and passing pointers

    if i have an array in main(), but i need to pass it to a function, which would be better (i want to modify the real array):

    1. Declare a ptr in main() that points to the array, and pass it

    2. Declare an array of ptrs in main() that points to the array, and pass it

    3. Or just pass the array itself. because if i do this, isnt it automatically converted to a pointer in the function?

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    3. Or just pass the array itself. because if i do this, isnt it automatically converted to a pointer in the function
    an array name is a pointer, so just pass the array name
    Code:
    void fill_array( char *p );
    
    // in main
    
    char array[20];
    
    fill_array( array );
    Also on a side note because an array name is a pointer you can use pointer notation on it.
    Code:
    char array[20];
    
    *( array + 2 ) = 'a';
    Last edited by C_Coder; 04-13-2002 at 12:25 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    i just went like this

    Code:
    void UpdateDisplay(char Board[4][4]);
    
    //in main
    
    char board[4][4];
    
    UpdateDisplay(Board);
    
    //end main
    
    void UpdateDisplay(char Board[4][4])
    {
    	for(int a=1;a<=3;a++)
    	{
    	    cout<<static_cast<int>(Board[a][1])<<" | "<<static_cast<int>(Board[a][2])<<" | "<<static_cast<int>(Board[a][3])<<endl;
    		if(a!=3) cout<<"--+---+---"<<endl;
    	}
    }

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    and that acts like a pointer, how it modifies board[][4] in main()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 10:25 AM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM