Thread: convert int to int error

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    19

    convert int to int error

    Can someone who knows C++ tell me what i doing silly in the code below
    i'm getting the error

    "Cannot convert int * to int in function main"


    Code:
    #include <stdlib.h>
    #include <iostream.h>
    
    int main(void)
    {
    
    int i,j;
    int ***DataCamera;
    
    DataCamera = new int[10];
    
    	for (i=0;i<=10;i++);{
            DataCamera[i] = new int [20];
                    for (j=0;j<=10;j++);
                      DataCamera[i][j]=new int[5];
    	}
    
    return 0;
    }

    any idea how i can fix it
    Last edited by sononix; 08-03-2004 at 07:19 AM.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Data camera is a triple pointer, so you can't make it point to an array of ints, it would have to point to an array of int double pointers. It seems like you need that triple pointer, so this would fix it:
    Code:
    DataCamera = new int** [10];

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I guess this is an extension of your attempt to allocate multiple dimension arrays
    http://cboard.cprogramming.com/showthread.php?t=55359

    Well here you go
    Code:
    // synthesize int array[10][20];
    int **array;
    array = new int*[10];
    for ( int i = 0 ; i < 10 ; i++ ) array[i] = new int[20];
    
    // do stuff
    
    for ( int i = 0 ; i < 10 ; i++ ) delete[] array[i];
    delete[] array;
    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.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    19
    Yes your guess would be right though was having major headaches trying to get that malloc function to work so moved over to c++ world

    not that familar with these pointer arrays nor the syntax

    cheers guys for pointing me in some direction

    cheers

    Later

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    19
    Where can i get some more info on this

    Code:
    new int
    i need to have control on how to assign values to the array and be able to dynamically change its dimensions

    cant find any examples of how you can define values for the array inside the for loop

    Any ideas
    Last edited by sononix; 08-03-2004 at 07:32 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    cant find any examples of how you can define values for the array inside the for loop
    Do you know how to prompt for a number? Do you know how to read a number? Ok, do both in a loop. Prompt for a number, then assign it to some place in your array.
    Code:
    for( x = 0; x < foo; x++ )
        for( y = 0; y < bar; y++ )
        {
            cout<<"Enter a number for array["<<x<<"]["<<y<<"]" :"
            cin>>array[x][y];
        }
    If that's not what you mean, please try and form better sentences.

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

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    19
    Ah Quzah if thats what i meant id be back in junior grade now wouldn't I

    Cheer Salem the code works for 2d defintion though what i need to do is define 3d arrays


    I'm looking to create a 20 20 3 array and im finding out its not all that straight forward as i thought when using pointers and the dynamic memory allocation.
    Code:
    int main(void)
    {
    
    int Row = 20;
    int Col = 20;
    int i,x;
    
    int ***array;
    
    array = new (int**)[3];
      for (int i=0; i < 3; i++)
      {
        array[i] = new (int*)[Row];
        for(int x=0; x < Row; x++)
          array[i][x] = new int[Col];
      }
    
        for (int i = 0; i < 3; i++)
    {
       for (int x = 0; x < Row; x++)
          delete [] array[i][x];
       delete [] array[i];
    }
    delete [] array;
    }
    This problem with this is i get a suspicious pointer conversion in function main()


    Found some nice 2d code but looks difficult to get a 3d version working for it

    Code:
    #include <iomanip>
    #include <iostream>
    const int Col = 4;
    void init2D(int A[][Col], int nRows, int nCols);
    
    int main(void)
    {
    int Row =5;
    int (*A)[Col];
    A = new int [Row][Col];
    
    init2D(A, Row, Col);
    
    for(int i =0; i<Row; i++){
            for(int j=0; j<Col; j++)
            	cout <<setw(5) <<A[i][j];
            cout << endl;
            }
    }// end main
    
    
    
    void init2D(int A[][Col], int nRows, int nCols){
    	for (int i=0; i< nRows; i++)
                    for(int j=0; j<nCols; j++)
                    A[i][j]=6*i;
    
    }
    Seems like all arrays in C++ are delat as one huge 1D array

    n e one any suggestion where i can go from here to create my 3D array

    cheers later

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In the case of multidimensional arrays, you can build from what you already know. The following compiles for me when placed in appropriate context. Notice the relationship between the number of * in the declaration of oneD, twoD, and threeD and the number of * associated with each subsequent call to new within each type of array.
    Code:
    //static 3D array
    int threeDArray[20][20][3];
     
    int x = 20;
    int y = 20;
    int z = 3; 
     
    //dynamic 1D array
    int * oneD;
    oneD = new int[x];
     
    //dynamic 2D array
    int ** twoD;
    twoD = new int*[x];
    int i,
    for(i = 0; i < x; ++i)
      twoD[i] = new int[y];
     
     
    //dynamic 3D array
    int j;
    int *** threeD;
    threeD = new int**[x];
    for(i = 0; i < x; ++i)
    {
      threeD[i] = new int *[y];
      for(j = 0; j < y; ++j)
    	threeD[i][j] = new int[z]
    }

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    19
    Cheers guys for all the help works fine

    Found this link when i was searching

    http://groups.google.com/groups?q=da...one.com&rnum=1

    compares memory allocation using new and std

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    1

    Cool int to int problem a 3d matrix code snipet

    Code:
    #include <stdlib.h>
    #include <iostream.h>
    
    int main(void)
    {
    
    int i,j,k;
    int ***DataCamera;
    
    DataCamera = new (int**[10]);
    
    	for (i=0;i<=10;i++){
            DataCamera[i] = new (int*[20]);
            for (j=0;j<=10;j++)
    		{
                DataCamera[i][j]=new int[5];
    		    for(k=0;k<5;k++)
    			{
    					  DataCamera[i][j][k]=i+j+k;
    			}
    		}
    	}
    	for (i=0;i<=10;i++){
                    for (j=0;j<=10;j++)
         			  for(k=0;k<5;k++)
    				  {
    					  cout<<"DataCamera[i][j][k]= "<<DataCamera[i][j][k];
    				  }
    	}
    	
    
    	for (i=0;i<=10;i++){
                    for (j=0;j<=10;j++)
    				{
         			     delete [] DataCamera[i][j];
    				}
    				delete []DataCamera[i];
    				}
    	delete [] DataCamera;
    return 0;
    
    }
    Last edited by Salem; 08-04-2004 at 06:36 AM. Reason: CODE TAGS!!!!

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (i=0;i<=10;i++){
    Arrays run from 0 to N-1 - you've overstepped the ends of you allocated memory

    Use
    for (i=0;i<10;i++){
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM