Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to do the following:
#define ConfigureCAN(index,value) CAN.MBOX##index=value
for (i=0;i<15;i++) ConfigureCAN (i,0);
This doesn't work. Is there an other way then:
ConfigureCAN (0,0);
ConfigureCAN (1,0);
...
Thanks.
Edit: Wil this work Or better is the indexing of the stuct correct?
#define ConfigureCAN(index,value) {struct MBOX *Mailbox;Mailbox = &can.MBOX0 + (index * sizeof(struct MBOX)); MBOX=value;}
No, you can't use a for loop index variable (which only gets its value at run-time) to build a compile-time symbol name like that. The symbol names are not available when the program runs, i.e. when the for loop executes, so it doesn't make any sense.
You can probably set up a (pointer) array to make accesses easier to loop, but then you're going to have initialize the array which will be similiar-looking (but enable better run-time performance, if that's your goal).
There is no easy way to do so in C.
Depending on the framework and hardware you use, there might be an alternative way to address your CAN structs:
If you need the MBOX### values more often in a loop, you can create an array with pointers at an appropriate place:
volatile int * MBOXPOINTERS[] = { &CAN.MBOX0, &CAN.MBOX1, /* go on on your own */ };
(replace volatile int by whatever is used in the respective header file)
If you have the device data sheet and you see that the fields are in a successive way, you mght eveb ne able to define an array situated at the appropriate address. But that is highly hardware dependent.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was asked a question during an C Language interview. the question is:
Can I change the address?
struct node * root;
root=(int*)malloc(sizeof(int));
printf("%d",root) =10128000 // new address: root=101590000
None of the C standard library's allocation functions allow you to specify the address where you want the allocated space to be. It would not make sense to do so, because it's very unlikely for the programmer to know or care what specific address they get, unless something known to them is already there, in which case it's not an available address.
You can, however, allocate a large block (e.g. via malloc), and then manually assign chunks of that block however you like. That allows you to choose your own addresses relative to the base of the allocated block. For example:
my_node *node_base = malloc(AS_MUCH_MEMORY_AS_I_NEED);
// ...
// malloc analog:
size_t an_index = choose_a_node_index_by_some_criteria();
my_node *node = node_base + an_index;
// free analog:
mark_index_available_again(an_index);
Of course, the devil is in the details, and those are both specific to your needs and more complex than I'm prepared to go into here. Overall, this is not something that a self-declared beginner really ought to be trying to do.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am currently studying the crazyflie 2.0 drone Firmware. For those who do not know the drone, here is a link to the Website:https://www.bitcraze.io/crazyflie-2/
It is an open source Project.
Anyway, I have toruble understanding some part of the Firmware Code. Actually it might be very simple, but I am very new to programming in C.
struct CommanderCrtpValues
{
float roll;
float pitch;
float yaw;
uint16_t thrust;
} __attribute__((packed));
static struct CommanderCrtpValues targetVal[2];
You can find this Piece of Code at: https://github.com/bitcraze
crazyflie-firmware/modules/src/commander.c
I do not understand the last line. I believe that it is an assignment of a struct to an Array, named targetVal, but I am not sure. Could you explain what's really going on ?
This is creating a static array of 2 CommanderCrtpValues structures.
Since this is declared in the global scope its memory is going to be initialized as 0 (i.e. the all the fields will have the value 0) when the program starts, see Why are global variables always initialized to '0', but not local variables?. I do not think this detail is important in this particular case but it is used in other places of the firmware.
(Disclaimer: I am the author of this code.)
The code defines a structure named CommanderCrtpValues that has four fields (roll, pitch, yaw, thrust).
The last line defines a variable named targetVal that is an array of 2 elements. Each array element is a structure of CommanderCrtpValues.
The variable targetVal has not been initialized yet so the actual content of each element is undefined. Even though you know that they are CommanderCrtpValues structures, you don't know what values the fields contain.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why do we use pointers in C Programming?
In general pointers are able to access the address where the int/float/char etc... is stored.
Are there any other uses?
It depends on what you try to achieve:
you can change the value of a variable inside a function
you can pass a struct to a function without having to copy all its fields - think of a function that receives a struct.
you can point to a specific variable/struct and point to it from other structs
and many other advantages (advantages is purpose dependant and it depends on whats your program is doing).
Pointers are quite basic C and there is a lot of material online you should get yourself familiar with them and the advantages will pop up themselves.
The reason is that pointers are used to bodge into C some vital features which are missing from the original language: arrays, strings, & writeable function parameters. They can also be used to optimize a program to run faster or use less memory that it would otherwise. A few tasks these days, such as programming microcontrollers, still need this.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a pointer of pointers, and I want a loop to go thorough them and store its value into something else. Is there any way to do that?
e.g:
char **variable;
Now I want to read that into another variable:
char **variable2
i thought of doing something like this:
for(i = 0;i <LENGTH_OF_VARIABLE-1;i++){
variable2[i] = variable[i+1]
}
But that is not possible in c, right?
Now you might ask why not variable2 = variable? well variable2 should store only parts of the variable, not all of them.
EDIT: Variable's size is not known, and its dynamic(read from the command line). AND no it doesn't contain '\0' at the end. Cause its processed to remove such a character and then passed to a function that I am implementing.
If you already putting anything to your **variable, does that mean that you have allocated memory correctly?
I think it will better for you to revise and understand how simple one dimensional array works, after understanding that, move to double arrays. Then take a look how pointers work and learn how to allocate memory. After understanding this steps i have mentioned above, take a look at double pointers and allocation of memory in case of double pointers.
here you go.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I got one question a very simple one.
while implementing a simple locking mechanism using global variable.I can't find out how to prevent the access of global variable from 2 processes/thread.
My algorithm (take 2 process)
Process p1 check if variable g is set then do not modify the code.
If not set then set it then modify code.
Same for process 2 .
while executing I got fair result but is it correct.
My doubt in some architecture if 2 instructions are not atomic then how to avoid accessing the global variable at a time.
please give me some basic idea.
Use atomic methods to manipulate the global variable.
Atomic operations are 'built-in' to gcc. While they are not generally 'portable', most compilers offer atomic operations.
For GCC, you might implement something like this:
if(__sync_bool_compare_and_swap(&g, g, g+1))
/* Changed by you. */;
else
/* Another process changed it before you could. Perhaps try again. */