pointer to 2D array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 163
Reputation: atish00 is an unknown quantity at this point 
Solved Threads: 4
atish00 atish00 is offline Offline
Junior Poster

pointer to 2D array

 
0
  #1
Aug 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: pointer to 2D array

 
0
  #2
Aug 22nd, 2008
C++?
2D arrays of char?

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

Easy, and safe!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 163
Reputation: atish00 is an unknown quantity at this point 
Solved Threads: 4
atish00 atish00 is offline Offline
Junior Poster

Re: pointer to 2D array

 
0
  #3
Aug 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 163
Reputation: atish00 is an unknown quantity at this point 
Solved Threads: 4
atish00 atish00 is offline Offline
Junior Poster

Re: pointer to 2D array

 
0
  #4
Aug 22nd, 2008
All above has to be done in pointer form.... thats a requirement.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: pointer to 2D array

 
0
  #5
Aug 22nd, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: pointer to 2D array

 
0
  #6
Aug 22nd, 2008
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):
  1. 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:
  1. 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
  1. 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
  1. 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:
  1. void myFunction( char myArrayOfStrings[30][21] );
OR
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: pointer to 2D array

 
0
  #7
Aug 23rd, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 163
Reputation: atish00 is an unknown quantity at this point 
Solved Threads: 4
atish00 atish00 is offline Offline
Junior Poster

Re: pointer to 2D array

 
0
  #8
Aug 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 163
Reputation: atish00 is an unknown quantity at this point 
Solved Threads: 4
atish00 atish00 is offline Offline
Junior Poster

Re: pointer to 2D array

 
0
  #9
Aug 23rd, 2008
The main program is :-

  1. /*==========================|| PRG-7 ||===================================
  2.   | Name - Atishay Kiran
  3.   | Class- XII - A
  4.   | Program info. -
  5.   |
  6.   | i) Program to Display
  7.   |
  8.   | \ Upper lalf of left diagonal
  9.   | \ Lower half of left diagonal
  10.   | \ sum of each row
  11.   | \ sum of each colum
  12.   | \ sum of left diagonal
  13.   | \ sum of right diagonal
  14.   |
  15.   | Date 12th August
  16.   |
  17. ==========================================================================*/
  18.  
  19. #include<iostream.h>
  20. #include<conio.h>
  21. #include<process.h>
  22.  
  23.  
  24. class matrix
  25. {
  26. private:
  27. int M[][20],row,col;
  28.  
  29. public:
  30. char* sum_r();
  31. char* sum_c();
  32. int sum_d();
  33. int input();
  34. void display();
  35. void diag_up();
  36. void diag_dn();
  37. };
  38. //=========================================================================
  39.  
  40. int matrix :: input()
  41. {
  42. cout<<"Input the number of rows in you matrix -> \n";
  43. cin>>row;
  44.  
  45. cout<<"Input the number of colums in your matrix -> \n";
  46. cin>>cin;
  47.  
  48. clrscr();
  49.  
  50. for(int i=0;i<row;i++)
  51. {
  52. for(int j=0;j<col;j++)
  53. {
  54. cout<<"Enter x= "<<i<<" y= "<<j<<" for the array -> ";
  55. cin>>M[i][j];
  56. }
  57. }
  58. cout<<"\n\n Martrix Sucessfully Inputted !! \n";
  59. }
  60.  
  61. /*=========================================================================
  62. Sum of Rows
  63.  
  64.   The function will store the values of the sum of each row of the array
  65.   in a CHAR 2D array. The End of file will be detected by a string 'end'.
  66.  
  67.   In a seperate non-member function to the class, the CHAR array will be
  68.   passed and each string will be converted to a number using ATOI.
  69. ==========================================================================*/
  70.  
  71. char* sum_r ()
  72. {
  73. char *a[20];

tHE PROBLES IS GIVING THE SUM OF EACH Row and coloum, TO THE USER.
Last edited by atish00; Aug 23rd, 2008 at 5:31 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: fishky is an unknown quantity at this point 
Solved Threads: 0
fishky fishky is offline Offline
Newbie Poster

Re: pointer to 2D array

 
0
  #10
Aug 24th, 2008
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.


  1. #include <iostream>
  2.  
  3. void print(int* first, int dim1, int dim2) // pointer to first element of array; dim1, dim2 -
  4. // dimensions of array
  5. {
  6. for (int i = 0; i < dim1; i++)
  7. {
  8. for (int j = 0; j < dim2; j++)
  9. {
  10. std::cout << first[i*dim2 + j] << "\t";
  11. }
  12. std::cout << "\n";
  13. }
  14. }
  15. int main()
  16. {
  17. const int dim1 = 3;
  18. const int dim2 = 5;
  19. int v[dim1][dim2] = {{1, 2, 3, 4, 5}, {3, 4, 5, 6, 7}, {6, 7, 8, 9, 10}};
  20. print(&v[0][0], dim1, dim2);
  21. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC