check for duplicates in enum [closed] - c

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 5 years ago.
Improve this question
Assuming I have the following enum:
enum {
PARM1 = 1,
PARM2,
PARM3,
PARM_MAX
};
I can add new members in it, but I want to make sure in compile-time, if possible, that all the members are consecutively incrementing and there is no duplicates. I'm wondering how to verify this in C code. I believe this is a math task.

If you use that enum declaration, the numbers will be
enum {
PARM1 = 1,
PARM2, /* = 2 */
PARM3, /* = 3 */
PARM_MAX /* = 4 */
};
It is not necessary to check this, all the compilers use this implementation.
Like Cubbi says in cppreference.com :
If enumerator is followed by = constant-expression, its value is the
value of that constant expression. If enumerator is not followed by =
constant-expression, its value is the value one greater than the value
of the previous enumerator in the same enumeration. The value of the
first enumerator (if it does not use = constant-expression) is zero.

Related

Understanding a line of C code containing braces and brackets [closed]

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 6 years ago.
Improve this question
The code is question is:
unsigned int iomask[]={1UL<<4};
I understand the left shift binary operation, but I do not understand the function of the {} and []. Could anyone help?
The [] tells you that iomask is an array whose size is determined by its initializer. The {} is that initializer. It can be used to initialized arrays or structs.
In this case, the initializer contains a single element, so iomask is an array of 1 element.
[ ] simply indicates how many elements (or, dimension) are in the array, however, C compilers allow the array dimension to be omitted, and will infer the number based on the number of elements listed.
int iomask[] = {1,2,3,4,5};
is equal to
int iomask[5] = {1,2,3,4,5};
where the { } contain the initializers for the elements of the array.
In your example, iomask has 1 element, the 1UL<<4

I need a variable that have a small range value [closed]

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 have an input number that it's value is not always consistent, i mean the value change plus 1 or minus 1 and etc. So, i want to compare it with a const but have a small range value, for example a const int Dist that have value between 14 to 16. Is that possible to implement it on C programming? Please help me.
You can set constant for lower bound, and constant for upper bound and check if the value falls within the range.
Pseudocode:
int const LOWER_BOUND = 14;
int const UPPER_BOUND = 16;
if (input <= UPPER_BOUND && input >= LOWER_BOUND)
... logic here ...

What does the following c code do? [closed]

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 7 years ago.
Improve this question
#define SRC_ASCLIN_ASCLIN0_RX (*(volatile Ifx_SRC_SRCR*)0xF0038084u)
Here SRC_ASCLIN_ASCLIN0_RX means ASCLIN(Async/Sync serial LIN Comm) Receive Service Request.
I know that the macro is used to point at the address 0xF0038084u. But I want real time examples.
Am working on UART development on Infineon microcontroller.
The macro, when expanded by the preprocessor, cast the integer literal as an address, a pointer to Ifx_SRC_SRCR, then dereferences the pointer so you can get or set the value of the memory stored at that specific address.
So you could write e.g.
Ifx_SRC_SRCR value = SRC_ASCLIN_ASCLIN0_RX;
or
SRC_ASCLIN_ASCLIN0_RX = some_other_value;
It basically equivalent to doing e.g.
int an_integer = 6;
int *pointer_to_an_integer = &an_integer;
*pointer_to_an_integer = 10; // Equivalent to SRC_ASCLIN_ASCLIN0_RX = some_other_value above

enum or typedef enum for return values [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a function that tests for several different states of the system.
I could either use
enum
{
limit = -2,
timeout = -1,
ongoing = 0,
finished= 1
};
The function would then be defined as:
static int test(void);
I personally prefer the typedef enum approach:
typedef enum
{
limit = -2,
timeout = -1,
ongoing = 0,
finished= 1
} eTest;
Then the function would be defined as:
static eTest test(void);
What would you consider better of these two?
Or is there an better way to do this that I haven't thought of?
What would you consider better of these two?
In fact second version makes more sense to reader.
In first version, reader can't know what that int would mean, but second version give clue to reader about the return value.

F# Newbie: Why does this function return array? [closed]

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
From what I've read, a function in F# returns the value which is the last line in that function. So how does the following function return an array?
let swap (a: _[]) x y =
let tmp = a.[x]
a.[x] <- a.[y]
a.[y] <- tmp
The function you've posted does not in fact return an array, it returns a unit. This is because the type of the last line is unit, by design of the <- operator.
The function you've posted is not a pure function because it has side-effects. The swap occurs on the array by reference, because .NET arrays are mutable and reference types. A more "pure" swap function would produce a new array without modifying the input array.

Resources