C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-02-2009, 12:02 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 5
Smile How to change Array Data type?

Hello, I was wondering if there was a way to change a array full of float values to an array of double values and then reference the new array to the value of a pointer?
Thank You
LennoZebra is offline   Reply With Quote
Old 10-02-2009, 12:09 PM   #2
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Quote:
Originally Posted by LennoZebra View Post
Hello, I was wondering if there was a way to change a array full of float values to an array of double values
Yep! it's called a cast.
Quote:
Originally Posted by LennoZebra View Post
and then reference the new array to the value of a pointer?
Not sure what you mean by the above?
itCbitC is offline   Reply With Quote
Old 10-02-2009, 12:18 PM   #3
Registered User
 
Join Date: Oct 2009
Posts: 5
I used a FOR loop to cast each variable in the array up to a data type double. But when I try and set the array to the pointer like:
*vrp = hab2;

where hab2 is a array of 300 units, it tells me that they are incompatible data types?
Thank You
LennoZebra is offline   Reply With Quote
Old 10-02-2009, 12:35 PM   #4
Registered User
 
Join Date: Sep 2008
Location: Toronto, Canada
Posts: 507
No. Since every array element uses 4 byes for float vs. 8 bytes for double, you can not cast the entire array at once.
nonoob is offline   Reply With Quote
Old 10-02-2009, 12:45 PM   #5
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Do you want a copy of the values in a separate array, or do you want both arrays to point to the same place? If the later, there is no point, since a double and a float are stored differently.

You should post that code that you tried.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 10-02-2009, 12:47 PM   #6
Epy
I like turtles
 
Join Date: Sep 2009
Location: Ohio
Posts: 179
Quote:
Originally Posted by LennoZebra View Post
Hello, I was wondering if there was a way to change a array full of float values to an array of double values and then reference the new array to the value of a pointer?
Thank You
Technically, an array is a pointer...a pointer to the beginning of the array.
For example, a[2] is the same as *(a+2)

anyway...
Code:
float a[4];
double b[4];
int i;
for (i = 0; i < 4; i++)
     b[i] = (double)a[i];
__________________
Jake, that CAD guy
Hazudra Fodder
Epy is offline   Reply With Quote
Old 10-02-2009, 01:27 PM   #7
Registered User
 
Join Date: Oct 2009
Posts: 5
Here is the code that I am having problems with. Notice


Code:
     #include "config.h"
     
     #include "mex.h"

     #include "f77-fcn.h"
     
     extern void F77_FUNC (contrl, CONTRL) 
      (float *phi, float *delta);

     void
     mexFunction (int nlhs, mxArray* plhs[], int nrhs, 
     	     const mxArray* prhs[])
     {
       mwIndex i;
       mwSize n;
       double *vri, *vro;
       double hab1, hab2[2];
       float heg1, heg2[2];
       float guo[300];
       int k;
       
       if (nrhs != 1 || ! mxIsNumeric (prhs[0]))
         mexErrMsgTxt ("expects matrix");
     
       n = mxGetNumberOfElements (prhs[0]);
       plhs[0] = (mxArray *) mxCreateNumericArray 
         (mxGetNumberOfDimensions (prhs[0]),
          mxGetDimensions (prhs[0]), mxGetClassID (prhs[0]),
          mxIsComplex (prhs[0]));
       vri = mxGetPr (prhs[0]);
       vro = mxGetPr (plhs[0]);
     
       if (mxIsComplex (prhs[0]))
         {
           double *vii, *vio;
           vii = mxGetPi (prhs[0]);
           vio = mxGetPi (plhs[0]);
     
           for (i = 0; i < n; i++)
     	{
     	  vro [i] = vri [i] * vri [i] - vii [i] * vii [i];
     	  vio [i] = 2 * vri [i] * vii [i];
     	}
         }
       else
         {

	   hab1 = *vri;
	   heg1 = (float)hab1;

	   F77_FUNC (contrl, CONTRL) (&heg1,(float *)guo);
	   heg2 [1] = guo[13];
	   heg2 [2] = guo[20];
           //***** Important Part*********
	   for (k = 0; k < 3; k++)
	     vro[k] = (double)heg2[k]; //This Line is Having PROBLEMS
          //I create the mex file using Octave and this part does not work
	   
         }
     }
I'm integrating MATLAB, C and FORTRAN together through a Mex function, and thus using API's to get information to and from FORTRAN and MATLAB.

C still has to abide by all the normal rules inside this wrapper. But using the code above or:

Code:
 //***** Important Part*********
	   for (k = 0; k < 3; k++)
	     hab2[k] = (double)heg2[k]; 
          *vro = hab2; //This Line is Having PROBLEMS
still leaves me with problems.

*****I am able to cast the array properly, but I am having problems assigning the array to the value of the pointer*****
Thank you for your help
LennoZebra is offline   Reply With Quote
Old 10-02-2009, 01:37 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
What kinds of problems are you having? Specifically.
tabstop is offline   Reply With Quote
Old 10-02-2009, 02:09 PM   #9
Registered User
 
Join Date: Oct 2009
Posts: 5
if I use :
Code:
 //***** Important Part*********
	   for (k = 0; k < 3; k++)
	     vro[k] = (double)heg2[k]; //This Line is Having PROBLEMS
          //I create the mex file using Octave and this part does not work
Then MATLAB or Octave both Crash while making the mex file


If I use :
Code:
//***** Important Part*********
	   for (k = 0; k < 3; k++)
	     hab2[k] = (double)heg2[k]; 
          *vro = hab2; //This Line is Having PROBLEMS
The programs tell me: error: incompatible types in assignment


The wrapper can send out a single value very well, but I need a array to come out of the wrapper.

Thank You
LennoZebra is offline   Reply With Quote
Old 10-02-2009, 02:27 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
So having looked up all these mxfunctions, if you want an array of doubles, shouldn't you create an array of doubles with your plhs[0] thing? It looks like you are not.
tabstop is offline   Reply With Quote
Old 10-02-2009, 03:17 PM   #11
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
*vro = hab2; //This Line is Having PROBLEMS


That line should be:

Code:
vro = hab2;
* dereferences the pointer.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 10-02-2009, 03:46 PM   #12
Registered User
 
Join Date: Oct 2009
Posts: 5
Awesome! Thanks Guys. No crashing anymore and the function is now outputting a array of zeros. I'm happier :-). I think there may be another problem with how I am declaring the mxCreateDoubleMatrix, but thats not a C issue.

Thank You so Much for all your help! Hope everyone have a nice weekend
LennoZebra is offline   Reply With Quote
Reply

Tags
array, pointers

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
parent in a binary search tree roaan C Programming 4 08-26-2009 07:08 PM
Problem in compiling targa2.c: error: field `ip' has incomplete type.. Moony C Programming 0 03-20-2008 07:59 AM
failure to import external C libraries in C++ project nocturna_gr C++ Programming 3 12-02-2007 03:49 PM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
change array data type panfilero C Programming 2 10-16-2005 04:52 AM


All times are GMT -6. The time now is 11:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22