-4
\$\begingroup\$

I have defined a global array in main.c:

__IO uint8_t data[3]={0,0,0};

then, I extern and assign vlues to this array in another c file:

extern __IO uint8_t *data;
INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4){

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_0) == SET)
  data[0] = 1;

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_1) == RESET);
  data[1] = 0;

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_1) == SET)
  data[1] = 1;

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_2) == RESET);
  data[2] = 0;

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_2) == SET)
  data[2] = 1; }

the problem is that I can not change the value of 'data'(I get the vaule through debug mode of IAR using J-link).If I define this array as local variable I can change the value:

__IO uint8_t data[3]={0,0,0};
INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4){

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_0) == SET)
  data[0] = 1;

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_1) == RESET);
  data[1] = 0;

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_1) == SET)
  data[1] = 1;

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_2) == RESET);
  data[2] = 0;

if(GPIO_ReadInputPin (GPIOB, GPIO_PIN_2) == SET)
  data[2] = 1; }

in the past time, I define a global variable not an array and I can extern change the value in another file. so, what is the difference between a vriable and array used as globle ones?

\$\endgroup\$

closed as off topic by Leon Heller, Dave Tweed, Nick Alexeev, Olin Lathrop, Brian Carlton May 14 '13 at 22:53

Questions on Electrical Engineering Stack Exchange are expected to relate to electronics design within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ You should not post the pictures of the code, instead you should post the code itself so that people can Google it. Besides, this website formats the code automatically. \$\endgroup\$ – abdullah kahraman May 14 '13 at 14:47
  • 1
    \$\begingroup\$ Does the _IO macro include volatile ? If not, you will need that on any variable you access from both normal and interrupt threads. \$\endgroup\$ – Chris Stratton May 14 '13 at 14:54
  • 1
    \$\begingroup\$ You're posting code as screenshots. And not for the 1st time. It was explained to you why this is a nuisance. -1 and vote to close. \$\endgroup\$ – Nick Alexeev May 14 '13 at 18:15
  • \$\begingroup\$ sorry, abdullah kahraman, next time, i will post the code myself. i used to consider print screen as a visual friendly way..... \$\endgroup\$ – oilpig May 15 '13 at 1:30
  • 1
    \$\begingroup\$ sorry, Nick, I have re-edited my question... \$\endgroup\$ – oilpig May 15 '13 at 1:52
3
\$\begingroup\$

The second declaration, extern __IO uint8_t *data; doesn't declare an array at all, but rather a pointer. While arrays and pointers are largely interchangeable in the arguments of functions (array arguments are implicitly turned into pointers to their first elements), this does not hold elsewhere, such as in this example.

You need to declare it as extern __IO uint8_t data[3];

\$\endgroup\$
  • \$\begingroup\$ Or extern __IO uint8_t data[];. \$\endgroup\$ – Pete Becker May 14 '13 at 21:49
  • \$\begingroup\$ thanks, @Dave Tweed, what does " this does not hold elsewhere"mean? I think extern __IO uint8_t *data point the first element of data[] and then I can assign value to data[1] data[2]... \$\endgroup\$ – oilpig May 15 '13 at 2:01
  • \$\begingroup\$ You think incorrectly. extern __IO uint8_t *data declares a single unit of storage that holds the address of an __IO uint8_t. On the other hand, extern __IO uint8_t data[3] declares three units of storage, each of which holds an __IO uint8_t. Entirely different, especially in terms of the code that the compiler must generate for each case. The first one implies a level of indirection that doesn't exist in the latter one. \$\endgroup\$ – Dave Tweed May 15 '13 at 4:00
  • \$\begingroup\$ actually, i mix it up with another case in one c file @DaveTweed:__IO uint8_t data[3]={0,0,0}; __IO uint8_t *back_data = *data; then back_data points to the first element of data and i can assign value to data[] using back_data[0]back_data[1]... \$\endgroup\$ – oilpig May 15 '13 at 4:28
1
\$\begingroup\$

Your indentation indicates that the lines like

data[x] = 0;

should be executed conditionally but as the prior if statement comparing to RESET is terminated with a semi-colon, they are unconditional.

\$\endgroup\$
  • \$\begingroup\$ Good point, but not really an answer to the actual question. \$\endgroup\$ – Dave Tweed May 14 '13 at 18:08
0
\$\begingroup\$

Use the same declaration type in your extern declaration as in the defition: data[3].

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.