Robby
2010-05-29 18:15:01 UTC
Hello,
I am porting my last module from the old MCU/compiler to the new
MCU/compiler and this is the only module that uses a double dimensional
array. Although this worked in the old compiler, now in the new one it
doesn't quite work and not to mention I am still a little confused on how to
go about assigning the innitial address of a two dimensional array to an
element in a structure... see below.
Here is a small example of the problem:
===============================
#include <stdio.h>
#include <malloc.h>
typedef struct tag_lb{
long *pmr; //<I need innitial address of 2 dim array stored here!
}lb;
lb* LB_create_lb (long pmr[][5])
{
lb *obj_lb = NULL;
obj_lb = (lb*) malloc(sizeof (struct tag_lb));
obj_lb->pmr = pmr; //<<< problem here!
return obj_lb;
}
long pmr[5][5] = { 195, 194, 193, 0, 0,
194, 193, 0, 0, 0,
195, 194, 193, 0, 0,
193, 194, 195, 91, 92,
195, 194, 193, 92, 91};
int main()
{
static lb *objLb1 = NULL;
objLb1 = LB_create_lb(pmr);
return 0;
}
========================================
I am getting the following error:
1>c:\_dts_programming\pic\_microchip_issues\simulated in
vc++\arrays_2dim\arrays_2dim\arrays_2dim\test1.cpp(14) : error C2440: '=' :
cannot convert from 'long [][5]' to 'long *'
If we want the address of the innitial location of pmr, then we can do
this... right?:
pmr OR &pmr[0][0]
Also, is casting from a long pmr[][5] to a long pointer the right thing to
do in this case? like this:
typedef struct tag_lb{
long *pmr;
}lb;
lb* LB_create_lb (long pmr[][5])
{
lb *obj_lb = NULL;
obj_lb = (lb*) malloc(sizeof (struct tag_lb));
obj_lb->pmr = (long*) pmr; ///<<< Cast from long to long *
return obj_lb;
}
For now I can't really change whats in the tag_lb structure since my code
uses pmr as a long pointer everywhere in my program.
Can someone confirm to me that the only thing I was missing is the cast?
All help appreciated. Thanking all in advance!
I am porting my last module from the old MCU/compiler to the new
MCU/compiler and this is the only module that uses a double dimensional
array. Although this worked in the old compiler, now in the new one it
doesn't quite work and not to mention I am still a little confused on how to
go about assigning the innitial address of a two dimensional array to an
element in a structure... see below.
Here is a small example of the problem:
===============================
#include <stdio.h>
#include <malloc.h>
typedef struct tag_lb{
long *pmr; //<I need innitial address of 2 dim array stored here!
}lb;
lb* LB_create_lb (long pmr[][5])
{
lb *obj_lb = NULL;
obj_lb = (lb*) malloc(sizeof (struct tag_lb));
obj_lb->pmr = pmr; //<<< problem here!
return obj_lb;
}
long pmr[5][5] = { 195, 194, 193, 0, 0,
194, 193, 0, 0, 0,
195, 194, 193, 0, 0,
193, 194, 195, 91, 92,
195, 194, 193, 92, 91};
int main()
{
static lb *objLb1 = NULL;
objLb1 = LB_create_lb(pmr);
return 0;
}
========================================
I am getting the following error:
1>c:\_dts_programming\pic\_microchip_issues\simulated in
vc++\arrays_2dim\arrays_2dim\arrays_2dim\test1.cpp(14) : error C2440: '=' :
cannot convert from 'long [][5]' to 'long *'
If we want the address of the innitial location of pmr, then we can do
this... right?:
pmr OR &pmr[0][0]
Also, is casting from a long pmr[][5] to a long pointer the right thing to
do in this case? like this:
typedef struct tag_lb{
long *pmr;
}lb;
lb* LB_create_lb (long pmr[][5])
{
lb *obj_lb = NULL;
obj_lb = (lb*) malloc(sizeof (struct tag_lb));
obj_lb->pmr = (long*) pmr; ///<<< Cast from long to long *
return obj_lb;
}
For now I can't really change whats in the tag_lb structure since my code
uses pmr as a long pointer everywhere in my program.
Can someone confirm to me that the only thing I was missing is the cast?
All help appreciated. Thanking all in advance!
--
Best regards
Roberto
Best regards
Roberto