C Programming - Different signature problem with extern variables

One of my friend asked me this question few days back which seemed to be very interesting for me atleast. The question was as follows





File1.c File2.c
1  int arr[80];
 
1 extern int *arr;
2 int main()
3 {
4 arr[1] = 100;
5 return 0;
6 }

What will be the output when complied and linked together?



It was surprising for me to see a segmentation fault for this kind of a code. Even though array and pointers are different in aspects like
1. Array variable is const pointer
2. Array size has to be defined while declaring the variable

Still I thought array will be manipulated like a pointer, but here in this case what is happening is when we are putting the value 100 to arr[1] it taking the value of the pointer here in this case 0 as arr[80] will be filled with zeros and then adding one to that which mean resulting value will be 0x4 as it is a pointer addition. After that it is trying to put 100 into the address pointed by 0x4 which results in a segmentation fault.

I don't have a sincere justification for this other than explaining why it is working like this. If anyone have any response please post it as a comment.

Labels:

Posted by - at 8:18 am | 3 comments read on

Window - 013 Windows XP Commands

Some nice Windows XP commands

1. Merge two files into another - copy smalefile1 + smallfile2 largefile
2. Decompress Files - extract
3. pushd and popd same as that for bash.
4. Associate a folder to a drive - subst X: C:\windows
5. tree structure for directory - tree
6. find files or find string with in a file - find/findstr
7. Hiding files within another file(NTFS Filesystem Specific) - type 1.jpg > 1.txt:hidden.jpg
8. Alias a command - doskey ls=dir
9. Starting Add/Remove Programs - appwiz.cpl
10. Show listening process and prot - netstat -no|findstr "LISTEN"

Labels:

Posted by - at 1:06 am | 0 comments read on

Archives

  • May 2011
  • February 2009
  • December 2008
  • November 2008
  • October 2008
  • August 2008
  • April 2008
  • December 2007
  • November 2007
  • January 2007
  • December 2006
  • October 2006
  • September 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005

Links