How to put a string clean into stdin?

Hey guys!

I want to call my function and it should read out the char* checkitem.
The function is called and if statement checks if item == "Apfel".
I read the input with scanf and typed Apfel - also check with printf() if the output == input. It does.

So I wonder why the if statement always turns to else even tho it should fit with the string... any specials for string comparisons...? wasnt there something like string.h and strcmp or this stuff?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>

void putItemInventory(char*);

int main()
{
	
	char* checkitem;
	scanf("%s",&checkitem);
	
	putItemInventory(checkitem);




	return 0;
}

void putItemInventory(char* item)
{
	if(item == "Apfel")
	{
		printf("Apfel in Inventory");
	}
	else
	{
		printf("Nothing");
	}
	



	return;
}
char* item is a pointer to a char array
"Apfel" is a pointer to a static field in executable file
u are comparing two pointers

you are need to use strcmp function with ! symbol

if( !strcmp(item,"Apfel"))
{

}
else
{

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <string.h>

void putItemInventory(char*);

int main()
{
	
	char* checkitem;
	scanf("%s",&checkitem);
	
	putItemInventory(checkitem);




	return 0;
}

void putItemInventory(char* item)
{
	if(!strcmp(item,"Apfel"))
	{
		printf("Apfel in Inventory");
	}
	else
	{
		printf("Nothing");
	}
	



	return;
}



Still a crash...somethings wrong with item in the function...tried *item and &item cause i cant figure it out otherwise both didn worked
does anybody can help me out?
checkitem points to a random place in memory. writing to that place results in undefined behavior.

Of course, that isn't quite what you're doing. You're interpreting the space occupied by the checkitem pointer as a string, which is equally wrong.

1
2
char checkitem[256] ;
scanf("%255s", checkitem) ;
You have not allocated memory to store the data returned by scanf.
Something like char* checkitem = malloc( 0X3FFFFFFF ); should prevent your code from crashing.
You'll need to include <stdlib.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <string.h>

void putItemInventory(char item[]);

int main()
{
	
	char checkitem[256];
	scanf("%255s",checkitem);
	
	putItemInventory(checkitem);




	return 0;
}

void putItemInventory(char item[])
{
	if(!strcmp(item,"Apfel"))
	{
		printf("Apfel in Inventory");
	}
	else
	{
		printf("Nothing");
	}
	



	return;
}




Many thanks - I made it now.

Few Questions to harden my new knowledge:

1.

char* variable; seems to be not a very good choice for a variable I need to store a value into right? At least not by a function getting stuff by the stream.
so every value stored into it has a random memory adress which can't be stored anyhow like scanf() tried to in my previous code?
Topic archived. No new replies allowed.