Thread: char pointers in C

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    6

    char pointers in C

    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int i;
    char *s;
    scanf("%s",&s);
    printf("%s",s);
    getch();
    }
    i ran this code on dev c++ in windows xp...
    xp shows a error:
    " ....has encountered a problem and needs to close.
    We are sorry for the inconvenience."

    why does this happen??
    and how to receive char pointers as input??

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by Rishi Kumar View Post
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int i;
    char *s;
    scanf("%s",&s);
    printf("%s",s);
    getch();
    }
    i ran this code on dev c++ in windows xp...
    xp shows a error:
    " ....has encountered a problem and needs to close.
    We are sorry for the inconvenience."

    why does this happen??
    and how to receive char pointers as input??
    You have to allocate memory for 's' either at initialization or at runtime using malloc.
    Also,No ampersand for scanf since string is the input.
    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

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    6
    i am a beginner.... i have never used malloc be4... how to use it...
    this program works perfectly in turbo c 2.01....
    (removing "&" makes no difference)...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sf.net/A_pointer_on_pointers
    If that doesn't make sense, you have to go back to the book.
    Also note that scanf is dangerous: http://cpwiki.sf.net/Buffer_overrun
    And implicit main is bad: http://cpwiki.sf.net/Implicit_main

    And if you don't know how malloc works, then maybe it's back to the book again. C is not an easy language. Make sure you properly understand what the functions do before you use them.
    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.

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Like this:
    with malloc:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int i;
        char *s;
        s=(char *)malloc(10);
        scanf("&#37;s",s);
        printf("%s",s);
    
        return 0;
    }
    or
    without malloc:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int i;
        char s[10];
        scanf("%s",s);
        printf("%s",s);
    
        return 0;
    }
    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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not cast the return of malloc.
    Do not read strings with scanf without specifying buffer size.
    FREE the memory after you're done with it.
    (Read links!)
    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.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    6
    Are char pointers an alternative to strings...???

  8. #8
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Thanks for the inputs Elysia.
    I'll try my best to improve my coding.
    If I do not cast malloc I get the Following error:

    error: invalid conversion from `void*' to `char*'

    Can you please explain how to do it.
    Last edited by stevesmithx; 05-23-2008 at 02:39 AM.
    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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Rishi Kumar View Post
    Are char pointers an alternative to strings...???
    No, they are not. Char pointers can be used, and are frequently used, as strings, but they are not strings (they are a pointer to somewhere in memory that typically contains a character array).
    You seem to lack basic understanding of how C works. Do you have a book?

    Quote Originally Posted by stevesmithx
    Thanks for the inputs Elysia.
    I'll try my best to improve my coding.
    Good luck
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If I do not cast malloc I get the Following error:

    error: invalid conversion from `void*' to `char*'
    You are probably compiling the code in a C++ compiler. In C, the recommended practice is not to have that cast, but in C++, a cast is necessary.
    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

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    6
    i dont have a fixed book for studying c...
    i refer to tutorials and books then n thr...
    any really gud somewat complicated book u wud recommend??

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by stevesmithx View Post
    If I do not cast malloc I get the Following error:
    error: invalid conversion from `void*' to `char*'
    Can you please explain how to do it.
    If using C++, then you must cast the result as laserlight points out.
    But be careful not to suggest it to other people who use C, because with a C compiler, it's a very bad thing(TM).

    Quote Originally Posted by Rishi Kumar View Post
    i dont have a fixed book for studying c...
    i refer to tutorials and books then n thr...
    any really gud somewat complicated book u wud recommend??
    See the recommended books thread on the forum.
    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.

  13. #13
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Thank you laserlight and Elysia.

    I am using Code::Blocks IDE with GCC compiler.

    Still I am getting this error.
    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

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since you are compiling C in C++, it must be:
    char* s = (char*)malloc(10);
    (Note the cast.)
    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.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am using Code::Blocks IDE with GCC compiler.

    Still I am getting this error.
    Make sure that your file extension is .c, not .cpp, .cxx, .cc or .C (etc).
    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

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