Thread: How to change Array Data type?

  1. #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

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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?

  3. #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

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No. Since every array element uses 4 byes for float vs. 8 bytes for double, you can not cast the entire array at once.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    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];

  7. #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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What kinds of problems are you having? Specifically.

  9. #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

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    *vro = hab2; //This Line is Having PROBLEMS


    That line should be:

    Code:
    vro = hab2;
    * dereferences the pointer.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parent in a binary search tree
    By roaan in forum C Programming
    Replies: 4
    Last Post: 08-26-2009, 07:08 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. change array data type
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 10-16-2005, 04:52 AM

Tags for this Thread