I am new to programming. This is a C language program.
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdbool.h>
#define ture 1
#define false 0
void add(int m, int* arr,int n)
{
if (n == 32) return;
arr[n] = m;
if ( arr[n] > 1)
{
arr[n] = 0;
add(m, arr, n);
}
return;
}
int main(void)
{
int T,n,r,m,i,j,k;
bool check = ture;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &r);
switch (r)
{
case 10:
printf("%d", n);
break;
case 2:
int arr2[32] = { 0 };
if (n > 0)
{
for (i = 0; i < 32 ; i )
{
arr2[i] = n % 2;
n = n / 2;
}
for (j = 31; j >= 0; j--)
{
if (arr2[j] == 0 && check == ture) continue;
else
{
check = false;
printf("%d", arr2[j]);
}
}
}
else if (n == 0)printf("%d", 0);
else if (n < 0)
{
n = -n;
for (i = 0; i < 32; i )
{
arr2[i] = n % 2;
n = n / 2;
}
for (k = 0; k < 32; k )
{
arr2[k] = !arr2[k];
}
add(1, arr2, 0);
for (j = 31; j >= 0; j--)
{
if (arr2[j] == 0 && check == ture) continue;
else
{
check = false;
printf("%d", arr2[j]);
}
}
break;
}
case 8:
int arr8[11] = { 0 };
if (n > 0)
{
for (i = 0; i < 11; i )
{
arr8[i] = n % 8;
n = n / 8;
}
for (j = 10; j >= 0; j--)
{
if (arr8[j] == 0 && check == ture) continue;
else
{
check = false;
printf("%d", arr8[j]);
}
}
}
}
}
return 0;
}
When I run the program in VS2022.There is a bug. Error C2360 Initialization of "arr2" is skipped by "case" tag Project5 C:\code\C\C_Single\Project5\Project5\test.cpp 74 I don't understand why this is happening. In my opinion,when I select the contents of case8, I don't need the contents of case2, certainly,including the declaration of arr2.But obviously the compiler doesn't think that way. So I turn to google for help. However,google tells me something like this. Your search - Error C2360 Initialization of "arr2" is skipped by "case" tag - did not match any documents.
Suggestions:
Make sure that all words are spelled correctly. Try different keywords. Try more general keywords. Try fewer keywords. So I want to get help in stackoverflow.Can anyone help me?
CodePudding user response:
This is one reason that goto
statements are frowned upon in modern code.
A case label is not much more that a regular label, and the switch will do something like:
if(value==2) goto label2;
if(value==3) goto label3;
etc.
But when you declare an array like:
int arr[10];
or actually any variable that goes on the stack, the compiler needs to increase the stack pointer to make space for that. in this case:
sp = 10 * sizeof(int)
(Of course this depends on your system/compiler etc) So what happens if you put this piece of code, in between to (case) labels...
label2:
//int arr[10];
sp = 10*sizeof(int);
label3:
...
// end of scope for arr
sp -= 10*sizeof(int);
// or not?
Yeah it happens only half the time. So now you end up at the end of you switch statement, and your compiler doesn't know weather to decrease the stack pointer or not.
CodePudding user response:
The compiler warns you that the initialization of the array arr2 can be skipped if the control will be passed to the label case 8:. In this case the array will have indeterminate values.
To avoid the compiler message just enclose statements after labels in braces creating a compound statement like for example
case 2:
{
int arr2[32] = { 0 };
//...
}
CodePudding user response:
You have several problems here, within your switch()
. (I'm just going to focus on that.)
Firstly, declaring variables within case
clauses is problematic: providing them with initialisers, even more so. If you enclose the entire case
clause within curly braces, that's a lot better. You also constrain the scope of your case-dependent variables to within that specific case.
Secondly, you have a significant logic error in your switch()
statement. Your case 2:
clause only hits a break
in the n < 0
instance: in all others, it will fall through to case 8:
. This is very clearly incorrect.
Thirdly, your case 8:
clause has no break
statement. As it's the last in the switch()
, that's benign - you'll "fall out the bottom", but it's bad practice.
Finally, there is no default:
clause. In just about every situation you use a switch()
you want to catch the default:
case, as it's either going to need a default handling, or it indicates an error condition.
In summary: brace your case
clause code, so you can do as you with with, and scope, the variables you declare, and be rigorous about break
and default:
use. You'll thank me in the future!