Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
2  
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. – abdullah kahraman May 14 at 14:47
1  
Does the _IO macro include volatile ? If not, you will need that on any variable you access from both normal and interrupt threads. – Chris Stratton May 14 at 14:54
1  
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. – Nick Alexeev May 14 at 18:15
sorry, abdullah kahraman, next time, i will post the code myself. i used to consider print screen as a visual friendly way..... – oilpig May 15 at 1:30
1  
sorry, Nick, I have re-edited my question... – oilpig May 15 at 1:52

closed as off topic by Leon Heller, Dave Tweed, Nick Alexeev, Olin Lathrop, Brian Carlton May 14 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.

3 Answers

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];

share|improve this answer
Or extern __IO uint8_t data[];. – Pete Becker May 14 at 21:49
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]... – oilpig May 15 at 2:01
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. – Dave Tweed May 15 at 4:00
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]... – oilpig May 15 at 4:28

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.

share|improve this answer
Good point, but not really an answer to the actual question. – Dave Tweed May 14 at 18:08

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

share|improve this answer

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