Like Tree1Likes

char pointers in C

This is a discussion on char pointers in C within the C Programming forums, part of the General Programming Boards category; thanx Elysia... i dont get any error with fgets... i use dev c++ with gcc compiler... but i get a ...

  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
    501
    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
    21,933
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    21,933
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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.

Page 2 of 2 FirstFirst 12
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, 04: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, 09:14 PM

Tags for this Thread


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