Thread: simple program can't run

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    14

    simple program can't run

    so i started learning about pointers today. I was doing a simple example but whenever i run the codes there is a pop up window saying "if.exe has stopped working" and then it says "a problem caused the program to stop working correctly. windows will close the program and notify you if a solution is available." then i close the pop up window and my program ends without doing anything. the message on program says something like this "process returned 255<0xFF> execution time :178.950s press any key to continue." what is wrong? Thank you for helping me out.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
     int *p,a=2;
     *p=&a;
     printf("%d",*p);
    
    
        return 0;
    }

  2. #2
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    You're trying to set the value at whatever storage location pointer *p points at to the storage location (address) of integer a. Because pointer *p has not been initialized it could contain a garbage value (which represents a storage location) and point to somewhere random in memory. Overwriting this value (the value at a random address in memory) will result in undefined behaviour causing your program to crash.

    You need to set the value of pointer p to the address of integer a.
    Last edited by Ktulu; 09-25-2015 at 05:08 PM.
    This parameter is reserved

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    It is
    Code:
    p = &a;

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by endrick View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int *p,a=2;
        *p=&a;
        printf("%d",*p);
    
    
        return 0;
    }
    The asterisk in line 6 and 7 have differend meanings.
    Code:
        int *p,a=2;
    This line declare p as a pointer to int.
    The asterisk mean, that p is an pointer.
    Code:
        *p = &a;
    In this line the asterisk mean, that p should be dereferenced.
    But this lead to an segmentation fault, because p is declared but not defined.
    The value of p is actual garbage.

    The line from Satya (post #3) is correct.
    It define the value of p to the address of a.
    Other have classes, we are class

  5. #5
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    Thank you. I just realized books lie sometimes. What i did was an example from a website i'm taking certificate course on. I don't know if it is me who didn't understand or if it is them who wrote wrong stuff. anyway thank you.

  6. #6
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    thank you all guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM