LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2003, 02:45 PM   #1
KneeLess
Member
 
Registered: May 2003
Distribution: Debian GNU/Linux 3.0 Sid, OpenBSD 3.5
Posts: 190

Rep: Reputation: 30
Help needed using char arrays, %s, and if() in C


Okay. I'm trying to learn C (I already have a pretty good knoweledge of C++ and I am no newbie to programming.). Now I was just reading about %d, and %s. I decided to see if I could take a "command" if you will from the user and display output. If they typed "kneeless" then it would print "kneeless! My Name too!". But for some reason it compiles 100% fine and then always goes to the else statement, never to the if(). Here is the code:

Code:
#include <stdio.h>

int main()
{
	char word[64];
	printf("\nPlease type my name: ");
	scanf("%s", word);
	if (word == "kneeless")
	{
		printf("%s! My name too!\n", word);
	}
	else
	{
		printf("%s is not my name.\n", word);
	}
	return 0;
}
Compiles fine. Doesn't do what I want. It's probably something stupid I overlooked. Can anyone help me out?

Last edited by KneeLess; 08-13-2003 at 02:57 PM.
 
Old 08-13-2003, 02:54 PM   #2
jqcaducifer
Member
 
Registered: Jul 2003
Distribution: Fedora 3
Posts: 133

Rep: Reputation: 15
in scanf you need to point the %s towards the address of word; like thus:
Code:
scanf("%s", &word);
there was some reason why scanf has to be stupid, involving reading to the memory address and stuff, which i forgot.
 
Old 08-13-2003, 02:57 PM   #3
KneeLess
Member
 
Registered: May 2003
Distribution: Debian GNU/Linux 3.0 Sid, OpenBSD 3.5
Posts: 190

Original Poster
Rep: Reputation: 30
Right you are. Simple mistake. But doesn't solve the problem.

New code (still doesn't work, still compiles):
Code:
#include <stdio.h>

int main()
{
	char word[64];
	printf("\nPlease type my name: ");
	scanf("%s", &word);
	if (word == "kneeless")
	{
		printf("%s! My name too!\n", word);
	}
	else
	{
		printf("%s is not my name.\n", word);
	}
	return 0;
}
 
Old 08-13-2003, 03:08 PM   #4
jqcaducifer
Member
 
Registered: Jul 2003
Distribution: Fedora 3
Posts: 133

Rep: Reputation: 15
well, what does the printf in the else thing say "word" is? if it says "kneeless is not my name" then clearly there's something wrong with the if statement, but if the output is a garble of letters, then its to do with scanf and the char work[64]; part...so whats the output?

EDIT
and also try doing if (word[] == "kneeless");

EDIT2
yah...ignore everything i said and read the next post

Last edited by jqcaducifer; 08-13-2003 at 03:10 PM.
 
Old 08-13-2003, 03:09 PM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
do you know what a pointer is? look it up before you read on. firstly you were correct initially there should be no & in front of word because word on its own is a constant pointer to the first byte of the array. ie word == &word[0]. now for the actual problem, as i said above, word is a pointer to the first byte of the 64 byte array and "kneeless" evaluates to a constant pointer to the array {'k', 'n', 'e', 'e', 'l', 'e', 's', 's', 0} stored in the programs data section. these two pointers are NOT equal they point to completely different parts of memory. what you want to compare is not the pointers but the actual array itsself, the standard way to do this is the strcmp() function defined in string.h

modifications to your code
Code:
#include <stdio.h>
#include <string.h>

int main()
{
	char word[64];
	printf("\nPlease type my name: ");
	scanf("%s", word);
	if (!strcmp(word, "kneeless")) //strcmp returns 0 if strings equal 
	{
		printf("%s! My name too!\n", word);
	}
	else
	{
		printf("%s is not my name.\n", word);
	}
	return 0;
}

Last edited by kev82; 08-13-2003 at 03:14 PM.
 
Old 08-13-2003, 08:42 PM   #6
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Yeah for c++ strings!

Hehe...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C pointers confusion - char ** = char [][] ?? saravkrish Programming 12 12-02-2004 10:06 AM
C Problem---convert char to char* totti10 Programming 11 11-06-2004 11:32 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM
Comparing Char arrays n_ick2000 Programming 5 02-25-2003 09:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration