?
Solved

why  run time error on execution code

Posted on 2011-09-16
9
Medium Priority
?
303 Views
Last Modified: 2012-06-27
#include<stdio.h>
main()
{
         int *p=10;
        printf("%d",*p);
       getch();
}
0
Comment
Question by:nagaharikola
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 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