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.
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 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 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.
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 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 question in C, how to assign the address of a variable to NULL?
For example:
int a;
And how to assign the address of variable a to NULL?
You got it backwards, you can't change the address of a variable. The compiler places it wherever it feels like, and the address becomes what it becomes.
What you can do is use a pointer:
int *a = NULL;
This of course means that the address of a, the pointer variable whose value is NULL, is not NULL, just as with int a;.
In some implementations (like for embedded systems where there is no operating system) you can control where (static only, typically) variables reside exactly, by instructing the linker and sometimes also by explicitly adding compiler-specific invocations that put various declarations into different segments. The segments are then laid out by the linker into the available memory.
You can't change the address of a variable, nor need to.
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 9 years ago.
Improve this question
I am curious about struct that we declare in C. What does it actually do ?
I know we can make nodes & pointers to those using struct but how does it function ? Like a while loop checks the condition & branches accordingly if equal or not equal. What does struct do under the hood ?
A struct type is a user-defined composite type. It is composed of fields or members that can have different types.
From struct-wiki:
A struct in the C programming language is a declaration that defines a list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer.
For memory allocation of a struct: check out How are C struct members allocated?
For why to use struct, check out Why should we typedef a struct so often in C?
Its nothing more than multiple variables treated as one entity. There is not much magic behind this, the values simply appear behind each other in the order of declaration in the memory.
A C struct simply represents data in a specified way and doesn't do anything at all. It is used to represent a more complex data-type, such as a linked-list node.
Using struct a user can define its own required data type to handle complex data.
just like array where all elements in array are of same type but in struct each element can be defined in user's desired way.
so struct is used to define ""user-defined data types".