Thread: syntax understanding?

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    2

    syntax understanding?

    Code:
    int get_maxpos(int x [],int eff_size){
    int i,maxpos = 0;
    
    for(int i = 0; i <eff_size; i++ ) 
    maxpos = x[i] > x[maxpos] ? i : maxpos;
    
    return maxpos;
    }
    I would like to know what does this line do? how does it work?
    maxpos = x[i] > x[maxpos] ? i : maxpos;

    Thanks, I'm new to C, never seen it before.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's the ternary operator.

    It's the equivalent of

    Code:
    if (x[i] > x[maxpos]) {
        maxpos = i;
    } else {
        maxpos = maxpos; /* sort of useless */
    }

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    2
    Thanks, I originally thought it was storing the value at position i into variable maxpos but in reality it's only storing the index.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Syntax Versus Pointer Syntax
    By LyTning94 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2011, 10:56 AM
  2. Beginner question about understanding general Syntax
    By lucidrave in forum C++ Programming
    Replies: 15
    Last Post: 08-13-2009, 03:57 PM
  3. Please help on understanding special C++ syntax used
    By Nout in forum C++ Programming
    Replies: 10
    Last Post: 07-12-2008, 08:34 PM
  4. Help understanding C syntax
    By hpteenagewizkid in forum C Programming
    Replies: 3
    Last Post: 12-04-2006, 10:04 AM
  5. Trouble understanding malloc() syntax
    By Sereby in forum C Programming
    Replies: 21
    Last Post: 07-28-2004, 10:19 AM

Tags for this Thread