(7 intermediate revisions by the same user not shown)
Line 12: Line 12:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Solution===
+
==={{Template:Author|Arjun Suresh|{{arjunweb}} }}===
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
int *a;  
 
int *a;  
 
</syntaxhighlight>
 
</syntaxhighlight>
This declares a as an integer pointer, meaning a can point to a memory address which contains an int
+
This declares a as an integer pointer, meaning 'a' can point to any memory address which contains an int
  
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
*a = 5;  
 
*a = 5;  
</syntaxhighlight> This makes the content of the address pointed to by a 5. But a is not pointing to any valid address (int *a assigns garbage value to a) and hence this assignment can cause segmentation fault
+
</syntaxhighlight>  
 +
This makes the content of the address pointed to by 'a' 5. But 'a' is not pointing to any valid address (int *a assigns garbage value to 'a') and hence this assignment can cause segmentation fault
  
 +
{{Template:FBD}}
  
 
<div class="fb-like"  data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div>
 
 
 
<div class="fb-share-button"  data-type="button_count"></div>
 
<disqus/>
 
  
 
[[Category:Coding Questions]]
 
[[Category:Coding Questions]]

Latest revision as of 13:59, 14 April 2014

<syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

int *a;
*a=5;
printf("%d",a);
return 0;

}

</syntaxhighlight>

Solution by Arjun Suresh

<syntaxhighlight lang="c"> int *a; </syntaxhighlight> This declares a as an integer pointer, meaning 'a' can point to any memory address which contains an int

<syntaxhighlight lang="c">

  • a = 5;

</syntaxhighlight> This makes the content of the address pointed to by 'a' 5. But 'a' is not pointing to any valid address (int *a assigns garbage value to 'a') and hence this assignment can cause segmentation fault




blog comments powered by Disqus

<syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

int *a;
*a=5;
printf("%d",a);
return 0;

}

</syntaxhighlight>

Solution[edit]

<syntaxhighlight lang="c"> int *a; </syntaxhighlight> This declares a as an integer pointer, meaning a can point to a memory address which contains an int

<syntaxhighlight lang="c">

</syntaxhighlight> This makes the content of the address pointed to by a 5. But a is not pointing to any valid address (int *a assigns garbage value to a) and hence this assignment can cause segmentation fault



blog comments powered by Disqus