943,876 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5593
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 22nd, 2008
0

pointer to 2D array

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 4
Junior Poster
atish00 is offline Offline
163 posts
since Dec 2007
Aug 22nd, 2008
0

Re: pointer to 2D array

C++?
2D arrays of char?

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

Easy, and safe!
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 22nd, 2008
0

Re: pointer to 2D array

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.
Reputation Points: 10
Solved Threads: 4
Junior Poster
atish00 is offline Offline
163 posts
since Dec 2007
Aug 22nd, 2008
0

Re: pointer to 2D array

All above has to be done in pointer form.... thats a requirement.
Reputation Points: 10
Solved Threads: 4
Junior Poster
atish00 is offline Offline
163 posts
since Dec 2007
Aug 22nd, 2008
0

Re: pointer to 2D array

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?
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 22nd, 2008
0

Re: pointer to 2D array

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):
c++ Syntax (Toggle Plain Text)
  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:
c++ Syntax (Toggle Plain Text)
  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
c++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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:
c++ Syntax (Toggle Plain Text)
  1. void myFunction( char myArrayOfStrings[30][21] );
OR
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Aug 23rd, 2008
0

Re: pointer to 2D array

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 23rd, 2008
0

Re: pointer to 2D array

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.
Reputation Points: 10
Solved Threads: 4
Junior Poster
atish00 is offline Offline
163 posts
since Dec 2007
Aug 23rd, 2008
0

Re: pointer to 2D array

The main program is :-

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 4
Junior Poster
atish00 is offline Offline
163 posts
since Dec 2007
Aug 24th, 2008
0

Re: pointer to 2D array

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.


C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fishky is offline Offline
7 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Segmentation Fault.... pthread, classes, STL Containers...
Next Thread in C++ Forum Timeline: Alternatives to winsock?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC