C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-23-2008, 02:54 AM   #16
Registered User
 
Join Date: May 2008
Posts: 6
thanx Elysia...

i dont get any error with fgets...
i use dev c++ with gcc compiler...
but i get a warning: "passing arg 1 of `fgets' from incompatible pointer type"... y???
Rishi Kumar is offline   Reply With Quote
Old 05-23-2008, 02:55 AM   #17
Registered User
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 217
What's so bad about casting malloc in C? Wouldnt it be better because it will be easier to transfer your C code to C++?
39ster is offline   Reply With Quote
Old 05-23-2008, 02:56 AM   #18
and the hat of copycat
 
stevesmithx's Avatar
 
Join Date: Sep 2007
Posts: 417
Thank you laserlight.
I had saved it as .C
For .c it works.
And I thought extensions are not case sensitive.
Thanks all
__________________
Not everything that can be counted counts, and not everything that counts can be counted
- Albert Einstein.


No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
- Herbert Mayer
stevesmithx is offline   Reply With Quote
Old 05-23-2008, 02:58 AM   #19
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,362
Quote:
What's so bad about casting malloc in C? Wouldnt it be better because it will be easier to transfer your C code to C++?
read casting malloc.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-23-2008, 02:59 AM   #20
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
Quote:
Originally Posted by Rishi Kumar View Post
thanx Elysia...

i dont get any error with fgets...
i use dev c++ with gcc compiler...
but i get a warning: "passing arg 1 of `fgets' from incompatible pointer type"... y???
It means that you are trying to pass an argument to a function that expects a pointer of a certain type, but you are passing another type of pointer.
If we could see the fgets code, maybe we could help.

Example:
Code:
int x;
fgets(&x, 1, 1, f); /* passing arg 1 of `fgets' from incompatible pointer type */
(fgets is expecting char*, not int*)

Quote:
Originally Posted by 39ster View Post
What's so bad about casting malloc in C?
Because of implicit function calls are masked (in case you forgot to include the header where malloc's prototype resides). See the FAQ for more details.

Quote:
Originally Posted by 39ster View Post
Wouldnt it be better because it will be easier to transfer your C code to C++?
I certainly do agree with you there. I like to call it C++/C C code compiled with C++.
But we can't force anyone.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-23-2008, 03:03 AM   #21
Registered User
 
Join Date: May 2008
Posts: 6
Code:
#include<stdio.h>
#include<conio.h>
main()
{
char *s[10];
fgets(s, 10, stdin);
printf("%s",s);
getch();
}
it shows a warning...
" passing arg 1 of `fgets' from incompatible pointer type "

i accept only char* as input...
Rishi Kumar is offline   Reply With Quote
Old 05-23-2008, 03:06 AM   #22
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,362
In your new code example, s is an array of 10 char pointers. What you want is:
Code:
#include<stdio.h>

int main()
{
    char s[10];
    fgets(s, 10, stdin);
    printf("%s",s);
    return 0;
}
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-23-2008, 03:07 AM   #23
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
Try reading the link for implicit main and get rid of it.
The problem is

Code:
char* s[10];
Which creates an array of 10 char pointers.
Thus passing this to fgets will result in char**.
Futher, the array has no allocates storage so you'd get a crash immediately.

And it's better to use getchar() instead of getch() (it's non-standard).
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
char pointers, error, pointer input, pointers

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
seg fault at vectornew tytelizgal C Programming 2 10-25-2008 01:22 PM
Converting a circulating mouse pointer to use a Potentionmeter phoenix23 C Programming 16 10-29-2006 05:04 AM
newbie needs help with code compudude86 C Programming 6 07-23-2006 08:54 PM
Program Crashing Pressure C Programming 3 04-18-2005 10:28 PM
errors in class(urgent ) ayesha C++ Programming 1 11-10-2001 10:14 PM


All times are GMT -6. The time now is 02:12 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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