Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
C++
#include <stdio.h>
int main() {
	//code
	int i=5;
	int *p=&i;
	printf ("%d", *p);
	switch (*p)
	{
	    case *p:
                printf ("&&");
                break;
	}
	return 0;
}


*p gives value of i. Then why is this program running into error?

What I have tried:

Switch statement works on integral expressions and pointer is not an integral expression. But i am passing the value pointed by the pointer that is 5. So should this not run?
Posted
Updated 2-Sep-18 3:02am
v3

Quote:
Using a pointer value in switch case.

Short answer: For what you want, it is if structure, not switch.
 
Share this answer
 
This one switch (*p) is ok so far. But this one case *p: is the Problem.
Why?
Have a look to the Definition of the Switch Statement in c, e.g. here: switch Statement (C)[^]
The Definition says case constant-expression : and means you Need to use a constant Expression for the "case part", while *p is a (dereferenced pointer) variable and therefore not allowed here.

I hope this helps.
 
Share this answer
 
Comments
Member 13922884 2-Sep-18 9:44am    
Should *p be not replaced with its value it is referring to?
When executed does it not go as case 5: ... ?
[no name] 2-Sep-18 9:51am    
Not sure that I understand correctly. But to make it work you should replace *p by the constant value 5:

case 5:

Again, the case value needs to be a constant value (in C) and therefore a variable is not allowed here.
Member 13922884 2-Sep-18 10:51am    
Ohh.. my bad. I got your point. Thanks a lot.
[no name] 2-Sep-18 11:04am    
You are very welcome, it helped also me to learn. Thank you for accepting.
From switch statement in C[^]
Quote:

The following rules apply to a switch statement

- The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

- You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

- The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

- Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.

- A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

I think your pointer is not fitting in rule number one.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900