Thread: ++ operator on arrays

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55

    ++ operator on arrays

    Hey Everyone,

    Just began programming C and am working through the 'white bible'. Have programmed in LISP and Java prior to this. I have a question regarding the incrementer operator and its usage with arrays. Ex:

    Code:
    value = ++myarray[maxvalue - 1];
    What exactly is being incremented here?

    I haven't seen an increment operator used in this way before!

    Cheers

    BIOS

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are incrementing the value at myarray[maxvalue -1] and then assigning it to the variable value.

    For example:
    Code:
    int myarray[4] = {1,2,3,4}
    
    x = ++myarray[2];
    myarray[2] is 3 (remember C arrays index from 0, not 1)
    3 + 1 equals 4 myarray[2] ends up being 4
    assigning to x makes it 4

    (I sometimes find it helpful to substitute real numbers for the variables as a way to understand things)

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55
    Hey thanks for the reply!

    That makes perfect sense. Thanks for the explanation. I think it was the fact that you can place the increment operator as a prefix in C that threw me. I.e. in Java you would write:

    Code:
    myarray[x]++;

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BIOS View Post
    Hey thanks for the reply!

    That makes perfect sense. Thanks for the explanation. I think it was the fact that you can place the increment operator as a prefix in C that threw me. I.e. in Java you would write:

    Code:
    myarray[x]++;
    C has both prefix and postfix in this case... The difference being this...
    Code:
    int myarray[4] = {1,2,3,4}
    
    x = ++myarray[2];   // x = 4, myarray[2] = 4
    
    x = myarray[2]++;  //  x =3, myarray[2] = 4
    The difference is whether myarray[2] is incremented before the assignment or after...

    Also let me toss in just a small note of caution here... Beware of comparing languages! ... Java does a lot of babysitting that C does not, it also has features C does not. C gives you control over code that is hidden in Java, it can also do some stuff Java cannot... Whenever possible, you should treat your C learning experience as something entirely new and different...
    Last edited by CommonTater; 09-04-2011 at 09:11 AM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55
    Quote Originally Posted by CommonTater View Post
    C has both prefix and postfix in this case... The difference being this...
    Code:
    int myarray[4] = {1,2,3,4}
    
    x = ++myarray[2];   // x = 4, myarray[2] = 4
    
    x = myarray[2]++;  //  x =3, myarray[2] = 4
    The difference is whether myarray[2] is incremented before the assignment or after...
    Interesting! I had been wondering why C allowed this.

    Quote Originally Posted by CommonTater View Post
    Also let me toss in just a small note of caution here... Beware of comparing languages! ... Java does a lot of babysitting that C does not, it also has features C does not. C gives you control over code that is hidden in Java, it can also do some stuff Java cannot... Whenever possible, you should treat your C learning experience as something entirely new and different...
    This is great advice. Actually the reason I'm learning C is for this extra control and the greater awareness it brings with regard to managing memory etc. I must remember not to fall into that 'comparison' trap! Thanks alot.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BIOS View Post
    This is great advice. Actually the reason I'm learning C is for this extra control and the greater awareness it brings with regard to managing memory etc. I must remember not to fall into that 'comparison' trap! Thanks alot.
    One thing I can promise you... C will make you a very careful programmer. Almost everything is on you to get it right.

    (But I wouldn't have it any other way )

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by CommonTater View Post
    One thing I can promise you... C will make you a very careful programmer. Almost everything is on you to get it right.

    (But I wouldn't have it any other way )
    Considering my troubles understand C/C++ pointer concepts (one could say I am just stupid ) I have come to the conclusion that those languages were made by masochists for masochists :P
    You ended that sentence with a preposition...Bastard!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    Considering my troubles understand C/C++ pointer concepts (one could say I am just stupid ) I have come to the conclusion that those languages were made by masochists for masochists :P
    Ok... start a thread for pointers, show us some code... we'll see what we can do.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by CommonTater View Post
    Ok... start a thread for pointers, show us some code... we'll see what we can do.
    I assume you don't visit the C++ forum much, I have posted several pointer questions but most related to polymorphism...getting clearer though.
    Btw, how goes your learning C++?
    You ended that sentence with a preposition...Bastard!

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    I assume you don't visit the C++ forum much, I have posted several pointer questions but most related to polymorphism...getting clearer though.
    Btw, how goes your learning C++?
    I read the C++ section but rarely post there. Since I gave up on C++ entirely --it just doesn't make any sense to me-- it doesn't seem right that I should post there... well, except maybe to ask questions.

  11. #11
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by CommonTater View Post
    I read the C++ section but rarely post there. Since I gave up on C++ entirely --it just doesn't make any sense to me-- it doesn't seem right that I should post there... well, except maybe to ask questions.
    And here I thought I was the only one. The only reason I am learning it is for the sake of learning it..some say it will make sense when I start working. I hope they're bloody right. Java is even worse.
    Hopefully, you will go back to it one day man
    You ended that sentence with a preposition...Bastard!

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    AFAIK Java also have both post and pre increment operators, which also change the operator precedence related to the assignment operator.

  13. #13
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Yes, it does..But I was talking about the OOP style in java..Everything is OOP and it takes a while to find what I need. I hate it..
    If you have a string you can't modify a character in the string for example. I have to look for some other class. I hate that language..
    You ended that sentence with a preposition...Bastard!

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Eman View Post
    Yes, it does..But I was talking about the OOP style in java..
    I was referring to post #3, I should have made a quote to that post, sorry for the confusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ternary Operator to choose which operator to use
    By cncool in forum C Programming
    Replies: 7
    Last Post: 06-27-2011, 01:35 PM
  2. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  3. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  4. Replies: 1
    Last Post: 07-07-2008, 03:38 AM
  5. Replies: 6
    Last Post: 11-08-2005, 03:05 PM