hi all,

I am new to c++. I want to return array. I found that I have to use pointers but doesnt have idea.
here is my code

// test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
const int rows=13;
const int coloums=6;
char *begingStatus();
void print(char val[rows][coloums]);


int _tmain(int argc, _TCHAR* argv[])
{
	cout <<"hi ";
	char *ptr = new char[rows][coloums];
	ptr=begingStatus();
//	int nptrewarray[5]=ptr;
    print(ptr);
	cin.get();
	return 0;
}

void print(char val[rows][coloums])
{
	for(int i=1; i<14;i++)
	{
		for(int j=1;j<7; j++)
		{

		cout <<val[i][j];
		cout << '\n';
		}
		
	}

}
char *begingStatus()
{
	char *seats= new char[rows][coloums];
	for(int i=1; i<rows;i++)
	{
		for(int j=1;j<coloums; j++)
		{

		seats[i][j]='*';
		}
		
	}
	return seats;
}

plz check my code and let me know how to correct this

thanks in advance,
menuaka

One critical error I see is that you are indexing your arrays starting at 1. In C++, arrays are indexed from 0 to n-1 where n is the number of elements in the array. This means that in some of your loops you are accessing one past the end of your array and in others you are failing to access the first element.

yes yes thanks to show me that error.

but it doesnt help for the above question.
any way thanks again!

EDIT: deleted

this is how you create a 2-dimensional char array

char **ptr = new char *[rows]
for(int i=0;i<rows;i++)
       ptr[i]=new char[columns];

thanks for the reply.

can u give me an example of code that return 2D array and get in the main function then passe it to another function and view the code. I am stuck in my assingment . plz help

here is the new code
but dont get output properly

// test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
const int rows=13;
const int coloums=6;
//char *begingStatus();
//void print(char val[][coloums]);
char **createBoard();
void freeBoard(char **board);

int _tmain(int argc, _TCHAR* argv[])
{
	cout <<"hi ";

    char **board=createBoard();
freeBoard(board);
	/*char **ptr;
	ptr=begingStatus();
	int nptrewarray[5]=ptr;
    print(ptr);*/


	cin.get();
	return 0;
}

char **createBoard()
{
    char **board=new char*[16];
    for (int i=0; i<16; i++)
    {
       board[i] = new char[10];
       for (int j=0; j<10; j++)
         board[i][j]='*';
    }

    return board;
}

void freeBoard(char **board)
{
    for (int i=0; i<16; i++)
      cout<< board[i];
   
}

//void print(char val[][coloums])
//{
//	//char *myval= new char[rows][coloums];
//	//myval=val;
//	for(int i=0; i<14;i++)
//	{
//		for(int j=0;j<7; j++)
//		{
//
//		cout << val[i][j];
//		cout << '\n';
//		}
//		
//	}
//
//}
//char *begingStatus()
//{
//	char *seats= new char[rows][coloums];
//	for(int i=0; i<rows;i++)
//	{
//		for(int j=0;j<coloums+1; j++)
//		{
//
//		seats[i][j]='*';
//		}
//		
//	}
//	return seats;
//	char **ptr = new char *[rows]
//    for(int i=0;i<rows;i++)
//       ptr[i]=new char[columns];
//}
//

i think the function freeboard() is not giving the right output..since char **board is a 2-Dimensional array shouldn't you be accessing the columns of each row like this

for (int i=0; i<16; i++)
    for(int j=0;j<10;j++)
	cout<< board[i][j];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.