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

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

Related

assign zero to item in char 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 months ago.
Improve this question
I have some materials showing this code. what does code means by making these assignments?
char inputfilename[128];
inputfilename[0] = 0;
char *argv[128];
*argv[1] = 0;
In C, character arrays are terminated by a null character (value 0). In both cases in your example, the code initializes the strings to "empty" (with a terminator in the first element). This would prove useful in any subsequent string operations (strcat, strcpy, etc.).

how to print permutations of array of strings in c without recursion? [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 2 years ago.
Improve this question
Is anyone here who can explain me how to print the permutations of a array of strings in iterative method without using recursion. Please explain with code.
If the string is "abcd", put all of the "a" chars in position 0 for the first n-1! arrays, in position 1 for the next n-1! arrays, etc. Then put all of the "b" chars in position 1 for the first n-2! arrays, etc, all of the "c" chars in position 2 for the first n-3! arrays, etc, and all of the "d" chars in position 3 for the first n-4! arrays, etc, using modulo n arithmetic in each case to move from position 3 to position 0 as you are filling out the arrays.

How does the code that prints array elements works? [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 3 years ago.
Improve this question
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}
Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.

check for duplicates in enum [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 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.

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