0

Ive been trying this for hours no and have made no progress, the programme should create a 2D array in the function of a 16 by 16 grid of x's and then in the main programme i should be able to print this grid on the console but when run i get no result, any help would be apreciated (newbie)

#include <iostream>
#include <cstdlib>

char **create2DArray();  //function prototype 

#define WIDTH 16 
#define HEIGHT 16 

char** myArray;  //global array 


char **create2DArray(){ 
    int i,j; 
    char **array = (char **) malloc(sizeof(char *) * WIDTH); 

    for(i=0; i<WIDTH; i++) 
        array[i] = (char *) malloc(sizeof(char) * HEIGHT);  

        for(i=0; i<WIDTH; i++)              
            for(j=0; j<HEIGHT; j++)             
                array[i][j] = 'x'; 
    return array; 
} 

int main(int argc, char** argv) {

    char **create2DArray(); 
    myArray = create2DArray(); 
    void printArray(char** array); 

    return 0;
}
3
  • 1
    1-this is obviously not C++ but C. Please change tag. 2-Please tell us what the problem is with the code you show. What happens ? Does it compile ? If yes, what is the given output ? Commented Apr 19, 2015 at 16:04
  • void printArray(char** array); What is this supposed to do? This does not call any function, if that is what you're trying to do. In addition, I do not see a function that prints. Commented Apr 19, 2015 at 16:06
  • In main, you're just declaring a function called printArray(), but you never implement it. If you need to call that function, you need to implement it. Commented Apr 19, 2015 at 16:13

1 Answer 1

1

You have to implement printArray function.

void printArray(char** array)
{
    for(int i=0; i<sizeof(array); i++)
    {
        for(int j=0; j<sizeof(array[i]); j++)
        {
            std:: cout << array[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

Then call it in the main, and add void printArray(char** array) as a function prototype.

printArray(myArray);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.