Thread: char pointers in C

  1. #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???

  2. #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++?

  3. #18
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    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

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #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("&#37;s",s);
    getch();
    }
    it shows a warning...
    " passing arg 1 of `fgets' from incompatible pointer type "

    i accept only char* as input...

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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("&#37;s",s);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM

Tags for this Thread