Saturday, May 22, 2010

C Programming Help - Declaring Variables!!?

Create an appropriate declaration for the following:





a) A variable called “sensor_out” that will contain numbers from -10 to +45.


b) A character string constant that will contain the string “Press here to end”.


c) A variable array that will have ten elements, each holding numbers from -23 to 345.





THANKS SO MUCH!!!!

C Programming Help - Declaring Variables!!?
I never cooperate in sensorship!
Reply:In C, right? Do:





int sensor_out;


const char String[] const = "Press here to end";


int array[10];





....





Please note that there is no check for the range of the variables, only that the variable CAN hold the given range. Doesn't mean they can hold ONLY values in that range.
Reply:A) signed char sensor_out;


B) const char str[] = "Press here to end";


C) short int array[10];


D) unsigned int array_list[10], *ptr_array = array_list;


You reference ptr_array like an array.


ptr_array will point to the array array_list so ptr_array[0] will be the same as array_list[0].


To make ptr_array really dynamic requires you to use malloc or calloc memory allocation functions.


Use calloc as follows:


ptr_array = calloc(sizeof(unsigned int), 20).


This will allocate 20 * (memory required to hold an unsigned integer) in memory.


No comments:

Post a Comment