Need some help with a compiler error.

This is a discussion on Need some help with a compiler error. within the C Programming forums, part of the General Programming Boards category; First off, let me say hi to everybody here. I am trying to learn C but I am finding it ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    7

    Need some help with a compiler error.

    First off, let me say hi to everybody here. I am trying to learn C but I am finding it a little cryptic compared to java.

    anwyay, I am getting an error saying warning: assignment makes integer from pointer without a cast

    on this line: buff = gets(character);

    and heres the code:

    Code:
    #include <stdio.h>
    
    int main( int argc, char * argv[] ){
    
    	int i, j = 0;
    	char buff, p[2];
    	char character[256] = {0};
    
    	if( pipe(p)==-1 ){
    		printf("pipe failed");
    		exit(-1);
    	}
    
    	printf("Enter two chars to compare: ");
    	for(i; i<2; i++) {
    		switch( fork() ){
    		case 0: 
    			buff = gets(character);
    			printf("PID of child &#37;d is %d\n", j+1, getpid() );
    			write( p[1], &buff, sizeof(char) );
    			return 0;
    			break; 
    		case -1:
    			printf("A forking error has occurred\n");
    			exit(-1);
    			break;
    		default:
    			break;
    		}
    	}
    }
    Any idea what I am doing wrong?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    1) Don't use gets().
    2) You're using gets() wrong. It returns a char *, but you defined buff as a char.
    3) If you're brand new to C, I wouldn't work on fork()ing and pipes and all of that stuff. That's kind of way over your head if you're just trying to get the basics down.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    7
    Ok, so if I am using gets() incorrectly, what is the correct usage in this case?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    http://www.cplusplus.com/reference/c...tdio/gets.html

    But with that said, you should be using fgets(). gets() allows your program to suffer from hard-to-track bugs as well as providing a means for malicious users to hijack your program.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    7
    ok so if I only want to read in one character can I do something like this:

    buff = fgets(character, 1, STDIN);

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you want to read one char, use a function like getc().

    Code:
    int c;
    c = getc();
    fgets() incurrs way too much overhead for reading 1 char, and in addition, it tries to save what it reads as a string, which means it needs a spot to put the '\0' char. If you have only 1 byte in your buffer, guess where the '\0' char is going.

    If you want to use fgets(), do something like this:

    Code:
    char *last;
    char szBuffer[BUFSIZ];
    
    if(fgets(szBuffer,sizeof(szBuffer),stdin))
    {
    	if((last = strchr(szBuffer, '\n')))
    	{
    		*last = '\0';;
    	}
    }

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    7
    ok, I made buff an int and then did this:

    buff=getc();

    and I get the following errors:
    parse error before ')' token
    too few arguments to function '__filbuf'

    sorry about the number of questions, but this is irritating me

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Sorry, I messed that one up. Any of the following will work:

    Code:
    int c;
    c = getchar();
    Code:
    int c;
    c = getc(stdin);
    Code:
    int c;
    c = fgetc(stdin);
    Some details: http://man.he.net/man3/getc

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    int getc(FILE * stream);
    Where stream is the stream to read from, stdin in this case.

    or use
    Code:
    int getchar(void); /* gets a char from stdin */
    Note that it returns int not char

    [edit]* crap I'm so slow *[/edit]

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    7
    Cool, thank you so much for all your help. I came across another snag but I have to run to work.

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21