Helloe all

I have this small program using switch statment.
the problem that when I complie this C code using gcc under Linux I get message: switch guantity not integer!!

Now, when I use switch(*hello) instead switch(hello), I got run time error: segmentation fault!!

I want to know where is the problem?

#include <stdio.h>
main()
{
int *hello;
printf("Hi, Enter any value > ");
scanf("%d",&hello);
switch (hello)
{
case 1: printf("your 1 luck number is > %d\n",hello);
case 2: printf("your 2 luck number is > %d\n",hello);
case 3: printf("your 3 luck number is > %d\n",hello);
default: printf("your 4 luck number is > %d\n",hello);
}
}

thanks

Recommended Answers

All 9 Replies

See the changes

#include <stdio.h>
[B]int [/B]main([B]void[/B])
{
int hello;[B]//* removed[/B]
printf("Hi, Enter any value > ");
scanf("%d",&hello);
switch (hello)
{
case 1: printf("your 1 luck number is > %d\n",hello);
case 2: printf("your 2 luck number is > %d\n",hello);
case 3: printf("your 3 luck number is > %d\n",hello);
default: printf("your 4 luck number is > %d\n",hello);
}
}

Are you sure you don't want break after each case?

The problem is with the hello variable.

When you write

int *hello;

// What you're saying is create a pointer called hello. 

// When you write

cout << hello;  // hello will be some arbitrary hexadecimal number(a memory address)

// When you write

cout << *hello; // *hello = the data stored at the address of hello(that hexadecimal number from before)

The problem is that you didn't create an integer. You create a pointer to an integer.

To fix the problem, you need to remove the asterisk* in front of hello in the line

int *hello;

The reason you get a segmentation fault when you try to

switch(*hello)

is probably because the switch statement uses its parameter as an int. When it compiles/runs it takes the parameter, and it translates the variable name(in this case *hello) into an address that it can use. Somewhere between the looking up of the address that is pointed to by hello.....you are trying to use some number X as a memory location. The problem is, you don't know WHAT X is, and more chance than not, it is a memory location that is not in the same data segment as your program, therefore you get a segmentation error.

I hope this helps.
- Dano

Thanks sunnypalsingh
Thanks Nanodano

but I need hello to be a pointer because it might be passed as an argument to some function (like in case of use 'recv' in socket programing) in the program.
Actually I submet a very simple program but what if the program read hello from the keyboard and send it to another side in a socket connection, ofcourse, I will need hello to be a pointer.

so the change must be somewhere in the program rather than the decleration of hello.
but where?!!

thanks again
Moloza

Hey,

I found a solution!

Simply force type-cast it as an int.

switch(int(hello))

I tried it out with gcc under Linux, like you said you had tried. It worked for me. I hope it works with your current situation. I'm sorry my last one wasn't helpful, I didn't think about why it would need to be a pointer. I better keep things like that in mind!

I found a solution!

Simply force type-cast it as an int.

switch(int(hello))

That's not a solution, that's just hiding a problem. Solutions were already presented.

How is that hiding a problem vs a solution? I agree that removing the * and not letting hello be a pointer is changing something. Casting hello (which is declared as an int) to an int isn't changing anything.

What is the problem? The compiler isn't recognizing hello as an int.

But hello IS an int. So, all you need to do is TELL the compiler: "Hey, that IS an int, use it like one."

What would you consider a "solution" then?

Maybe try a different compiler?

How is that hiding a problem vs a solution? I agree that removing the * and not letting hello be a pointer is changing something. Casting hello (which is declared as an int) to an int isn't changing anything.

Clue: a pointer is not an integer. Merely telling the compiler that you know that a point is an integer to keep it quiet is hiding the real problem, that the value is not being read into an integer.

Well,

I suppose a solution to that would to declare one variable as a pointer and one as an int.

Maybe something like:

int hello;
int *hello_ptr;
hello_ptr = &hello;

This way hello really is an int, and you still have a pointer to it.

Or just do it like it was already presented.

I guess the real issue for moloza is this.

but I need hello to be a pointer because it might be passed as an argument to some function (like in case of use 'recv' in socket programing) in the program.
Actually I submet a very simple program but what if the program read hello from the keyboard and send it to another side in a socket connection, ofcourse, I will need hello to be a pointer.

No moloza, you don't need a pointer. The address of your variable will become a pointer to it. Reread sunnypalsingh's post a couple more times. There a pointer to hello is being passed to scanf .

BTW, the original is passing a pointer to a pointer.

int *hello;
scanf("%d",&hello);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.