Can anyone explain me pointers to 2D array ?? I have to input array of string using and read them in another function but I am having a REALLY TOUGH time with pointersan anyone explin it to me or refer good guide to pointers ?? I am using borlnd c++ 3.0v.

Recommended Answers

All 10 Replies

C++?
2D arrays of char?

What about a reference to a std::vector of std::string ?

Easy, and safe!

is it ll seeming greek and latin.

char axx0 [20][30];

I wan to input this in pointer form the pass it to another fucntion which can read it and manipulate the 30 strings.

I have no clue where to start from.

All above has to be done in pointer form.... thats a requirement.

I think you need to be more concise with the requirements.

You have 30 strings correct? Does this mean that they each cannot be more than 20 characters long (including the null terminator)?

You must make your char[20][30] (or most likely char[30][20]) in pointer form, then convert it into an array of strings, or vice versa?

What are the required functions to use in this project?

First of all, if you want 30 strings of up to 20 characters each, then your declaration should be the other way around, and you need an extra char for each null terminator (the character that ends a string):

char axx0[30][21]; // 30 strings of up to 20 character

Now for a little lesson on arrays/pointers. If you have a normal 1D array such as this:

char myString[10];

This declaration does a few things... it makes a pointer to char (type is char*), allocates space for 10 chars somewhere in memory, and then makes the pointer point to the first char in that allocated space. The pointer is named myString. So really, myString is of type char*, a pointer to char. Now when you do something like

myString[2] = 'A';

, what happens is it dereferences the myString pointer, moves 2 positions in memory, and puts the character 'A' there. Essentially myString[0] is the same as *myString, and myString[2] would be the same as *(myString + 2);

Now, when we move to 2D arrays, something similar happens. When we declare

char axx0[30][21];

, it makes a pointer to a pointer of char (type is char**) which is the variable axx0. Space is allocated for 30 pointers to char (char*), and axx0 will point to the first of those. Then, space is allocated for 21 characters for each pointer to char that was created, each pointer points to the first character of the corresponding 21 character sequence in memory.

So, getting to the point, axx0 is a char**. If you want to send the 2D array to a function, you simply send axx0. The function should look like one of the following:

void myFunction( char myArrayOfStrings[30][21] );

OR

void myFunction( char myArrayOfStrings[30][] );

When accepting a multi-dimensional array as a parameter to a function, you must at LEAST include the size of each dimension except the last. You have the option of including the size of the last dimension if you wish.

> When accepting a multi-dimensional array as a parameter to a function, you must at
> LEAST include the size of each dimension except the last. You have the option of
> including the size of the last dimension if you wish.
Close. It's the first one which is optional, not the last.

but How do I read it ?? like I pass the pointer to another function to echo the data.

void Echo_dat ( char a[30][21],OR char* a[30][21] ?)

{

I want to read the string till it detects the string END.

}

will the normal nested for loop work for reading ??

Plz suggest me a good guide to pointers, I have my practical in around 2 weeks.

The main program is :-

/*==========================|| PRG-7 ||===================================
  | Name - Atishay Kiran
  | Class- XII - A
  | Program info. -
  |
  |                 i) Program to Display
  |
  |				      \	  Upper lalf of left diagonal
  |				      \	  Lower half of left diagonal
  |				      \	  sum of each row
  |                                   \   sum of each colum
  |                                   \   sum of left diagonal
  |                                   \   sum of right diagonal
  |
  | Date 12th August
  |
==========================================================================*/

#include<iostream.h>
#include<conio.h>
#include<process.h>


class matrix
{
  private:
	int M[][20],row,col;

  public:
	char*     sum_r();
	char*     sum_c();
	int      sum_d();
	int      input();
	void	 display();
	void 	 diag_up();
	void	 diag_dn();
};
//=========================================================================

int matrix :: input()
{
 cout<<"Input the number of rows in you matrix -> \n";
 cin>>row;

 cout<<"Input the number of colums in your matrix -> \n";
 cin>>cin;

 clrscr();

  for(int i=0;i<row;i++)
  {
	for(int j=0;j<col;j++)
	  {
	     cout<<"Enter x= "<<i<<" y= "<<j<<" for the array -> ";
	      cin>>M[i][j];
	  }
  }
  cout<<"\n\n Martrix Sucessfully Inputted !! \n";
}

/*=========================================================================
		   Sum of Rows

  The function will store the values of the sum of each row of the array
  in a CHAR 2D array. The End of file will be detected by a string 'end'.

  In a seperate non-member function to the class, the CHAR array will be
  passed and each string will be converted to a number using ATOI.
==========================================================================*/

char* sum_r ()
{
 char *a[20];

tHE PROBLES IS GIVING THE SUM OF EACH Row and coloum, TO THE USER.

Hello, this is a small suggestion for your problem.
This (the code is my solution to similar problem) is recommended in Stroustrup (C++ program language) book, about passing 2d arrays to functions. Try it and I hope it will help you with your problem.

#include <iostream>

void print(int* first, int dim1, int dim2)  // pointer to first element of array; dim1, dim2 -                  
                                                         // dimensions of array
{
	for (int i = 0; i < dim1; i++)
	{
		for (int j = 0; j < dim2; j++)
		{
			std::cout << first[i*dim2 + j] << "\t"; 
		}
		std::cout << "\n";
	}
}
int main()
{
	const int dim1 = 3;
	const int dim2 = 5;
	int v[dim1][dim2] = {{1, 2, 3, 4, 5}, {3, 4, 5, 6, 7}, {6, 7, 8, 9, 10}};
	print(&v[0][0], dim1, dim2);
}

> int M[][20],row,col;
You need to specify a size here.

> char* sum_r ()
Since you can't return arrays (not without dynamic allocation, or trickery, which you're not ready for), the easiest thing to do would be to pass the array where you want the answer stored as a parameter.

Eg. sum_r ( char answers[21][10] ); The main() call would be simply

int main ( ) {
  char answers[21][10];
  matrix var;
  // some other operations on var
  var.sum_r ( answers );
}

> The End of file will be detected by a string 'end'.
This is why it's 21

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.