Thursday, July 30, 2009

Strong in C? Then you might be able to answer this question!!?

Is there any datat types to assign dynamic data's to an array?





I mean I would like to assign datas dynamically in a real time environment. I need to pass those dynamic datas to a variable and need to use it with an array.( the variables inside the array should contain the updated as soon as the variables value changed)





Ex:





#include%26lt;stdio.h%26gt;





a1=p1.0;


a2=p2.0;


...upto





a16=p16.0; // The value of p1.0 will vary dynamically and the values assigned to a1, a2,.. a16 must be passed to the array I am going to use inside the main.





Main()





{


int i;


char c[]={a1,a2,...a15}





for(i=0;i%26lt;=15;i++)


{


if( c[i]==1)


{


bla bla..


}


}


}

Strong in C? Then you might be able to answer this question!!?
First, you have totally mixed concept of dynamic data type with your so called dynamic data here.





What you are talking here is about the ports on some system [probably some evaluation board in embedded system or some port from your computer].





Now you want to assign those values in your array. Please understand that though these values are varying at those port pins, it does not mean that by assigning variables in array it will automatically vary inside the array also.





So first thing is you can not assign dynamically modified value to some variable until some kind of event tells you about the value modification at source [Px.0 in your case].





Here even if you assign those values [a1,a2 etc] to array, still those will not be the current values at Px.0 as your Px.0 is modifying itself very fast.





So you either just assign those variables to your array one by one as c[0]=a1; c[1]=a2;...c[14]=a15;





Or have some timer event based on which you will do above functionality after some time intervals so that you get updated values periodically.





That's all I can say after looking at your incomplete, confusing question and the code. Make sure that you put your code and question completely [or code where actual problem is]. Still if you have anymore doubts then send me an email, I will try to help you.
Reply:ur question is pretty much unclear,


if u are asking about dynamic data type then try templates in c++ and i dont see any other solutions in c.


but from ur example, its unclear and confusing to give the exact answer.
Reply:Hi,


U may declare ur array-the one u r using in main(), as global variable, whenever or anywhere u assign any value to elements of this array, u may access these values in ur main() and u need not pass this array also;
Reply:ya


if i m getting u right then i guess what u want is to change certain data stored in u r array at the time of executing the progrm





this can be done by creating an array of pointers n then changing the value stored in the pointer


in this way u will have the pointer value i.e addresses that r, will remain same but the value in them will change


thus u r purpose is solved


No comments:

Post a Comment