Like Tree5Likes
  • 5 Post By comocomocomo

2d array

This is a discussion on 2d array within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int HighestTemp(int**, const int, const int); int HighestTemp(int** temp, const int row, const int ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    24

    2d array

    Code:
    #include <iostream>
    using namespace std;
    
    int HighestTemp(int**, const int, const int);
    
    int HighestTemp(int** temp, const int row, const int col) {
    	int highest = 0;
    	for (int i=0; i<row; i++) {
    		for (int j=0; j<col; j++) {
    			if((int)*(temp+i*col+j) > highest) {
    				highest = (int)*(temp+i*col+j);
    			}
    		}
    	}
    	return highest;
    }
    
    int main() {
    	int temp[3][3]={1,2,3,4,5,6,7,8,9};
    	int h=HighestTemp((int**)temp, 3, 3);
    	cout << h << endl;	
    	return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > int HighestTemp(int**, const int, const int);
    Change this to
    int HighestTemp( int(*)[3], const int, const int);

    > int HighestTemp(int** temp, const int row, const int col)
    int HighestTemp(int (*temp)[3], const int row, const int col)

    > if((int)*(temp+i*col+j) > highest)
    To
    if( temp[i][j] ) > highest)

    And
    > int h=HighestTemp((int**)temp, 3, 3);
    to
    int h=HighestTemp(temp, 3, 3);
    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.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    Quote Originally Posted by gunitinug View Post
    Code:
    //..
        int temp[3][3]={1,2,3,4,5,6,7,8,9};
        int h=HighestTemp((int**)temp, 3, 3);
    //..
    Arrays and pointers are _not_ the same. The bare name of the 2d array _decays_ into a pointer to array(s) of 3 ints each: int (*)[3]. This cannot be simply converted to int **. It is something completely different.

    I strongly recommend you to read the "Arrays and pointers" section of the C FAQ: Arrays and Pointers

    Besides, this forum has some wonderful documents that explain how to ask questions... ;-)

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Please don't use horrible code like this. It's so easy to make mistakes, it looks ugly, it's hard to maintain, and you have to deal with pointers.
    Just make your life easier, use

    std::vector<std::vector<T>> v(N, std::vector<T>(M));

    Where T is your type, N is size of the first dimension and M is the size of the second dimension. It would look something like

    T array[N][M];

    if written as a fixed array.
    And please ask an actual question next time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  3. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 6
    Last Post: 11-09-2006, 02:28 AM

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