[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

why  run time error on execution code

Posted on 2011-09-16
9
Medium Priority
?
306 Views
Last Modified: 2012-06-27
#include<stdio.h>
main()
{
         int *p=10;
        printf("%d",*p);
       getch();
}
0
Comment
Question by:nagaharikola
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 36548653
What is supposed to be located at address 10 ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 36548667
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
 
LVL 4

Expert Comment

by:stachenov
ID: 36548679
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36549898
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 36549912
@hmccurdy : a segmentation fault is a runtime error ;)
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36549915
*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
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36550237
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 36551029
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
 

Author Closing Comment

by:nagaharikola
ID: 36565850
thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question