How to initialize an array in C with one line? [duplicate] - c

This question already has answers here:
Assign multiple values to array in C
(8 answers)
Closed 8 years ago.
This is my code
int numLeft[5];
void init()
{
numLeft = {5,4,3,3,2};
}
When I tried compiling this code, I got this error: "error: expected expression before '{' token.
I know in java something like this could work
int[] numLeft;
void init() {
numLeft = {5,4,3,3,2};
}
Is there something i am missing in my C code? A quick google search isn't helping.

int numleft[5] = {5,4,3,3,2}
is the way to go. Here numleft is the variable that is referring to the array, but to access each, you've to refer to it as numleft[0], numleft[1], likewise.

int numLeft[5] = {5,4,3,3,2}; wiil do the job
if you need to initialize the global array each time, need to use memcpy(), as suggested in the other duplicate answers.

Related

Call a char pointer in the function [duplicate]

This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 2 years ago.
I saw the piece of the code. But do not know what the purpose is.
void a()
{
static const char *string = "STRING";
...
(void)string; // <---- What the purpose of the line is?
...
}
(void) before a variable like that creates an empty expression and is used to silence warnings about a variable not being used by the program.
In this specific case it would be better to simply comment out the variable declaration.

Is it possible to pass a struct to a function without creating a variable? [duplicate]

This question already has an answer here:
Is it possible to pass a structure variable as a function argument without previously defining it?
(1 answer)
Closed 3 years ago.
In C++ this code is valid
struct foo{
int x;
};
int bar(foo f);
bar({1});
However I get an error when I try to do something similar in C. Is there way a to pass a struct to a function without actually creating a variable for it?
You need a compound literal for this:
bar((struct foo){1});

What is the significance of asterisks postfixing a variable type in a method header? [duplicate]

This question already has answers here:
difference between int* i and int *i
(9 answers)
Why is the asterisk before the variable name, rather than after the type?
(12 answers)
Closed 3 years ago.
I have been trying to learn C as a programming language, and have been trying to solve sample problems on site like LeetCode in C programs. When I was reading over some of the skeleton code that was provided as a function header for a problem on LeetCode that I want to solve in C, the function header had asterisks post fixing some of the types, specifically like this:
int* twoSum(int* nums, int numSize, int target, int* returnSize) {
/* Code goes here */
}
After doing a fair bit of reading, I learned that prefixing a variable with an asterisk when declaring a variable reserves the variable as a pointer, but I have not been able to find anything about what it means when the type specifier itself is post fixed with an asterisk.
The spaces there don't matter.
int* nums is identical to int *nums. So are int * nums and int*nums.
All four of these declare nums as pointer to int.
It's a matter of style preference (though I wouldn't use that last one), with no effect on the generated code.

bizarre C statement [duplicate]

This question already has an answer here:
What is the purpose of static keyword in array parameter of function like "char s[static 10]"?
(1 answer)
Closed 9 years ago.
void test(int x[static 10]);
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10,11};
test(a);
return 0;
}
void test(int x[static 10])
{
printf("%d",x[9]);
}
I was looking for bizarre C statements. I found this one, But could not understand what is the use of static 10 in that statement. Is it same as int x[10]?
Another thing, you can use volatile also, in place of static e.g int x[volatile 10]
Anybody knows what is the use of this kinda declaration?
PS: Compiled using GCC 4.6.3,
It's a hint for the compiler telling that the x pointer parameter points to the first element of an array of at least 10 elements.
For example:
test(NULL); // undefined behavior

Passing array not pointer to a function in C [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
I want to pass an array to a function such that if I use the sizeof operator on array inside the function body, I should get the actual size of the array and not that of a pointer. For e.g -
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,0};
printf("Array size = %d\n", sizeof(arr)/sizeof(int)); ---> O/P = 40/4 = 10
func(arr);
}
void func(int arr[])
{
printf("Array size = %d\n", sizeof(arr)/sizeof(int)); --->O/P = 4/4 = 1
}
The printf statement in the main function prints 10 while the one in func prints 1. Is it possible to do so in C language?
EDIT: I know other alternatives like wrapping the array into a struct etc. Actually, I was trying to solve a problem given on TopCoders site and the requirement of one problem is that I have to write a function(requirement given below). Now I am confused that how I am going to calculate the size of the int[] donations array inside int maxDonations(int[] donations) function.
Definition
Class: BadNeighbors
Method: maxDonations
Parameters: int[]
Returns: int
Method signature: int maxDonations(int[] donations)
(be sure your method is public)
If you want, I can post the entire question.
A cheap way would be to wrap the array into a struct and pass that (or a pointer to the struct).
However, I would recommend passing an additional size_t argument that specifies the size of the array. This would make your function more flexible.
In C you cannot pass an array as a parameter to a function directly. You can pass a structure, a pointer or some other numeric value, but not an array.
Based on the constraints in your question, this question is impossible to answer.
In C, sizeof is an operator, rather than a function: you correctly mention this in your question. However, C operators are evaluated at compile time, rather than runtime, which means that you cannot use sizeof in a function in the way you want to - at compile time the compiler cannot tell how func() is going to be called.
Looking at the problem as set, I am assuming it is this one:
http://www.cs.duke.edu/csed/algoprobs/neighbors.html
The required method signature on that page is:
public int maxDonations(int[] donations)
although for some reason, you've omitted the public keyword in your question. This keyword indicates that the language in question is not C, and this example solution looks very much as if it is written in Java, in which language this is a non-problem, arrays have a length member.
I'd suggest you clarify the requirements for the question from your teacher/website/whatever to check what is really expected.

Resources