calling an array

This is a discussion on calling an array within the C Programming forums, part of the General Programming Boards category; Hello, i want a little help to make my code more efficient cause i work on an embedded system and ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    calling an array

    Hello,
    i want a little help to make my code more efficient cause i work on an embedded system and i exceeded my rom capacity.

    I have an int.
    a=3 for ex.

    I want for each a to call one array.

    so for this example i want to call number3

    I did it with cases,

    switch(a)...


    case(0): call number0
    case(1): call number1


    but i think that there could be a way to avoid cases and do it with one command.

    Can you help me?

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    i think that there could be a way to avoid cases
    There is, use If-statements.
    Code:
    if(a==0){
      /*do something*/
      } 
    if(a==1){
      /*blabla*/
      }
    /*& so on*/
    Ganglylamb.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    The problem is that i want to make my code smaller in bytes. So the if clauses do not offer me something better than case. I think that the cases are more efficient than the if clauses.

    Anyway,

    In php i would do something like
    button_$a and it would call button_1 (if a=1) and so on...

    But here i don't know how to declare that the a is a variable in button_a

    Any ideas?

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Sry mayb its my poor english or maybe you should post some of your code to clarify your question, because you lost me right now..

    And when it comes down to efficiency i think that cases and if statements have the same efficiency and that it wont be any bytes longer in the resulting exe when you use if statements...
    But in the end a if statement does the same as a case you can have several if statement and after that you use an else statement (and then you have the same as if you would use a case with default ).

    Anyhow some code would make sense i guess (for me although).

    ::edit::
    i might have just seen "the light".
    I guess , because i dont know any php, you just need something like this .
    Code:
    int button[BUFSIZE];
    /*do something with the a you are talking about to determine the value of a*/
    /*of course you would have some values stored in te array button*/
    /* now you say , for instance a=0*/
    printf("%d",button[a]);
    /* this will print the value of the first element in the array button*/
    i gotta jet laterz,

    ::/edit::
    Ganglylamb.
    Last edited by GanglyLamb; 07-08-2003 at 05:43 AM.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    i think it is better to give you the some code to see what i mean.


    these are two arrays

    number_0[5][27]
    number_1[5][27]

    i didnot give you the content of the arrays because it was too big.

    and inside this function

    void call_my_array(void){
    int a;
    switch(a) {

    case(0): call number_0[5][27];
    case(1): call number_1[5][27];
    }

    I want to change it and do something like this
    void call_my_array(void){
    int a;
    call number_a[5][27];
    }
    how can i do it so as to be correct?

  6. #6
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    With my basic knowledge of C i DONT think its possible to do with you are trying to do you are actually trying to put the value of a variable (a) into the name of another variable called button...

    Greetz,

    Ganglylamb.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    What you are wanting to do can be accomplished very easily via pointers. However I see no use for what you are doing.

    Perhaps an explanation is in order.

    If you are wanting to call functions via an array then you need to set up an array of pointers to functions, but again I'm sort of lost here. If you want to set a value in an array via a pointer then you need to create a pointer of the same data type as the array and point to (row*width+column) or vice versa depending on how you set it up. But this can also be done very easily in C so again I'm lost.

  8. #8
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Bubba did you just said its possible to rename variables?? (and not the value of it but the name of the vars...??)

    off to work,
    Laterz,

    Ganglylamb.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    No.

    What is a variable? It is a section of memory that holds some value. The name is used to simplify it for the programmer by the compiler. In actual assembly language the variable is simply a portion of memory - thankfully we can refer to it by a name rather than an address or specific location.

    Can you change the value of a declared variable w/o using its name. Yes.

    Can you change the name of a declared variable inside of the compiler. No.

    Names are compiler-side and assembler-side (some) devices. You don't want to see their pure assembly language equivalents.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    No, i am afraid you didn't understand what i mean.

    I will set a simple task.

    ----------------------------
    I have 10 variables.
    From
    number_0 = "blah..blah"
    to...
    number_9 = "another blah.."

    for(i=0; i<10; i++) {

    HOW CAN I CALL EACH VARIABLE ON EACH LOOP(using i)???

    }

    I hope you can understand what i mean now.


    Sorry for annoying you. In php i could call the variable like this:

    for(i=0; i<10; i++) {

    another variable = "$number_$i";

    }

  11. #11
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >>HOW CAN I CALL EACH VARIABLE ON EACH LOOP(using i)???
    You use an array. I don't understand what you're going on about, if the switch/case works then why bother doing something exotic to avoid it?

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    So you are trying to create new variable names from one variable.

    You can't do that.

    Variable names are only strings on the compiler side, they represent memory locations in assembly. The only thing you can do is use an array to represent a set of values that would represent several variables.

    For instance in BASIC:

    Code:
    for i=0 to 10
      s$=str$(i)
      name$="variable"+mid$(s$,1,len(s$))
      print name$
    next
    And you want to use the printed name as a variable - you can't do that.

    You cannot create several variables on the fly for the compiler. You must explicitly declare every variable with a unique name - depending on the scope of the variable - local variables are popped off of the stack on a function exit or ret.

    Why do you need to do this? Perhaps we could point you in the right direction if you gave us more info about your problem.

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    He wants to create variable names on the fly w/o explicity declaring them. Unfortunately you cannot do this but you can accomplish the same thing using an array to hold several values.

    I have no idea what calling a variable means.

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    The best method to use depends entirely on what embedded system you are are targetting your code for and what compiler. An array with pointers to functions for example will actually generate more code than your switch statement if used on a PIC for example. The hiware compiler for the ST7 Microcontrollers will generate less code if you use "if" statements rather than a "switch/case" statement, whilst the Cosmic compiler for the ST7 is the other way around (switch is more efficient than if/elseif).

    Generally, however, don't use function pointers in an 8-bit processor - a switch/case will always be more efficient.

  15. #15
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    ::Time out::

    What the heck are we talking about anyway?



Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 06:49 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  4. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  5. Replies: 2
    Last Post: 07-11-2008, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21