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.