user2293738
user2293738

Reputation: 21

Debugging program with pointers and arrays

Kindly help in resolving this,the error i'm getting here is syntax near '{' since i had declared unsigned char near DAC_table i got an error,so define outside the function which is wrong...i have not posted my complete code here...in this part of the code i'm getting problem..

unsigned char DAC_table[16];     
unsigned char *ptr2tbl; 
void fnSelectVoltage(void)
{
    line_display(1, "Volt Sel");
    sprintf(line_buf," %d V",(unsigned int)*ptr2tbl);       
    line_display(2, line_buf);

    DAC_table[16] = ( 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                      0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F);
    *ptr2tbl = &DAC_table;  
    while (START_KEY)
    {
        if (!UP_KEY)
        {   
            wait_for_any_key_counter_0 = 0;
            for (i = 0; i<15; i++)      
            {
                P2 = *ptr2tbl++;            
                //  delay_ms(1000);         
            }
        }
        else if(!DOWN_KEY)
        {
            wait_for_any_key_counter_0 = 0;
            for (i = 0; i<15; i++)      
            {
                P2 = *ptr2tbl++;                
                // delay_ms(1000);              
            }
        }
    }
}

Upvotes: 0

Views: 119

Answers (2)

Amadeus
Amadeus

Reputation: 10665

After a brief look at your code, I can say that:

  • ptr2tbl is being used without initialization, when it is first called -> sprintf(line_buf," %d V",(unsigned int)*ptr2tbl).
  • *ptr2tbl = &DAC_table; is incorrect. You probally wanna something like that: ptr2tbl = DAC_table once both are pointers. Remember that *ptr2tbl will access the element that it is pointing to, and at this moment, in your code, it is pointing to nowhere
  • P2 is not declared (Is P2 a global variable?)
  • and, as said before, you cannot initialize DAC_table that way (using parenthesis is wrong).

Upvotes: 0

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

unsigned char DAC_table[16];// u hv created a global array here 

To assign values to it,

    DAC_table[0] = 0x00;
    DAC_table[1] = 0x01; and so on.

OR even better

unsigned char DAC_table[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; at the line of declaration.

You can't assign values to variables at file scope except at the line of declaration.

Upvotes: 1

Related Questions