[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

why run time error on execution code

#include<stdio.h>
main()
{
         int *p=10;
        printf("%d",*p);
       getch();
}
0
nagaharikola
Asked:
nagaharikola
1 Solution
 
Infinity08Commented:
What is supposed to be located at address 10 ?
0
 
Infinity08Commented:
Maybe you meant :
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = (int*) calloc(1, sizeof(int));
    *p = 10;
    printf("%d\n", *p);
    free(p);
    return 0;
}

Open in new window

0
 
stachenovCommented:
Because you assign a numeric value to a pointer. A pointer is a variable holding a memory address. You set it to point to a place with address "10". Since this address is probably invalid, it means that your variable points to some random place in memory or even doesn't point anywhere. Then you try to access this invalid address, so it fails. Any modern complier should issue a warning on code like this.

If you wanted the pointer to point to a varable holding the number 10, you should have created that variable first:
 
#include<stdio.h>
main()
{
       int n = 10;
       int *p=&n;
       printf("%d",*p);
       getch();
}

Open in new window

0
The eGuide to Automating Firewall Change Control

Today‚Äôs IT environment is constantly changing, which affects security policies and firewall rules. Discover tips to help you embrace this change through process improvement & identify areas where automation & actionable intelligence can enhance both security and business agility.

 
Hugh McCurdyCommented:
I'm pretty confident you didn't compile this program in Linux.  In Linux, after some modifications, this program will produce a segmentation fault.  The Wikipedia article on segmentation faults might help you to understand.

http://en.wikipedia.org/wiki/Segmentation_fault
0
 
Infinity08Commented:
@hmccurdy : a segmentation fault is a runtime error ;)
0
 
Hugh McCurdyCommented:
*after some modifications == means that I needed to modify the program just to get it to compile.  Changing getch() to getchar() and including <stdio.h> were the modifications I made.
0
 
Hugh McCurdyCommented:
Infinity, I know that a segmentation fault an element in the set of runtime errors..  I just didn't explicitly say that I knew.
0
 
Infinity08Commented:
I probably misunderstood you ... I linked the first two phrases in your post together ... ie. that you said he didn't compile under Linux because he'd have had a segmentation fault if that were the case. But it's possible that those two phrases weren't related to each other.
0
 
nagaharikolaAuthor Commented:
thanks
0

Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now