Thread: referencing to array-values with pointers

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    36

    referencing to array-values with pointers

    Hi!

    Im not sure why the following output of the following code is:
    2 15 6 8 10

    Code being:

    Code:
    #include <stdio.h>
    
    
    
    
    
    void change(int *b, int n) {
        int i;
        for(i=0; i<n; i++)
            *(b+1) = *(b+i)+5;
    }
    
    
    int main() {
        int i, a[] = {2, 4, 6, 8, 10};
        change(a, 5);
        for(i=0; i<=4; i++)
            printf("%d  ", a[i]);
    
    
        return 0;
    }
    I mean, from my understanding an array is basically a pointer of the 1st element of the array, i.e. an array is the address of the first element of an array.

    So here *b should be basically a[0] and hence the value of 2 - is this right so far?

    if so, then *(b + 1) should be just be basically a[1] and hence 4?
    But what is happening then?
    it is set to *(b + i) + 5, which should be within the first iteration
    *(b + 0) + 5, and hence a[0] + 5 and finally this should be 7?

    Im not sure how the calc works to be honest and would be grateful for help to understand it.

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    your assigning the value of a[0] + 5 to a{0+1] then your assining 6+5 to a[1] and so on which is why the 2 doesnt change or the 6, 8 and 10 and why a[1] is 15 ( 10 + 5)
    Last edited by cooper1200; 05-11-2019 at 05:02 AM.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    by adding 1 or i to the pointer is another way of saying b[ 1 ] or b[ i ]

  4. #4
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Hi!
    Thanks for the reply - Im afraid I still do not fully get it.

    Quote Originally Posted by cooper1200 View Post
    your assigning the value of a[0] + 5 to a{0+1]
    But this should be 7 then? Since a[0] is 2?
    then your assining 6+5 to a[1]
    when and how?
    and so on which is why the 2 doesnt change or the 6, 8 and 10 and why a[1] is 15 ( 10 + 5)
    Need to think more about it, because so far I really apparently do not get it.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're only ever changing *(b + 1). You keep assigning to it, so only the last assignment has a net effect, and in that assignment i == 4, i.e., you compute *(b + 4) + 5, which is 15.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by laserlight View Post
    You're only ever changing *(b + 1). You keep assigning to it, so only the last assignment has a net effect, and in that assignment i == 4, i.e., you compute *(b + 4) + 5, which is 15.

    so basically 10 + 5, since *(b + 4) should be b[4], which is 10.
    If this is right, I think I get it now with your help - thanks much^^

    ...............

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Placebo View Post
    so basically 10 + 5, since *(b + 4) should be b[4], which is 10.
    If this is right, I think I get it now with your help - thanks much^^
    laserlight's answer is the right one... and you understood perfectly well the equivalence of *(b+n) and b[n].

  8. #8
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by flp1969 View Post
    laserlight's answer is the right one... and you understood perfectly well the equivalence of *(b+n) and b[n].
    All right, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i assign pointers to specific array values?
    By brianjoo in forum C Programming
    Replies: 2
    Last Post: 11-29-2017, 04:15 AM
  2. output for array of pointers is not giving correct values
    By nkrao123@gmail. in forum C Programming
    Replies: 8
    Last Post: 09-03-2011, 07:40 AM
  3. changing array values using pointers
    By dford425 in forum C Programming
    Replies: 8
    Last Post: 01-15-2011, 10:45 PM
  4. pointers and referencing
    By nata in forum C Programming
    Replies: 5
    Last Post: 10-22-2002, 06:01 AM
  5. Replies: 2
    Last Post: 03-07-2002, 10:14 AM

Tags for this Thread