Thread: How to assign array of pointers to pointers, to content of static array?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    How to assign array of pointers to pointers, to content of static array?

    Code:
    char myData[90][3][50];   //90 rows, 3 columns, each block 50 chars long   
    char **tableData[90] = {NULL};  
    
    const char* text;
    text = sqlite3_column_text(stmt, col_index);
    
    text_lenght = strlen(text) + 1;         // add 1 to get null 
    strncpy(myData[row_index][c_index], text, text_lenght);
    //top line in a loop for all rows and each column    <--EDIT the assignment is outside the column loop within the row loop.
    
    
    tableData[row_index] = myData[row_index]; <--?Warning on this line
    I need tableData to correctly point to contents of myData but it's not happening.
    The warning is:
    Code:
     : warning C4047: '=' : 'char ** ' differs in levels of indirection from 'char (*)[50]'
    During run time I can see myData properly populated how I expect. However, tableData is filled with bad pointers...

    Any help is appreciated.
    Thanks.
    Last edited by tommy00b; 06-30-2009 at 07:13 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to assign every element of tableData to point where you want it to point.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Assigning each element runtime error

    I moved the assignment inside the column loop that is within the row loop and I believe this assigns each element, correct?
    Code:
    tableData_gmti[row_index][c_index]= myData_gmti[row_index][c_index];
    No warnings but now I have the runtime error:
    Code:
    Unhandled exception at 0x0040308e in GUI.exe: 0xC0000005: Access violation writing location 0x00000000.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I need tableData to correctly point to contents of myData but it's not happening.

    That will never work because 'myData' isn't an array of pointers.

    Code:
    int main( int argc, char** argv )
    {
    	const int 
    		ROW_COUNT = 90, 
    		COLUMN_COUNT = 25, 
    		TEXT_LENGTH = 50;
    	typedef char 
    		text_block[TEXT_LENGTH];
    	typedef text_block 
    		columns[COLUMN_COUNT];
    	typedef columns
    		table[ROW_COUNT] ;
    	table 
    		myTable; 
    	columns*	
    		myColumns[ROW_COUNT] = {0};
    	myColumns[8] = &myTable[15];
    	strncpy(myTable[15][4], "foobar", TEXT_LENGTH - 1);
    	puts((*myColumns[8])[4]); 	
    }
    Last edited by Sebastiani; 06-30-2009 at 07:36 PM. Reason: fixed confusing variable names
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by tommy00b View Post
    Code:
    char myData[90][3][50];   //90 rows, 3 columns, each block 50 chars long   
    char **tableData[90] = {NULL};  
    ...
    tableData[row_index] = myData[row_index]; <--?Warning on this line
    I need tableData to correctly point to contents of myData but it's not happening.
    Perhaps something like
    Code:
        char *a[50];
        char md[90][3][50];
        char **td[90] = {NULL};  
        a[0] = md[0][0];
        td[0] = &a[0];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  2. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

Tags for this Thread