Re: Storing an array's subscript into a structure's member
Scott,
You seem to be trying to strip away the essential "width [5]" information
I understand now what you meant by this.
So I will use pointer arithmetic.... I don't know if this is okay to do it
this way, but it sure does compile without error or warnings and it does work:
===============================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tag_lb
{
long *pmr;
}lb;
lb* create_obj(long pmr[][5])
{
lb *ptr_lb = NULL;
ptr_lb = malloc (sizeof *ptr_lb);
ptr_lb->pmr = (long *) pmr;
return ptr_lb;
}
void f1(lb *pObj)
{
long t;
t = * (pObj->pmr);
t = *((pObj->pmr)+1);
t = *((pObj->pmr)+2);
//...
}
int main()
{
lb *pObj = NULL;
long pmr[][5] = { {195, 194, 193, 111, 111 },
{194, 195, 112, 222, 111 } };
pObj = create_obj(pmr);
f1(pObj);
free(pObj);
return 0;
}
=================================
The basic difference is that I type casted the prm type to a long* type.
Thanks for your help!
--
Best regards
Roberto
"Scott McPhillips [MVP]" wrote:
"Robby" <Robby@discussions.microsoft.com> wrote in message
news:E6E59503-0F4E-4B3F-9D75-4AAE219DB2B5@microsoft.com...
Hello,
I am trying to store an array's subscript value into a structure's member
pointer.
An array does not have a "subscript value." You seem to mean an array's
base address. Still struggling with pointer/array fundamentals, eh?
... ... ...
lb* create_obj(long pmr[][5])
{
lb *ptr_lb = NULL;
ptr_lb = malloc (sizeof *ptr_lb);
ptr_lb->p = pmr; << errors point here
return ptr_lb;
}
The types are incompatible.
ptr_lb->p is a pointer to long.
pmr is a pointer to a 2-dimensional array of width [5].
apples != oranges
You seem to be trying to strip away the essential "width [5]" information
and then hoping you can somehow index into the 2-dimensional array without
that essential information.
--
Scott McPhillips [VC++ MVP]