7 Replies - 519 Views - Last Post: 23 March 2020 - 10:22 PM Rate Topic: -----

#1 JOHN051518   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 20

[Warning] passing argument 1 of 'gets' from incompatible point

Posted 22 March 2020 - 03:35 PM

[Warning] passing argument 1 of 'gets' from incompatible pointer type

I want to create a program that allows me to input the student full name that's why I used the function "gets" however when I run my program it will only read the input on name, age and telephone it won't read the address why is this happening ? can someone help me? I still new to this programming thing

#include<stdio.h>
#include<string.h>
#include<conio.h>

struct data
	{
		char name[10][40];
		int age[10];
		char address[10][40];
		char telephone[10][40];
	}d;
main()
{

	char ans='y';
	int i;
	
	i=1;
	while(ans=='y'||ans=='Y')
	{
		
		printf("Enter name: ");
		gets(d.name[i]);
		
		printf("Enter age: ");
		scanf("%d",&d.age);
		
		printf("Enter address: ");
		gets(d.address[i]);


		printf("telephone: ");
		scanf("%s",&d.telephone[i]);
		
		
		
		i++;
		printf("Enter another [Y/N]? ");
		scanf("%s",&ans);

	}
getch();
	}


can someone please help i really need it for my project......

Is This A Good Question/Topic? 0
  • +

Replies To: [Warning] passing argument 1 of 'gets' from incompatible point

#2 Ornstein   User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 107
  • Joined: 13-May 15

Re: [Warning] passing argument 1 of 'gets' from incompatible point

Posted 22 March 2020 - 04:06 PM

It's most likely because scanf() doesn't consume the newline from the first input - and gets() reads until a newline is encountered - which in this case is immediately because there's still a newline in the stream.

You could perhaps try something like this:

printf("Enter name: ");
scanf(" %s", &d.name[i]);

printf("Enter age: ");
scanf(" %d", &d.age);

printf("Enter address: ");
scanf(" %s", &d.address[i]);

printf("telephone: ");
scanf(" %s", &d.telephone[i]);


(The leading whitespace in the first argument to scanf() tells it to consume whitespaces/newlines first.)

There's more reliable/secure approaches, but maybe you can look into that in more detail another time.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg   User is online

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: [Warning] passing argument 1 of 'gets' from incompatible point

Posted 22 March 2020 - 04:09 PM

The first thing is to never Never NEVER use gets! That function is so dangerous that it has actually been removed from the language. Use fgets() instead.

Also don't forget that in C arrays start at zero, not one.

Next why are all the structure elements arrays?

Wouldn't creating an array of your structure in main() make more sense?

Lastly for now, main() should be defined as returning an int, int main(), using default return types is no longer supported by the language, I suggest you insure that you compile the program to the C11 standard.


Jim
Was This Post Helpful? 3
  • +
  • -

#4 JOHN051518   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 20

Re: [Warning] passing argument 1 of 'gets' from incompatible point

Posted 23 March 2020 - 01:12 AM

thank you for that reply it is really helpful but scanf will only read on word and it's not so good specially at my case which might input two words...
I updated my program but when it passes through the first loop it skipped the name input part why is that?
struct data
	{
		char name[10][40];
		char age[10][40];
		char address[10][40];
		char telephone[10][40];
	}d;
main()
{

	char ans;
	int i;
	ans='y';
	//input statements
	i=0;
	while(ans=='y'||ans=='Y')
	{
		
		printf("Enter name: ");
		fgets(d.name[i],100,stdin);
		
		printf("Enter age: ");
		fgets(d.age[i],100,stdin);

		printf("Enter address: ");
		fgets(d.address[i],100,stdin);
		
		printf("telephone: ");
		fgets(d.telephone[i],100,stdin);
				
		i++;
		printf("Enter another [Y/N]? ");
		scanf("%c",&ans);
		printf("\n\t\t.\t\t.\n\n\t\t.\t\t.\n");

	}


Was This Post Helpful? 0
  • +
  • -

#5 Ornstein   User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 107
  • Joined: 13-May 15

Re: [Warning] passing argument 1 of 'gets' from incompatible point

Posted 23 March 2020 - 01:32 AM

Yeah scanf was never the ideal/proper solution; I just thought it might be easier to digest as a starting point.

Quote

but when it passes through the first loop it skipped the name input part why is that?


I imagine it's the same problem as before; your scanf at the end is leaving a newline in the stream - so your first fgets returns immediately.

This post has been edited by Ornstein: 23 March 2020 - 01:32 AM

Was This Post Helpful? 1
  • +
  • -

#6 JOHN051518   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 20

Re: [Warning] passing argument 1 of 'gets' from incompatible point

Posted 23 March 2020 - 06:10 AM

Sir is their a way to use fgets in a single character?

Quote

I imagine it's the same problem as before; your scanf at the end is leaving a newline in the stream - so your first fgets returns immediately.

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is online

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: [Warning] passing argument 1 of 'gets' from incompatible point

Posted 23 March 2020 - 08:08 AM

Quote

Sir is their a way to use fgets in a single character?

Not really, fgets() requires an array of characters as one of it's parameters.

First realize that mixing fgets(), and scanf() is what is causing your problems.

Here is a program demonstrating several ways to accomplish retrieving strings from the user:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STRING_SIZE 40

typedef struct Data
{
    char name[STRING_SIZE];
    int age;
    char address[STRING_SIZE];
    char telephone[STRING_SIZE];
}Data;

void eat_newline()
{
    // Now get and discard the offending new line character.
    while((getchar()) != '\n');
}

void remove_newline_from_string(char *string)
{
    // The fgets() function places the new line character in the string.
    // So we usually want to remove that character.
    string[strcspn(string, "\n")] = 0;
}

int main()
{
    // Create an array of your structure of size 10.
    Data data[10];
    char ans = 'y';
    int index = 0;

    //input statements

    while(ans == 'y' || ans == 'Y')
    {
        printf("Enter name: ");
        fgets(data[index].name, STRING_SIZE, stdin);
        remove_newline_from_string(data[index].name);

/*********************************************************************************
        Using the incorrect array size is as almost as bad as using gets().
        The second parameter must be equal to or less than the size of the array.
        fgets(d.name[i], 100, stdin);
**********************************************************************************/

        printf("Enter age: ");
        scanf("%d", &data[index].age);
        eat_newline();

        //Another way would be to use scanf() to retrieve the string.
        printf("Enter address: ");
        fgets(data[index].address, STRING_SIZE, stdin);
     
        //Another way would be to use scanf() to retrieve the string.
        printf("telephone: ");
        /* Notice the leading space, to skip leading whitespace.
           Notice the optional width specifier, which is 1 less than the string size.
           Notice the "%*c" specifier, to throw away the end of line character.
           Without that last bit the end of line character would be left in the buffer. */
        scanf(" %39[^\n]%*c", data[index].address);


        index++;
        printf("Enter another [Y/N]? ");
        // Notice the leading space, this forces scanf() to skip leading whitespace.
        scanf(" %c", &ans);
        eat_newline();
        
        printf("\n\t\t.\t\t.\n\n\t\t.\t\t.\n");

    }
}



And yes getting user input in C can get fairly messy quickly.

By the way it is often just easier to use fgets() to always retrieve user input, then process that input with sscanf().


   char buffer[40];
   char name[40];
   char answer;

   printf("Please enter the name: ");
   fgets(buffer, 40, stdin);

   // scan the buffer, until it finds the end of string character.
   // The size of the buffer should no larger than the size of name.
   sscanf(buffer, " %[^\n]", name);

   int age;
   fgets(buffer, 40, stdin);
   sscanf(buffer, "%d", &age);

   fgets(buffer, 40, stdin);
   sscanf(buffer, " %c", &answer);




Jim

This post has been edited by jimblumberg: 23 March 2020 - 08:14 AM

Was This Post Helpful? 1
  • +
  • -

#8 JOHN051518   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 20

Re: [Warning] passing argument 1 of 'gets' from incompatible point

Posted 23 March 2020 - 10:22 PM

it really solve the problem thank you so much more power !! God bless you all
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1