![]() |
How to manipulate a variable value using an array?
I really really really want to be able to use an array to call my relays. How can i do this? See below post for what i want to be able to do.
|
Re: How do i put all of the relay inputs into an array?
Better yet, how can a store variable names within an array so that when i say:
myVar = 1; array[1] = {myVar}; array[1] = 2; printf(myVar); I want the displayed output to be 2 |
Re: How do i put all of the relay inputs into an array?
Quote:
myVar = 1;Good luck, ~Phil |
Re: How to manipulate a variable value using an array?
Or use a macro.
Code:
#define MYVAR array[1] |
Re: How to manipulate a variable value using an array?
I need you to confirm if what I am thinking is correct:
myVar = 1; Establish the "shell" as being = 1 array[1] = &myVar; Let the 1st value of array[] = the address of the "shell" (*)(array[1]) = 2; the * means modify/check the contents of the address of myVar printf(*myVar); since the shell wasn't updated, the * is needed to display the contents of the address of myVar since printf(myVar) would return 1 |
Re: How to manipulate a variable value using an array?
Quote:
Code:
(*)(array[1]) = 2;Code:
printf("%d\n",myVar); |
Re: How to manipulate a variable value using an array?
int *ptr;
int a, b; a = 10; ptr = &a; b = *ptr; Why bother with the int *ptr declaration when it has to be used again in the b= part? Is this just proper etiquette and not really needed? Is this alright for my declaration: int joystickbuttons[8] = {&p1_sw_trig, &p1_sw_top, &pl_sw_aux1, &p1_sw_aux2, &p2_sw_trig, &p2_sw_top, &p2_sw_aux1, &p2_sw_aux2}; And this for my looped search: if ((*)joystickbuttons[arrayindexer[1]] == 1) arrayindexer is just a simple array I am using for a counter. If all this looks good then it means I finally figured this whole pointer thingie out largely thanks to this forum and Wiki. |
Re: How to manipulate a variable value using an array?
Quote:
Code:
int *ptr; //ptr is a pointer to the address an 'int' somewhere in memoryThe first line declares a pointer, which is different from a regular variable. You can do other cool things with this, too. Code:
void idouble(int * input){ |
Re: How to manipulate a variable value using an array?
So i have to say that my array of buttons is going to be holding addresses by declaring it like this:
Code:
int (*)joystickbuttons[8] = {&p1_sw_trig, &p1_sw_top, &pl_sw_aux1, &p1_sw_aux2, &p2_sw_trig, &p2_sw_top, &p2_sw_aux1, &p2_sw_aux2};And the 2nd half of the above code: Code:
if ((*)joystickbuttons[arrayindexer[1]] == 1)I don't get some of your example: Code:
void idouble(int * input){ Input the address and convert it to the data "input" |
Re: How to manipulate a variable value using an array?
Quote:
|
Re: How to manipulate a variable value using an array?
Oh you sly bastard ;) You are basically bypassing the whole naming system of variables with input and directly puting data to addresses. I like this :) The int *input is just the declaration of input and not the actual conversion of &x to data....that was the confusing part. I can see why google refers to pointers as "advanced c".
Can you check the code i posted above the above post to tell me if that will work fine? |
Re: How to manipulate a variable value using an array?
This code isn't working:
int *joystickbuttons[8] = {&p1_sw_trig, &p1_sw_top, &pl_sw_aux1, &p1_sw_aux2, &p2_sw_trig, &p2_sw_top, &p2_sw_aux1, &p2_sw_aux2}; it errors me saying I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1105] symbol 'pl_sw_aux1' has not been defined I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1101] lvalue required I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1218] extraneous initializer values |
Re: How to manipulate a variable value using an array?
Well I see you have a pl instead of a p1
|
Re: How to manipulate a variable value using an array?
I fixed that but it still says
I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield I:\Robot\2007\MPLAB\Current\2007_2005\user_routine s.c:411:Error [1200] cannot reference the address of a bitfield |
Re: How to manipulate a variable value using an array?
not a 100% but I think it may have something to do with the fact that the p1, p2 etc switches are defined to structures. They're defined in the C preprocessor but they were never declared as variables anywhere else(atleast not anywhere I could find)
I'm thinking that you can't do this with those variables... |
| All times are GMT -5. The time now is 01:23. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi