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 a code like this :

void display_character (char j,k,l,m)  {

unsigned char display [4] = {j,k,l,m}
.
.
.
.
}

void main()
 {

   while(1)
    {
      display_character(1,2,3,4);
      display_character(5,6,7,8);

    }//end of while
 }

How can I pass 1,2,3,4 into j,k,l,m in the array ? because I got error with this code : error C247: non-address/-constant initializer

Thank you

it doesn't work

void display_character (char j,k,l,m)  {

//unsigned char display [4] = {1,2,3,4}; // we can modify value of this variable in run time
unsigned char display [4]; // we can modify value of this variable in run time
                display[0]=j;
                display[1]=k;
                display[2]=l;
                display[3]=m;

unsigned char x,y,a,z;
.
.
.

error : *

**32_8_MAIN.C(98): error C141: syntax error near 'unsigned'
32_8_MAIN.C(98): error C202: 'x': undefined identifier
32_8_MAIN.C(101): error C202: 'z': undefined identifier
32_8_MAIN.C(103): error C202: 'x': undefined identifier
32_8_MAIN.C(105): error C202: 'y': undefined identifier
32_8_MAIN.C(107): error C202: 'a': undefined identifier
32_8_MAIN.C(108): error C202: 'a': undefined identifier
32_8_MAIN.C(111): error C202: 'a': undefined identifier
32_8_MAIN.C(112): error C202: 'a': undefined identifier
32_8_MAIN.C(118): error C202: 'z': undefined identifier
32_8_MAIN.C(120): error C202: 'x': undefined identifier
32_8_MAIN.C(122): error C202: 'y': undefined identifier
32_8_MAIN.C(124): error C202: 'a': undefined identifier
32_8_MAIN.C(125): error C202: 'a': undefined identifier
32_8_MAIN.C(128): error C202: 'a': undefined identifier
32_8_MAIN.C(129): error C202: 'a': undefined identifier**

*

share|improve this question
1  
Looks like a software question, not electronics – stevenvh Mar 12 '12 at 8:45
it's electronics, this software is for MCS51 in Keil – Rick Ant Mar 12 '12 at 8:49
2  
It is a pure C question having nothing to do with electronics. You could have the same question on any machine. – Olin Lathrop Mar 12 '12 at 12:37
@RickAnt To clarify what these guys are saying, we do accept firmware question, but only when they some how relate to the specifics of the hardware they are on. Like if you are interfacing with a hardware PWM or something along those lines. Questions that don't have any hardware interaction should be on stackoverflow. – Kellenjb Mar 12 '12 at 18:27

closed as off topic by stevenvh, Olin Lathrop, Leon Heller, Kevin Vermeer Mar 12 '12 at 17:47

Questions on Electrical Engineering Stack Exchange are expected to relate to electronics design within the scope defined in the FAQ. 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 closed questions here.

3 Answers

up vote 1 down vote accepted

You probably cannot initialize the variable with other variables, so unsigned char display [4] = {1,2,3,3} would be OK, but the way you wrote it isn't.

In that case just do like this:

unsigned char display [4];
display[0]=j;
display[1]=k;
display[2]=l;
display[3]=m;

it's not working

error :

32_8_MAIN.C(99): error C141: syntax error near 'unsigned'
32_8_MAIN.C(99): error C202: 'x': undefined identifier
32_8_MAIN.C(102): error C202: 'z': undefined identifier
32_8_MAIN.C(104): error C202: 'x': undefined identifier
32_8_MAIN.C(106): error C202: 'y': undefined identifier
32_8_MAIN.C(108): error C202: 'a': undefined identifier
32_8_MAIN.C(109): error C202: 'a': undefined identifier
32_8_MAIN.C(112): error C202: 'a': undefined identifier
32_8_MAIN.C(113): error C202: 'a': undefined identifier
32_8_MAIN.C(119): error C202: 'z': undefined identifier
32_8_MAIN.C(121): error C202: 'x': undefined identifier
32_8_MAIN.C(123): error C202: 'y': undefined identifier
32_8_MAIN.C(125): error C202: 'a': undefined identifier
32_8_MAIN.C(126): error C202: 'a': undefined identifier
32_8_MAIN.C(129): error C202: 'a': undefined identifier
32_8_MAIN.C(130): error C202: 'a': undefined identifier
Target not created
share|improve this answer
but give me another error, can you see my edit ? thanks – Rick Ant Mar 12 '12 at 8:43
You've added a declaration of more variables (x.y etc) after some code, in C you have to do all the declarations at the start of a code block and before you start the code statements. – Martin Mar 12 '12 at 9:34

it works :

void display_character (char j,k,l,m)  {
unsigned char x,y,a,z;

unsigned char display [4]; // we can modify value of this variable in run time

                    display[0]=j;
                    display[1]=k;
                    display[2]=l;
                    display[3]=m; 

.
.
.
.
}

display_character(1,2,3,4)
share|improve this answer
why did you include the "unsigned char x,y,a,z"? Well, I guess it was used somewhere in your code. Also display[4] should be display[3]. All the best. :) – Paul A. Mar 12 '12 at 11:21

THIS SHOULD WORK................

void display_chacracter(j,k,l,m); //declaration

void main()

{

while(1)

{

display_character(1,2,3,4);
display_character(5,6,7,8);
while(1);       //just to put a stop :)
}//end of while

}

void display_chacracter(j,k,l,m)

{

unsigned char j,k,l,m;

unsigned char display[3];

display[0]=j;
display[1]=k;
display[2]=l;
display[3]=m;

}

share|improve this answer
Downvoted because this is wrong, the number in brackets at the array declaration is number of elements, not max element index. From K&R: "The declaration int ndigit[10]; declares ndigit to be an array of 10 integers. Array subscripts always start at zero in C, so the elements are ndigit[0], ndigit[1], ..., ndigit[9]." – Martin Mar 12 '12 at 12:56

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