Thread: one question about array

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    one question about array

    Hi there,

    if there is a code like this :

    Code:
    int c, ndigit[10];
    
    if (c >= '0' && c <= '9')
    	  ++ndigit[c-'0'];
    ++ndigit[c-'0'] is that the same with :

    ndigit[10] = ndigit[10] + ndigit[c-'0']?

    sorry if this is a stupid question, I'm just a newbie

    if full program is needed (a short program this is) to answer my question, I will put it online. Just tell me so.

    thanks!


  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Code:
    ++ndigit[c-'0']
    Is the same as

    Code:
    ndigit[c-'0'] = ndigit[c-'0'] + 1

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vsovereign View Post
    ++ndigit[c-'0'] is that the same with :

    ndigit[10] = ndigit[10] + ndigit[c-'0']?
    No. The ASCII value of '0' is actually 48:
    ASCII Table / Extended ASCII Codes
    '9' is 57. If you don't understand what the ascii table is about, ask.

    So that code could be written:
    Code:
    if (c >= 48 && c <= 57)
    	  ++ndigit[c-48];
    Thus c-48 will be between 0 and 9. ++ means to increment that value in the array by one. Here it is in front of the variable, meaning "pre-increment" (increment before the value is used in the statement context). Since there is no statement context, it means the same as ndigit[c-48]++.
    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

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by MK27 View Post
    So that code could be written:
    Code:
    if (c >= 48 && c <= 57)
    	  ++ndigit[c-48];
    "could" but very much should not! It's purely an exercize in obfuscation.

    Do not do that!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    Quote Originally Posted by MK27 View Post
    No. The ASCII value of '0' is actually 48:
    ASCII Table / Extended ASCII Codes
    '9' is 57. If you don't understand what the ascii table is about, ask.

    So that code could be written:
    Code:
    if (c >= 48 && c <= 57)
    	  ++ndigit[c-48];
    Thus c-48 will be between 0 and 9. ++ means to increment that value in the array by one. Here it is in front of the variable, meaning "pre-increment" (increment before the value is used in the statement context). Since there is no statement context, it means the same as ndigit[c-48]++.
    so that means that if c = 0 -> c = 48 in ASCII

    so
    Code:
    ndigit[48-48] = ndigit[48-48] + 1
         ndigit[0] = ndigit[0] + 1
    if we initialize ndigit[10] :

    Code:
    for (i = 0; i < 10; i = i + 1)
    	 ndigit[i] = 0;
    that means ndigit[0] = 0

    so
    Code:
    ndigit[0] = ndigit[0] + 1
         ndigit[0] = 1
    is that correct?

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    if we initialize ndigit[10] :
    Careful with your notation. ndigit[10] does not exist. ndigit is an array of 10 elements with indices 0-9 inclusive. Saying ndigit[10] is saying "the 11th entry in ndigit" which does not exist.

    What you mean to say was: "if we initialize all of the entries of ndigit to 0". The [10] is not part of the variable name, it just tells you how many elements are in it.

    c = 0 does not mean c=48 in ASCII. c='0' does. Note the quotes, which makes it the zero character, rather than the number 0. The character '0' has value 48 "in ASCII."

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    Quote Originally Posted by KBriggs View Post
    Careful with your notation. ndigit[10] does not exist. ndigit is an array of 10 elements with indices 0-9 inclusive. Saying ndigit[10] is saying "the 11th entry in ndigit" which does not exist.

    What you mean to say was: "if we initialize all of the entries of ndigit to 0". The [10] is not part of the variable name, it just tells you how many elements are in it.

    c = 0 does not mean c=48 in ASCII. c='0' does. Note the quotes, which makes it the zero character, rather than the number 0. The character '0' has value 48 "in ASCII."
    Yeah, but if we input c = 0, won't it changed to c = 48 because it's getchar() ?

    so it will be
    Code:
    ndigit[c-'0'] = ndigit[c-'0'] +1 --> I input c = 0
    
    ndigit[c-'0'] = ndigit[48-48] +1 
    ndigit[c-'0'] = ndigit[0]+1
    I also don't understand this : does ndigit[0] + 1 = ndigit[1] ?
    if yes, how come if I input 0, then it will be enterd into ndigit[1] instead of ndigit[0] ?

    help will be appreciated.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vsovereign View Post
    I also don't understand this : does ndigit[0] + 1 = ndigit[1] ?
    if yes, how come if I input 0, then it will be enterd into ndigit[1] instead of ndigit[0] ?
    No, you are only looking at part of the line. The whole thing translates something like:
    Code:
    int x;
    ...
    x = x + 1;
    The first part:
    Code:
    ndigit[ 0 ] =
    Is the same as above when I did:
    Code:
    x =
    Then the next part is the remainder:
    Code:
    ndigit[ 0 ] + 1;
    Is the same as:
    Code:
    x + 1;
    Again, taken all together:
    Code:
    int x;
    ...
    x = x + 1;
    Is the same as:
    Code:
    int ndigit[ SOMESIZE ];
    ...
    ndigit[ SOMEPLACE ] = ndigit[ SOMEPLACE ] + 1;

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM