Re: [CPPForum] Pointers and Arrays

2 views
Skip to first unread message
Message has been deleted

Ananth G

unread,
Nov 17, 2004, 7:16:55 AM11/17/04
to cppf...@googlegroups.com
1)
typedef struct
{
int **matrix;
} MATRIX;

int main()
{
MATRIX *test =(MATRIX *) malloc( sizeof(MATRIX));
//5 is the row dimension
test->matrix = (int *) malloc( 5 * sizeof(int*) );
//lets assume that each row will contain 10 elements
for (i=0;i<5;i++) {
test->matrix[i] = (int *) malloc( 5 * sizeof(int) );
}
//assign some value
for (i=0;i<5;i++) {
for (j=0;j<5;j++) {
test->matrix[i][j] = 1;
}
//print the 2d array...
.....
......
// If im rite the above code should work . pls correct me if im wrong
2)
you cannot and should not use a char * directly for storing a string of chars.
use malloc allocate memory and then store the chars in it

char * strBlah;
strBlah = (char *) malloc( 100); //size of ur string
strcpy( strBlah , "hello world"); //make sure its not greater than
the size of allocated memory or else you will have a buffer overflow.


On Wed, 17 Nov 2004 17:41:38 +0530, Ananth G <anan...@gmail.com> wrote:
> 1)
> typedef struct
> {
> int **matrix;
> } MATRIX;
>
> int main()
> {
> MATRIX *test =(MATRIX *) malloc( sizeof(MATRIX));
> //5 is the row dimension
> test->matrix = (int **) malloc( 5 * sizeof(int**) );
> //lets assume that each row will contain 5 elements
> for (i=0;i<5;i++)
>
>
>
> On Wed, 17 Nov 2004 02:34:01 -0800, joshualimm <joshu...@gmail.com> wrote:
> >
> > I think I know my arrays well enough. But I can't seem to get this
> > pointer and array thingy.
> >
> > >From what I've learnt, an array is actually a pointer, and each element
> > is accessed via the [], instead of the *. That means, a[4] is the same
> > as *(a+1). And for 2d arrays, a[4][5] is the same as *(*(a+4)+5)
> >
> > Now here's my question:
> >
> > say I have this data type:
> >
> > typedef struct
> > {
> > int **matrix;
> > } MATRIX;
> >
> > how would I set the dimension of the "matrix" in my main?
> >
> > int main()
> > {
> > MATRIX *test = new MATRIX;
> > test->matrix??
> >
> > do I just go : test.matrix[dimX][dimY]? Or is this array extendable,
> > such that I can just access/modify any element I want?
> >
> > Usually, when I use array of chars for my string, I'd just go:
> >
> > char *strBlah;
> >
> > and not care about the size. But is there a better way of programming
> > this?
> >
> > Thanks!
> >
> >
>
>
> --
> If you can dream IT. You can do IT.
>


--
If you can dream IT. You can do IT.

joshualimm

unread,
Nov 17, 2004, 8:01:18 AM11/17/04
to CppF...@googlegroups.com
Hi, and thanks for the reply. Thanks for the tips on constructing the
rows first, followed by the individual constructor. I've managed to get
it working using "new". But can someone please help me check if it is
valid? Thanks a lot!


MATRIX *a = new MATRIX;
(int *)a->matrix = new int[ROWS];
for (int i=0; i<3; i++)
{
a->matrix[i] = new int[COLS];
}

Ananth G

unread,
Nov 17, 2004, 7:11:38 AM11/17/04
to cppf...@googlegroups.com

Venkatesh Raja

unread,
Nov 18, 2004, 5:30:57 AM11/18/04
to cppf...@googlegroups.com
#include<iostream>

struct matrix
{
int **base;
};

int main()
{
matrix *m = new matrix();
m->base = new int*[3];

for(int i=0;i<3;i++)
m->base[i] = new int[3];

for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
m->base[i][j] =1;

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
std::cout<<m->base[i][j]<<"\t";
std::cout<<"\n";
}

delete m->base;
delete m;
return 0;

}


I think this would help...

Regards,
Venkat.




On Wed, 17 Nov 2004 17:41:38 +0530, Ananth G <anan...@gmail.com> wrote:
>
--

********** IT is everywhere. **************
Venkatesan. R
Reply all
Reply to author
Forward
0 new messages