473,811 Members | 4,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Nov 14 '05 #1
5 1441
Kutty Banerjee wrote:
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=&A;
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

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Nov 14 '05 #2
In article <c2**********@b igboote.WPI.EDU >,
"Kutty Banerjee" <ku****@wpi.edu > wrote:
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
Nov 14 '05 #3
Kutty Banerjee wrote:
hi ,
i ve
int (*Data)[4];
<snip>
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*D ata);
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.
Nov 14 '05 #4
On Tue, 2 Mar 2004 23:11:40 -0500, "Kutty Banerjee" <ku****@wpi.edu >
wrote:
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[i].

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>>
Nov 14 '05 #5
"Kutty Banerjee" <ku****@wpi.edu > wrote in message news:<c2******* ***@bigboote.WP I.EDU>...
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
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
12620
by: Peter Scheurer | last post by:
Hi, we found some strange behavior when operating with floats and round(). The following simplified statement reproduces the problem. select 6.56 - round(convert(float, 6.56), 2) from sysusers where name = 'public'; =========== -8.88178419
14
2726
by: Glen Able | last post by:
Should it be possible to create a custom class, 'Float', which would behave as a drop-in replacement for the builtin float type? As mentioned in another thread, I once tried this in rather a hurry to try and catch some floating point naughtiness, but was stumped in various places. Is there a fundamental obstacle to doing this? thanks, G.A.
2
32334
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
8
18893
by: Kenny ODell | last post by:
I do not know how to convert from a byte array to a float, and back again. I read data from a serial port into a byte (entire command structure which I parse). I am able to sift the data and isolate the information I want, in this case a structure that contains a bunch of floats and longs. So, lets say I have a byte containing the 4 bytes of info that are actually a float. In old C code, I would simple declare a pointer to the correct...
20
3150
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51 cm ---1 inch = 2.54/100 Meter ---1 yard = 3 feet ---1 feet = 12 inch
9
30527
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats. How can I convert the sets of 4 bytes to floats? Thanks, Greg Book
60
7240
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
6
8608
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar circumstances(XML file source) and with exactly the same value it gets it perfectly correct all the time. These are the results I got, XML is always correct, CSV are only incorrect for some of the values (above about 0.01) but always gives the...
3
10674
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience with this and if there is a better solution. -jeff
2
2019
by: Peter | last post by:
I have OLE Object field in Access Database. This field contains an array of floats. The array was moved from memory into a string and the string was saved as OLE Object in a database (That was written in VB6). This data was used to create graphs, instead creating a record for each point the entire array was saved in one field. How do I move the same data from database field in to float array suing C# ? Thank You
0
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.