Like Tree3Likes

running through a loop using pointers

This is a discussion on running through a loop using pointers within the C Programming forums, part of the General Programming Boards category; I am working on a homework assignment an have spent quite some time trying to figure out why my code ...

  1. #1
    boy
    boy is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    16

    running through a loop using pointers

    I am working on a homework assignment an have spent quite some time trying to figure out why my code is not running right when I convert my loop to using pointers. I have gone through and debugged my code and found that it is sending a segmentation fault when it hits the if statement the sends it back through the loop. If anyone can help me it would be greatly appreciated.
    Code:
     
    #define ARRAY_SIZE   10000
    
    double  *array = calloc(ARRAY_SIZE, sizeof(double));
    double  sum = 0;
    
    int *max, *end;
    max = &end[ARRAY_SIZE];
    								
    loop:   //loops through array and adds up all elements of array
        sum = sum + array[0] + array[1] + array[2]; 
    
    if(*array < *max) // this is where the debugger throws the segmentation fault error
    	goto loop;
    This is not my whole code, but I know the other parts of the code work, so I am just posting the relevant parts.

    Thanks
    boy
    Last edited by boy; 08-18-2011 at 05:59 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there some reason you are using a goto instead of for or while or do while? Also, you are derefererencing the pointers to control your loop. You shouldn't be.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    boy
    boy is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    16
    Thanks quzah for the quick reply. The point of the assignment was to optimize the loop, after trying the for and while loop methods, I found it faster to use the goto; for some reason. I am new to pointers so I was trying to follow an example to work with my code. If I don't add the de-referencing symbol, the compiler gives me the warning:" assignment makes pointer from integer without a cast".

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You may benefit from reading through:
    Lesson 3-Loops
    Lesson 6-Pointers
    Lesson 8-Arrays
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by boy View Post
    Thanks quzah for the quick reply. The point of the assignment was to optimize the loop, after trying the for and while loop methods, I found it faster to use the goto; for some reason. I am new to pointers so I was trying to follow an example to work with my code. If I don't add the de-referencing symbol, the compiler gives me the warning:" assignment makes pointer from integer without a cast".
    goto has fallen to disfavour because it causes more problems than it solves. Trust me, while(), for() and do-while() are every bit as fast and far less likely to mess up.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by boy View Post
    Code:
    loop:   //loops through array and adds up all elements of array
        sum = sum + array[0] + array[1] + array[2];
    when do you add the other elements of your array?


    Quote Originally Posted by boy View Post
    Code:
    int *max, *end;
    max = &end[ARRAY_SIZE];
    It throws the segfault because you never initialize end to anything, yet then you assign some random value to max. You should read through your book again.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by boy View Post
    The point of the assignment was to optimize the loop, after trying the for and while loop methods, I found it faster to use the goto; for some reason. I am new to pointers so I was trying to follow an example to work with my code. If I don't add the de-referencing symbol, the compiler gives me the warning:" assignment makes pointer from integer without a cast".
    I'd worry about actually getting it working before I started optimizing anything.
    Code:
    int *max, *end;
    max = &end[ARRAY_SIZE];
    What is it you think end is pointing at here?
    Code:
    array = allocate space
    for( current = array, end = array + ARRAY_SIZE; current < end; current++ )
        ...do stuff

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I see no reason why &end[ARRAY_SIZE] should even exist, let alone be a reasonable thing to assign to max.

  9. #9
    boy
    boy is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    16
    Thank you everyone for helping me. Thanks Andrew for giving me the links, they actually were much clearer than my book. To be honest, I wasn't really sure how the &end[ARRAY_SIZE] worked at all. So I went back and change my code to clear up all the mess following all of your input and initialized the pointer (which my book never mentioned). When I tried to compile it again the compiler said: assignment makes pointer from integer without a cast. which points to the max pointer
    Code:
    int *max = malloc(sizeof(int));
    max = ARRAY_SIZE;
    am I assigning it right?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. Your compiler should be complaining at you.

    1. You only allocate 1 int. (That may be what you intended, but I doubt it.)
    2. You immediately toss away what you have assigned by assigning it something else (an integer, instead of an address).


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by boy View Post
    This:
    Code:
    int *max = malloc(sizeof(int));
    max = ARRAY_SIZE;
    should be:
    Code:
    int *max = malloc(ARRAY_SIZE * sizeof(int));
    EDIT: That is assuming, you want max to be your array.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    boy
    boy is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    16
    yea, max is the max size of my array. I am getting a comparison error now when I run through the loop comparing the array and max, this is my while statement:
    Code:
    while(array < max)
    I have tried using Quzah's example for my loop,
    array = allocate space
    for( current = array, end = array + ARRAY_SIZE; current < end; current++ )
    ...do stuff
    but I am still confused at how to set current up to work, I have tried making it a pointer, but it still gives me the comparison error when i compile it, am I suppose to make current another array?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "The" comparison error? There are more than one.

  14. #14
    boy
    boy is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    16
    oh, sorry, the error says: comparison of distinct pointer types lack cast

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if current and end are supposed to point to the same kind of thing (which they are in your example, since they're pointing in the same array), then they really need to be of the same type.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop not running properly
    By Todd88 in forum C++ Programming
    Replies: 21
    Last Post: 04-04-2008, 06:27 PM
  2. Running a program in a loop
    By Robert_Sitter in forum C# Programming
    Replies: 7
    Last Post: 01-27-2006, 10:09 AM
  3. User input while loop is running
    By two31d in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2005, 05:28 PM
  4. functions stops loop from running
    By a1pro in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 03:02 PM
  5. Running triangles through loop
    By shanedudddy2 in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2003, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21