LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling Multiple mains

ok guys i  tried one of my old school tricks that i forgot about, i had go back to my college notes lololol.

 

i did it this way.

 

 

what do you think?   you think i did well that time?

 

 

*****************************************

 

 

#include <stdio.h>


long f_ConvDblToSingleArray ( double** double_array, double* single_array , int index1, int index2);


int main(int argc, char* argv[])
{
    int q=4,u=1000;
    double temp_data[4][1000] ={0};
    double conv_data[4][1000] ={0};

    double meas[4000] ={0};


    f_ConvDblToSingleArray( (double**)(&temp_data), (double*)(&meas), q, u );
    f_ConvDblToSingleArray( (double**)(&conv_data), (double*)(&meas), q, u );

    conv_data[q][u]= temp_data[q][u];


    return 0;
}



//Library Function...(This compiles...)
long f_ConvDblToSingleArray ( double** double_array, double* single_array , int index1, int index2)
{

    long error = 0;

    int idx1 = 0;
    int idx2 = 0;
    int index = 0;
//    double array1[4][1000];
//    double array2[4000];

    //array1 = (double[4][1000])double_array;
    //array2 = (double[4000])single_array;

    for (idx1=0; idx1 < index1; idx1++)
    {
        for ( idx2=0; idx2 < index2; idx2++ )
        {
           single_array[index++] = double_array[idx1][idx2];
        }
    }

    return 0;

}

0 Kudos
Message 31 of 38
(1,852 Views)

i got the concept now. i think im going to take a class on just arrays .  lolololololol thats funny

 

but i was helping someone on this. 

0 Kudos
Message 32 of 38
(1,851 Views)

AHHHHHHHHHH you didnt give me credit on the last problem i post, did i do well. my friend everything works.

 

alot of people dont know about the double pointer double**,  he said i caught him off gaurd with that one.

0 Kudos
Message 33 of 38
(1,841 Views)
Darnell:

 

I don't understand your solution.  Can you explain how you can reference an element in the double_array in your function in the format double_array[idx1][idx2] when you passed it as a double **?

 

Here's what I understand about the way C stores  two-dimension arrays in memory.

At a low level, each location in computer memory has only one address, not a row,column address.  C stores the elements of a two dimension array in memory as if the array was a one dimension array, starting with the elements of row 0, and then following them with the elements of row 1, etc.

So if I create a two-dimension array with the following data:

 

double my2d_array[2][5] = {

{1, 2, 3, 4, 5}, // row 0

{11, 12, 13, 14, 15} // row 1

};

 

C stores the data in a linear fashion:

1

2

3

4

5

11

12

13

14

15 

 

There is no marker in memory to show where one row stops and the next one begins.  C knows where one row stops only because you tell it how many columns are in each row.

 

If you tell C that you want my2d_array[1][3], it calculates the address by adding the starting address of my2d_array, the desired row number (1) times the number of elements in a row (5) and the column number.

 

So the address of my2d_array[1][3] = *(my2d_array + 1*5 + 3).

 

The way C knows how to address the elements of a two-dimension array by the array[row][column] format is by knowing how many elements are in each row.  When you pass a 2d array as a double**, it doesn't know how many columns are in each row, so it doesn't know where to go when you show a [row][column] format.

 

I'd suggest you flatten you 2d array by passing it as a double * (instead of a double **).  Since C has no problem passing a 1d array to a function, I'd also suggest you pass your 1d array as a double array ([]) instead as a double*.

 

Another thing I don't understand about your solution is the following statement:

 

   conv_data[q][u]= temp_data[q][u];

 

Since your 2d arrays are dimensioned to [q][u], element [q][u] is out of the array bounds.  You statement above will compile with no errors, but you will get a run-time error trying to access non-existant array elements.  Did you run your program to prove you solution?

 

Here's how I would suggest reworking your code.

 

// start code sample

 

#include <stdio.h>

 

long f_ConvDblToSingleArray ( double* double_array, double single_array[] , int num_rows, int num_columns);

 

 

int main(int argc, char* argv[])

{

    int q=4,u=1000;

    int i, j;

    

    double temp_data[4][1000];

    double conv_data[4][1000];

 

    double meas[4000]; 

 

// create some test data for demonstration purposes

for (i=0, j=0; i<q; i++, j+= u/q)

{

if (j > u)

j=u-1;

temp_data[i][j] = i+j;

}

 

for (i=0, j=0; i<q; i++, j+= 5)

{

if (j > u)

j=u-1;

conv_data[i][j] = i*j;

}

    // put a breakpoint on the next line to see before and after 

    f_ConvDblToSingleArray( (double*)(temp_data), meas, q, u );

     // put a breakpoint on the next line to see before and after 

    f_ConvDblToSingleArray( (double*)(conv_data), meas, q, u );

 

    return 0;

}

 

 

 

//Library Function...(This compiles...)

long f_ConvDblToSingleArray ( double* double_array, double single_array[] , int num_rows, int num_columns)

{

 

    long error = 0;

 

    int idx1 = 0;

    int idx2 = 0;

    int index = 0;

 

    for (idx1=0; idx1 < num_rows; idx1++)

    {

        for ( idx2=0; idx2 < num_columns; idx2++ )

        {

            // create the equivalent of double_array[idx1][idx2] using pointer math

           single_array[index++] = *(double_array + (idx1*num_columns) + idx2);

        }

    }

 

    return 0;

 

 

// end code sample

 

Finally, I'm surprised that you were disappointed that we didn't give you quick credit for your solution.

You post a dubious solution that could not have run without errors and expect us to congratulate you on your programming expertise for using something "alot of people dont know"?  Well there are a lot of smart people in other fields who haven't heard of C, but don't ask us to congratulate you for knowing something they don't.

You have not given us credit for the hours we have spent helping you, but you would like some credit for the few lines of questionable code you posted.

 

Do you realize that most of the people helping you are volunteering their time and don't work for National Instruments?  We have our own jobs and don't have time to just sit and wait for your next question.  We would all appreciate a little more courtesy, a little more appreciation, and a little more effort on your part. 

 

Message 34 of 38
(1,819 Views)

darnell,

 

Are you sure "everything works" with your code?

It compiles without errors but it generates a runtime error: "dereference of null pointer" ? 

S. Eren BALCI
IMESTEK
0 Kudos
Message 35 of 38
(1,815 Views)

i got you i was getting it confuse about the give credit, i thought yal meant telling you thank you on on the message box, so how do i suppose to  give credit on here.

 

also i was helping a friend out  on that particular code.  basically what you just sent all i got to  do is remove a pointer  from each variable and it would look like yours.

 

also i dont know if it works or not, i just let him borrow the code i wrote. but he will let me know if it work or not. then i will make the necessary changes.  i just got it to

 

compile. when i was saying credit , i was meaning give me some points on the message box for trying it myself. do NI have some type of point system or something? 

0 Kudos
Message 36 of 38
(1,804 Views)

Darnell:

 

Be careful when you start to think like your statement "basically what you just sent all i got to  do is remove a pointer  from each variable and it would look like yours."  As you know, those type of details are very important in C.  There's a big difference between looking at an address and looking at the contents at that address.  Another important difference between the code you posted and my version is the way we access double_array in the function.  You try to access it by row and column (which I don't believe C knows how to do).  I access it doing pointer math.

 

As a suggestion to any help you give a friend: try your code before you send it to your friend.  It would have taken less than a minute to run your code, and you would have found the error that ebalci pointed out to you.  I always try out any code I post here.  I wouldn't want to waste anyone's time sending them code that doesn't work.

 

Sorry I don't have time right now to respond to your questions on giving credit.  Sometimes technical questions are easier to answer.

0 Kudos
Message 37 of 38
(1,792 Views)
he is doing it in c++ 2008 professional .net  , the data types are handle differently for some reason in c++, you will see if you try it out in the environment i tried it in. He's not doing it in the basic C environment, aight i got to get back to work
0 Kudos
Message 38 of 38
(1,785 Views)