Thread: pointer math

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    39

    pointer math

    I have been stuck on a simple problem for 10 hours, can someone please help


    all im doing is the following.

    I have a function that is being passed a pointer.
    The pointer points to an int array.

    I wrote a function to find the largest value. For some reason it is failing the condition after it runs 6 times. The array is 10 long therefore it should run 10 times. Can someone please help. I have to write it this was, so yes im sure there are easier way but i need to write it the way i have it. For some other odd reason if i change 10 to 12 it works. but the array is not 12 long its 10 long.


    Code:
    //*a points to &myarray[0] inside of main. Then a is passed to the //function 
    //&a+10 should of failed when it reaches address 9 of the array
    
    for(;a<&a+10;a++){
    
    printf("This is the address of a right now [%p]\n",a);//just for          
    
    //test,prints the address im currently at.
    
    count++;//count just keeps track of how man times the loop goes off
    
    if (*a>*largest){
    
    *largest=*a;
    }
    }
    
    
    a=&a-10;
    
    for(;a<&a+10;a++){
    
    if(*a<*largest&&*second_largest<*a){
    
    *second_largest=*a;
    
    
    }
    
    
    }
    Im sorry its so messy i tried to clean it up but the forum editor will not coperate

    Thanks for the help

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Why don't you just use for (int i = 0; i < 10; i++) a++, etc? Maybe you are wasting your time trying to be clever ? It's not going to accomplish anything.

    Here, it would seem you are comparing a (pointer) value to the address the pointer is stored at. That does not make much sense.

    Code:
    a<&a+10
    When/if this condition fails depends where, in memory, a is in relation to to what it points to. Probably not a good thing to rely on, but since you did not post all the code (where a is declared, where the array is declared, etc) it's hard to say. In any case, this is not at all what you want to do.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by automagp68 View Post
    Code:
    //*a points to &myarray[0] inside of main. Then a is passed to the //function 
    //&a+10 should of failed when it reaches address 9 of the array
    
    for(;a<&a+10;a++)
    There's a couple of problems with this, if *a = &myarray[0] then &a is the same as **a.

    Secondly, since you increment a you can not compare with a. Let's say that a is initially 1 you then test if a is less than 1 + 10, second iteration a i 2 and you test if a is less than 2 + 10 and so on..

    You always push the condition ahead 10 steps, you need a copy of a and increment that.

    Code:
    for(p = a; p < a + 10; p++)

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by Subsonics View Post
    There's a couple of problems with this, if *a = &myarray[0] then &a is the same as **a.

    Secondly, since you increment a you can not compare with a. Let's say that a is initially 1 you then test if a is less than 1 + 10, second iteration a i 2 and you test if a is less than 2 + 10 and so on..

    You always push the condition ahead 10 steps, you need a copy of a and increment that.

    Code:
    for(p = a; p < a + 10; p++)

    Hi i tried your example exactly like that yesterday and it crashes.

    Just for kicks i just tried it again. still crashes.

    @mk27
    Thanks for the idea. I know how to use subscripts very well. As my original problem said, it has to be written this way.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by automagp68 View Post
    Hi i tried your example exactly like that yesterday and it crashes.

    Just for kicks i just tried it again. still crashes.
    Then you obviously have other issues, that approach works.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    no

    you are not understanding my issue correctly.

    a is a pointer to the begining of an array

    of you create a pointer inside of the function and point them to each toher then a never gets incremeted. so how does that work?

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by automagp68 View Post
    no

    you are not understanding my issue correctly.

    a is a pointer to the begining of an array

    of you create a pointer inside of the function and point them to each toher then a never gets incremeted. so how does that work?
    Sure I do, a is initialized to p. Example:

    Code:
    void foo(char *s, size_t size) {
        char *p;
        for(p = s; p < s + size; p++) {
            printf("%c", *p);
        }
    }
    But if you must increment a, here is an alternative.

    Code:
    void foo2(char *s, size_t size) {
        char *p = s + size;
    
        for(; s < p; s++) {
            printf("%c", *s);
        }
    }

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    sigh! ugh, thanks

    Somedays i feel like i wil never be a good programmer. i need a martini and its not even noon

    check this out. I changed it to this and now the top part works but not the smallest





    Code:
    for(ptr=a;ptr<a+10;ptr++){
    
    
    printf("This is the address of a right now [%p]\n",a);
    
    count++;//count just keeps track of how man times the loop goes off
    
    if (*ptr>*largest){
    
    *largest=*ptr;
    }
    }
    
    
    
    
    for(ptr2=a;ptr2<a+10;ptr++){
    
    if(*ptr2<*largest&&*second_largest<*ptr2){
    
    *second_largest=*ptr2;
    
    
    }
    
    
    }

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    I just found the mistake!

    Thank you!

    it works now!

    well i guess i should of posted this 10 hours ago instead of pulling my hair out
    Thanks man have a good day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on pointer math please =)
    By pnob32 in forum C Programming
    Replies: 5
    Last Post: 02-13-2011, 05:54 PM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Issue with pointer math
    By Kudose in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 04:25 AM
  4. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  5. pointer int math wrong
    By mart_man00 in forum C Programming
    Replies: 6
    Last Post: 04-26-2003, 09:17 PM