943,900 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 922
  • C RSS
Jan 18th, 2009
0

Segmentation Error

Expand Post »
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Jan 18th, 2009
1

Re: Segmentation Error

>p_arr[i]=insert;
Tell me the value of i in this line of code.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 18th, 2009
0

Re: Segmentation Error

Click to Expand / Collapse  Quote originally posted by Narue ...
>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...
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Jan 18th, 2009
0

Re: Segmentation Error

Click to Expand / Collapse  Quote originally posted by atman ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 19th, 2009
1

Re: Segmentation Error

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 19th, 2009
0

Re: Segmentation Error

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. }
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 19th, 2009
1

Re: Segmentation Error

>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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 19th, 2009
0

Re: Segmentation Error

Point taken, sorry.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 24th, 2009
0

Re: Segmentation Error

thank you guys so much!!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: about file handling
Next Thread in C Forum Timeline: Custom function help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC