freelanceprogrammers.org Forum Index » C
Re: [c-prog] Pointer2Array
Joined: 17 Jul 2003
Posts: 2
Re: [c-prog] Pointer2Array
Hello All,I have MS VC++ version 6.0.And when i try this i have this error.
#define COLS 3
#define ROWS 3
int (*p)[COLS];//need to dynamically allocate memory for ROWS
p = (int**)malloc(ROWS*COLS*sizeof(int));
ERROR:
error C2440: `=` : cannot convert from `int ** ` to `int (*)[3]` Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
So did`nt understand.
thanking all,
KittaArumita De <ar_frenz@...> wrote:
Actually i din`t undersatnd the question completely .tell me if i`m wrong .
Is the thing you what to do
#define COLS 4
#define ROWS 2
int (*p)[COLS];//need to dynamically allocate memory for ROWS
p = (int **)calloc(sizeof(int),COL);
/* but here you don`t want to use int ** for type casting is the problem ??
you can do it without type casting also it will warning only not error
*/
kitta techi <kitta_techi@...> wrote:
hello,
this a dumb question but run into compilation errors, all that i tried was using
pointer to an array.
#define COLS 4
#define ROWS 2
int (*p)[COLS];//need to dynamically allocate memory for ROWS
I did`nt want use the DOUBLE pointer method.
Grepped Google on this particular METHOD of POINTER to AN Array, lots of tutorial on 1)array of pointers 2)Double Pointers
but nothing on this one.Just found the explanation but not the usage, working on it..if if hit it i will publish the answer myself...
So someone shower some light on resources or the method itself?
thanking all,
Kitta
Do you Yahoo!?SBC Yahoo! DSL - Now only $29.95 per month! To unsubscribe, send a blank message to <mailto:c-prog-unsubscribe@yahoogroups.com>. Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
Do you Yahoo!?SBC Yahoo! DSL - Now only $29.95 per month! To unsubscribe, send a blank message to <mailto:c-prog-unsubscribe@yahoogroups.com>. Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
Joined: 07 Jul 2003
Posts: 14
Re: [c-prog] Pointer2Array
> int (*p)[COLS];//need to dynamically allocate memory for ROWS
>
> p = (int**)malloc(ROWS*COLS*sizeof(int));
>
> ERROR:
>
> error C2440: `=` : cannot convert from `int ** ` to `int (*)[3]`
> Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast
Dear friends ,
lets start from begining .
What pointer is , is it the simple locater of a memory
location ??
if a pointer is pointing to memory location AAAA:1234
so is the only inforamation
that this pointer contain ??
NO .
Every pointer has starting location and ending location
as well .
let me clear it .
Case 1 :
when you say
int *ptr=0xb0000000 ;
it tells to compiler taht ptr is a pointer & pointing to
TWO SUCESSIVE
BYTE (if size of int is 2 in VC++ it will be 4) FROM
MEMORY LOCATION 0xb0000000 .
that`s means when you say cout<<*ptr what it does is it
pick two( 4 in case of VC++) sucessive bytes from
0xb0000000 and then evaluate it .
Case 2 :
Lets if you have char ptr
char *ptr=0xb0000000 ;
it tells to compiler taht ptr is a pointer & pointing to
ONE SUCESSIVE
BYTE FROM MEMORY LOCATION 0xb0000000 .
that`s means when you say cout<<*ptr what it does is it
pick one byte from 0xb0000000 and then
evaluate it .
Case 3 :
when you say
double *ptr=0xb0000000 ;
it tells to compiler taht ptr is a pointer & pointing to
EIGHT SUCESSIVE
BYTE (if size of int is 8 ) FROM MEMORY LOCATION
0xb0000000 .
that`s means when you say cout<<*ptr what it does is it
pick eight
sucessive bytes from 0xb0000000 and then evaluate it .
Case 4 :
if you have a double pointer
double *ptr=0xb0000000 ;
and if you say
cout<<*ptr /* Evaluate 8 bytes as double from
0xb0000000 */
cout<<*(int*)ptr /* Evaluate 2(or 4 ) bytes as
integerfrom 0xb0000000 */
cout<<*(char*)ptr /* Evaluate 2(or 4 ) bytes as
integerfrom 0xb0000000 */
I hope the above info will help you in understanding
what type casting is .
and how pointer derefrences the refrence .
now lets discucuss you problem .
in you problem you have a pointer
int (*ptr)[4] ;
which is a pointer to array of 4 integer element .
so if you want allocate memmory to it the type casting
should be done as
ptr=(int (*) [4] ) memorry_allocating_function (....);
while you are casting it as (int**) this casting should
be done if you have
a pointer 2 pointer 2 integer value or else if you you
want to use this pointer to
access two dimensional array .
BTW which book you are using for pointers .
Correct me if i`m wrong .don`t get irritated for the
above lecture(case 1,2,3,4) if
you already knew it .
here is you code with little modification
#include<iostream.h>
#include<malloc.h>
void main()
{
int (*ptr)[4] ;
ptr = (int (*) [4])calloc(sizeof(int),4);
return;
}
-Arif
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







