Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

noob help

Status
Not open for further replies.

inabox

Programmer
Nov 3, 2005
3
AU
I'm new to C, but have been programming a couple of years. I've been getting some curious errors with this code and I just don't understand what's wrong.

Here are the errors:
seek_word.c:10:30: missing terminating " character
seek_word.c: In function `main':
seek_word.c:11: warning: comparison between pointer and integer
seek_word.c:11: error: parse error before "word"
seek_word.c: At top level:
seek_word.c:16: error: parse error before string constant
seek_word.c:16: warning: conflicting types for built-in function `printf'
seek_word.c:16: warning: data definition has no type or storage class

Here's the code:
#include <stdio.h>

int main() {
char * input;
char * word;
char * sentence;
printf(">> ");
scanf("%s", input);
int i;
for(i=0,word!="\n"",i++) {
word = input;
}
for(i,word!="\n",i++) {
sentence = input;
}
printf("word: %s.\nsentence: %s\n", *word, *sentence);
}

I'd really appreciate if someone could help me out.

Thanks,
inabox
 
In C, all the declarations have to be before the code. Your int i should be before the printf. Also, your for loop is wrong. It should read
Code:
        for(i=0;word[i]!='\n';i++) {
                word[i] = input[i];
        }
        for(i;word[i]!='\n';i++) {
                sentence[i] = input[i];
        }
Also you have to allocate the space for input, word and sentence before using it.
 
Great thanks.
One more thing. If I used scanf("%s", this_is_a_string) what would the last character of this_is_a_string be? '\0'?
 
the format of scanf should be scanf("%s",&var),so it's wrong , i think ,
 
It's a pointer (an array of characters) so the & isn't necessary.
 
the '\0' will be added to the end of the string of cause,if not there will be an error when printf is used to print the string on the screen.
 
Well, you have much more serious problems in your code. At first, there are a lots of syntax errors in the snipped (for example, double quotes in "\n""). But the most problem:
Code:
char* input;
...
scanf("%s", input);
Oops, you have uninitialized pointer (referenced to nowhere;) then try to scan user input and place it via that pointer to nowhere (then have a memory crash and erratical program behaviour;).

Declare true input buffer (char array): char input[128], for example.

And don't forget: %s format specificator stops the input at 1st whitespace (not at end of line). So with input string It's me, God!" you will get It's only...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top