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; 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++ ...

  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
    494
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    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.
    Spray likes this.
    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
    494
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    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
    494
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    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
    21,660
    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.
    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

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    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
    494
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    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
    21,660
    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).
    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

Page 1 of 2 12 LastLast
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