| | |
pointer to 2D array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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?
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?
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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):
Now for a little lesson on arrays/pointers. If you have a normal 1D array such as this:
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 , 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 , 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:
OR
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.
c++ Syntax (Toggle Plain Text)
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)
char myString[10];
c++ Syntax (Toggle Plain Text)
myString[2] = 'A';
Now, when we move to 2D arrays, something similar happens. When we declare
C++ Syntax (Toggle Plain Text)
char axx0[30][21];
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)
void myFunction( char myArrayOfStrings[30][21] );
c++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Dec 2007
Posts: 163
Reputation:
Solved Threads: 4
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.
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.
•
•
Join Date: Dec 2007
Posts: 163
Reputation:
Solved Threads: 4
The main program is :-
tHE PROBLES IS GIVING THE SUM OF EACH Row and coloum, TO THE USER.
c++ Syntax (Toggle Plain Text)
/*==========================|| 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.
Last edited by atish00; Aug 23rd, 2008 at 5:31 am.
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
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.
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)
#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); }
![]() |
Similar Threads
- Pointer to array (C++)
- pointer and array arithmetic (C)
- Declaration of dynamic pointer array puzzle. (C)
- structures containing a pointer to an array (C++)
- writing an array to a file (C++)
- pointer (C)
- Array limit (C)
- struct dynamic 2d array alloc (C)
Other Threads in the C++ Forum
- Previous Thread: Segmentation Fault.... pthread, classes, STL Containers...
- Next Thread: Alternatives to winsock?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






