Segmentation Error

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Segmentation Error

 
0
  #1
Jan 18th, 2009
  1. #include<stdio.h>
  2. #define COUNT 5
  3.  
  4.  
  5. void enter(int *p_arr);
  6. void report(int arr[], int count);
  7.  
  8. int main()
  9. {
  10.  
  11.  
  12. int arr[5];
  13. int i;
  14. for(i=0; i < COUNT; i++)
  15. {
  16. enter(&arr[i]);
  17. }
  18.  
  19. report(arr, COUNT);
  20.  
  21.  
  22. }
  23.  
  24. void enter(int *p_arr)
  25. {
  26. int i;
  27. int insert;
  28. printf("\nplease enter the number:");
  29. scanf("%d", &insert);
  30. p_arr[i]=insert;
  31. }
  32.  
  33.  
  34. void report(int arr[], int count)
  35. {
  36. int i;
  37. printf("The numbers you have entered are:");
  38. for(i=0; i< count; i++)
  39. printf("\n%d", arr[i]);
  40. }

Hello Guys.,
I'm new and learning C, when i compiled and ran this program on my mac in bash using gcc, it works... on our school server however it compiles but after the first entry it sais "Segmentation Error" and quits, any thoughts would be greatly appreciated!
Thanx!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Segmentation Error

 
1
  #2
Jan 18th, 2009
>p_arr[i]=insert;
Tell me the value of i in this line of code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Re: Segmentation Error

 
0
  #3
Jan 18th, 2009
Originally Posted by Narue View Post
>p_arr[i]=insert;
Tell me the value of i in this line of code.
depends on the pass if its the first pass then 0, if second then 1, e.c.t...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,039
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Segmentation Error

 
0
  #4
Jan 18th, 2009
Originally Posted by atman View Post
depends on the pass if its the first pass then 0, if second then 1, e.c.t...
Nope. That i has the value of "segmentation fault".
It never gets initialized.
If you want to pass it a value you need to do it as an argument.
void enter(int *p_arr, int i); Make sure the passed int contains a meaningful value.
Last edited by Aia; Jan 18th, 2009 at 10:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Segmentation Error

 
1
  #5
Jan 19th, 2009
>depends on the pass if its the first pass then 0, if second then 1, e.c.t...
Bzzt! Wrong. The correct answer is "I don't know". Though I would have accepted "indeterminate" as well. Technically you don't need i at all in that function because you pass the address of the correct array element:
  1. void enter(int *p_arr)
  2. {
  3. int insert;
  4. printf("\nplease enter the number:");
  5. scanf("%d", &insert);
  6. *p_arr=insert;
  7. }
However, because you use p_arr[i] where i is indeterminate (usually this means a large number), then you're pretty sure to index the array well out of its bounds.

If you want to continue using array notation, this will solve the problem immediately (though it's technically identical to my previous fix):
  1. void enter(int *p_arr)
  2. {
  3. int i = 0;
  4. int insert;
  5. printf("\nplease enter the number:");
  6. scanf("%d", &insert);
  7. p_arr[i]=insert;
  8. }
Note that i is always 0, because p_arr always points to the element you want to initialize. You can also change your logic as Aia suggested such that the index actually makes sense:
  1. int main()
  2. {
  3. int arr[5];
  4. int i;
  5.  
  6. for ( i = 0; i < COUNT; i++ )
  7. enter ( arr, i );
  8.  
  9. report ( arr, COUNT );
  10.  
  11. return 0;
  12. }
  13.  
  14. void enter ( int *p_arr, int i )
  15. {
  16. int insert;
  17.  
  18. printf ( "\nplease enter the number:" );
  19. scanf ( "%d", &insert );
  20. p_arr[i] = insert;
  21. }
Last edited by Narue; Jan 19th, 2009 at 9:41 am.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Segmentation Error

 
0
  #6
Jan 19th, 2009
If you have to use array notation, also consider using a static variable instead of a parameter:
  1. void enter(int *p_arr)
  2. {
  3. static int insert = 0;
  4. printf("\nplease enter the number:");
  5. scanf("%d", &insert);
  6. p_arr[i++]=insert;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Segmentation Error

 
1
  #7
Jan 19th, 2009
>If you have to use array notation, also consider
>using a static variable instead of a parameter
That's a horrible suggestion. You've managed with one change to ruin the function's flexibility, potential error handling, actual error handling, and thread safety. Congratulations, this gets my vote for the most damaging minor change of the week award.

p.s. Your code is broken, so I mentally changed it to this:
  1. void enter(int *p_arr)
  2. {
  3. static int i = 0;
  4. int insert;
  5. printf("\nplease enter the number:");
  6. scanf("%d", &insert);
  7. p_arr[i++]=insert;
  8. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Segmentation Error

 
0
  #8
Jan 19th, 2009
Point taken, sorry.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Re: Segmentation Error

 
0
  #9
Jan 24th, 2009
thank you guys so much!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC