float (*Data)[4] vs float *Data[4]

K

Kutty Banerjee

hi ,
i ve
int (*Data)[4];
int A[4]={1,2,3,4};

Data=&A;
//Question here, how d i dynamically allocate space for Data using calloc?

printf("%d\n",*Data[0]) ; //gives 1
//But printf("%d\n",*Data[1]) produces weird value.

Can somebody please tell me what is the correct way to dynamically allocate
space for int (*Data)[4] as also to access the in dividual elements of the
array that it points to.

thanks

kutty
 
D

David Rubin

Kutty said:
hi ,
i ve
int (*Data)[4];

Data is a pointer to an array 4 of int.
int A[4]={1,2,3,4};

A is an array 4 of int.

Data points to A.
//Question here, how d i dynamically allocate space for Data using calloc?

I'm not sure what you are asking or why you are asking it.
printf("%d\n",*Data[0]) ; //gives 1
//But printf("%d\n",*Data[1]) produces weird value.

Try

printf("%d\n", (*Data)[1]);

Since [] has higher precedence than *, you are dereferencing an invalid
pointer. Data points to A, so Data[0], or *Data, is the same as A. *A,
which is the same as A[0], or *Data[0], or **Data, is 1. OTOH, Data[1]
is some unknown value, say v, and *v is...undefined.

/david
 
F

Francois Grieu

Kutty Banerjee said:
int (*Data)[4];
int A[4]={1,2,3,4};
Data=&A;
printf("%d\n",*Data[0]) ; //gives 1
printf("%d\n",*Data[1]) ; //produces weird value.

problem is that * and [] are evaluated right to left,
when you want left to right.

printf("%d\n",(*Data)[1]) ; //gives 2


François Grieu
 
S

sellountos euripides

Kutty said:
hi ,
i ve
int (*Data)[4];
said:
Can somebody please tell me what is the correct way to dynamically allocate
space for int (*Data)[4] as also to access the in dividual elements of the
array that it points to.

Data is a pointer to an array of int 4.
So...

/* Allocates the appropriate space for 4 integers */
Data = malloc(sizeof*Data);


Now Data points to a malloced space for 4 integers.
You can access them
(*Data)[0] = 1;
(*Data)[1] = 2;
e.t.c.

The calloc works similar.

e.j.s

Any correction is welcome.

cheers.
 
B

Barry Schwarz

hi ,
i ve
int (*Data)[4];

Data is a pointer to an array of 4 int.
int A[4]={1,2,3,4};

Data=&A;
//Question here, how d i dynamically allocate space for Data using calloc?

printf("%d\n",*Data[0]) ; //gives 1

Since [] has higher precedence than *, the second argument is
evaluated as *(Data[0]). Data[0] is the first object Data points to,
namely A. A is an array of 4 int. *(Data[0]) has the same meaning as
*A which has the same meaning as A[0] which is the first int in the
array A, which you initialized to 1.
//But printf("%d\n",*Data[1]) produces weird value.

Data[1] is the "second" object Data points to. It points to a
non-existent array immediately following A. Attempting to dereference
this invokes undefined behavior.

If you want to use Data to access elements in A using the existing
definition, you can code
(*Data)[1]
This causes *Data to be evaluated first producing the same result as
Data[0] above, namely A. Then (*Data)[1] is exactly equivalent to
A[1] which is the second element of the array A, which you initialized
to 2.

Alternately, you could define Data as
int *Data;
initialize it with
Data = A;
and then access the elements in A with Data.
Can somebody please tell me what is the correct way to dynamically allocate
space for int (*Data)[4] as also to access the in dividual elements of the
array that it points to.

To allocate space for a pointer to point to a single object of the
appropriate type (in this case the object is an array of 4 int), you
can always use
Data = malloc(sizeof *Data);

If you want to allocate space for n objects, then use
Data = malloc(n * size of *Data);

This will work for any type of pointer except pointers to function.
thanks

kutty



<<Remove the del for email>>
 
M

mehul raval

Kutty Banerjee said:
hi ,
i ve
int (*Data)[4];
int A[4]={1,2,3,4};

Data=&A;
//Question here, how d i dynamically allocate space for Data using calloc?

printf("%d\n",*Data[0]) ; //gives 1
//But printf("%d\n",*Data[1]) produces weird value.

Can somebody please tell me what is the correct way to dynamically allocate
space for int (*Data)[4] as also to access the in dividual elements of the
array that it points to.

thanks

kutty

hi ,
regarding ur query as to print the individual elements here is the
poss way to do it. # include <stdio.h>

void main()
{
int (*Data)[4];
int A[4]={1,2,3,4};
Data=&A;

/* A is a pointer to an array of 4 int.*/

printf(" %d %d",*((*Data)),*((*Data)+1));
return;
}
this prog will print both the elements and similarly all the elements
can be printed with the format *((*Data)+ i). in the prog u had
initalized Data to be a ptr to a 2D array with the no of columns
specified but the no rows werent. *Data pts to the 1st row in such
case and then other rows can be accessed by *(Data)+ (rowno) and llly
the contents by *(*(Data)+ (rowno)),thats the format.
Hope it helps.
mehul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top