Re: Storing an array's subscript into a structure's member
Hello Alex,
I think that 2.10 and 2.11 of your link is what I wanted to acheive
yesterday. This describes a way to simulate array subscripting
"subscript[x][y]" format instead of pointer arithmetic as mentioned in 2.11:
{ ... ary[i][j] is really aryp[i * ncolumns + j] ... }
But, in my sample, I am storing the array's base address in a member
variable of a structure before reusing it to access the array's data in f1().
So, I tried to implement the notes of 2.10 and 2.11 so it would fit into my
sample, and I did get it to compile but with warnings. I think the warnings
can't be ignored in this case because I am obviously doing something wrong.
I have been looking at this for a while now and I think I just will stay
with the type cast solution since there is no errors or warnings. I am quite
happy with that one for now, however here is the last attempt that I tried
using the notes of 2.10 and 2.11. I don't know what else to try. Eventually,
I hope this stuff will sink in. :-)
===============================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tag_lb
{
long (*pmr)[5];
}lb;
lb* create_obj(long (*pmr)[5])
{
lb *ptr_lb = NULL;
ptr_lb = malloc (sizeof *ptr_lb);
ptr_lb->pmr = pmr;
return ptr_lb;
}
void f1(lb *pObj)
{
long t;
//t = pObj->pmr[0][0];
}
int main()
{
lb *pObj = NULL;
long pmr[][5] = { {195, 194, 193, 111, 111 },
{194, 195, 112, 222, 111 } };
pObj = create_obj(&pmr[0][0]);
f1(pObj);
free(pObj);
return 0;
}
===============================
Here are the two warnings:
1>c:\_dts_programming\c_programming\c\c_tests\c_string_samples\misc_c_samples\assignarradd2.c(40)
: warning C4047: 'function' : 'long (*)[5]' differs in levels of indirection
from 'long *'
1>c:\_dts_programming\c_programming\c\c_tests\c_string_samples\misc_c_samples\assignarradd2.c(40)
: warning C4024: 'create_obj' : different types for formal and actual
parameter 1
Alex, I sincerely thankyou for your help !
--
Best regards
Roberto
"Alex Blekhman" wrote:
"Robby" wrote:
The basic difference is that I type casted the prm type to a
long* type.
Yes, cast will silent the compiler, but you'd better fix the
types, so the problem won't bite you at the runtime. Here's good
overview of arrays and pointers in C:
"Section 2. Arrays and Pointers"
http://www.lysator.liu.se/c/c-faq/c-2.html
Alex