Thread: Tracking null pointer assgn

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    8

    Tracking null pointer assgn

    Hi
    I have declared this line in my pgm.
    char *a=NULL,*b=NULL;
    then i used
    scanf("%s",a); even i tried gets(a);
    its giving null pointer assignment error but pgm works fine and produces correct o/p;

    but when i change the pointer to array no error is there
    char a[20],b[20]; //no error for this

    but i want to use pointers. what can i do

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If you declare a pointer, and never associate heap memory with it, then the pointer is not initialized.

    When you declare and use an array, the memory is allocated for you in the program stack.

    So, you need to malloc() some storage.

    Todd
    Last edited by Dino; 01-22-2008 at 11:38 AM. Reason: typo

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to give your pointers some memory, for example with malloc().

    Given that it doesn't crash when accessing a NULL pointer hints to me that you are running in DOS-mode - for example using Turbo C - try finding a compiler that at least produces 32-bit code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And don't use gets, use fgets.
    Nor should you use scanf("%s", a). Read my signature on how to do it properly.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > its giving null pointer assignment error but pgm works fine and produces correct o/p;
    You're using DOS aren't you, which has no memory protection.

    Always remember, "works" != "bug free". You can do all sorts of dumb stuff, and the program can still manage to produce the result you expect.

    Try it on say Linux, and you're looking down the barrel of a segmentation fault.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM