Array and Pointers

This is a discussion on Array and Pointers within the C++ Programming forums, part of the General Programming Boards category; Hi All. I was trying to compile this program....but it was not compiling. Can any one help.. Code: #include<iostream.h> void ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    India
    Posts
    14

    Array and Pointers

    Hi All.
    I was trying to compile this program....but it was not compiling.
    Can any one help..
    Code:
    #include<iostream.h>
    
    void display(int *a,int row,int col){
    			int i,j;
    			for(i=0;i<row;i++)
    			 {
    				cout<<"\n";
    				for(j=0;j<col;j++)
    					{
    						cout<<" "<<*(a+i*col+j);
      						}
    				   }
    		}
    
    void main(){
    	int a[3][2]={
    			{1,5},
    			{3 ,2},
    			{1,4},
    			};
    	int i,j;
    	display(a,3,2);
    	getch();
         }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first, when you have compile errors, post them.

    - change 'iostream.h' to 'iostream'
    - add 'using namespace std;' after the include statement
    - in your function 'display', 'a' is an array so in your last cout statement i imagine you want a[i]
    - 'void main' should be 'int main'
    - i think if you use 'getch' you have to '#include <ncurses>'

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    What's the point of indenting if you do it at random?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    India
    Posts
    14

    compile errors

    Quote Originally Posted by nadroj View Post
    first, when you have compile errors, post them.

    - change 'iostream.h' to 'iostream'
    - add 'using namespace std;' after the include statement
    - in your function 'display', 'a' is an array so in your last cout statement i imagine you want a[i]
    - 'void main' should be 'int main'
    - i think if you use 'getch' you have to '#include <ncurses>'
    The error coming while compiling is
    1) cannot convert ' int [2] * ' to ' int * '
    2) Type mismatch in parameter a in call to display

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, in int main() a is declared as a two-dimensional array, which needs to be passed to a function that accepts a pointer to a pointer.

    I suppose, if the array is declared statically as you do, it's still a contiguous block of memory, and you can also pass the first pointer (a[0]).

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    India
    Posts
    14
    Quote Originally Posted by anon View Post
    Yes, in int main() a is declared as a two-dimensional array, which needs to be passed to a function that accepts a pointer to a pointer.

    I suppose, if the array is declared statically as you do, it's still a contiguous block of memory, and you can also pass the first pointer (a[0]).
    Thanks for your response.
    It worked with display(a[0],row, col)

    I was wondering if there is any other way to do this except this a[0] way......

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    &a[0][0] would also work.

    Hand sub-scripting a 2D array is on the margins.
    http://c-faq.com/aryptr/ary2dfunc2.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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