Thread: Array of Constants Initialization

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47

    Array of Constants Initialization

    I am trying to initialize an array of Constants for an embedded computer. It's a lookup table for a non-linear function. I can't seem to get it to compile correctly. Here is the simplified Syntax:

    Code:
    const int junk [3][2] = {10, 2. 5, 4, 1, 6};
    This is how it's used:

    Code:
    int trsh;
    int trsh2;
    
    trsh = 3;  //This value is input from an A/D conveter.
    
    if (trsh == junk[0][0])
    (trsh2 = junk[0][1];
    else
    if (trsh >= junk[1][0])
    (trsh2 = junk[1][1];
    else 
    trsh2 = junk[2][1];
    I actually extrapolate between the elements to determine the value of trsh2, but I didn't do it here for simplification.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your posted code will not compile due to several syntax errors (parenthesis where I beleive you mean braces, a dot instead of comma in the intializer list for the array).

    Also, a 2D array would normally have two levels of braces, e.g.
    Code:
    const int array[3][2] = { { 0, 1 } , { 2, 3 }, {3, 4} };
    At the very least, gcc will give you a warning if you don't have the extra braces.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You are also not indenting your code and your if/else statements are missing braces.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    if (trsh == junk[0][0])
        trsh2 = junk[0][1];
    else
    {
        if (trsh >= junk[1][0])
            trsh2 = junk[1][1];
        else 
            trsh2 = junk[2][1];
    }
    This is more like how it should look like.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    why the extra braces for the this:
    Code:
    else
    {
        if (trsh >= junk[1][0])
            trsh2 = junk[1][1];
        else 
            trsh2 = junk[2][1];
    }
    you don't need that!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    why the extra braces
    To improve clarity.
    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

  7. #7
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    what clarity???

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The clarity of the code, of course, in addition to what the indentation provides. Put it another way: why the indentation? You don't need that!
    Code:
    if (trsh == junk[0][0]) trsh2 = junk[0][1];
    else if (trsh >= junk[1][0]) trsh2 = junk[1][1]; else trsh2 = junk[2][1];
    EDIT:
    On second thought, perhaps what you mean is that we can indent like this:
    Code:
    if (trsh == junk[0][0])
        trsh2 = junk[0][1];
    else if (trsh >= junk[1][0])
        trsh2 = junk[1][1];
    else
        trsh2 = junk[2][1];
    Admittedly, in answering your question, this did not cross my mind since your focus was on the braces, but we certainly could write:
    Code:
    if (trsh == junk[0][0])
    {
        trsh2 = junk[0][1];
    }
    else if (trsh >= junk[1][0])
    {
        trsh2 = junk[1][1];
    }
    else
    {
        trsh2 = junk[2][1];
    }
    Last edited by laserlight; 08-29-2008 at 08:05 AM.
    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

  9. #9
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    EXACTLY! And you don't need the braces there either (not even for clarity!)

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm guessing the braces are there because the actual code is bigger than one line (as hinted at by OP).

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kcpilot
    And you don't need the braces there either (not even for clarity!)
    I agree, but of course it may be advised by a style guideline motivated by clarity and future proofing against misleading additions of statements, or simply for consistency.
    Last edited by laserlight; 08-29-2008 at 10:59 AM.
    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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kcpilot View Post
    why the extra braces for the this:
    Code:
    else
    {
        if (trsh >= junk[1][0])
            trsh2 = junk[1][1];
        else 
            trsh2 = junk[2][1];
    }
    you don't need that!
    Because it's more than one line.
    When it is, I usually add them for more clarity.
    It's purely a style question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I always add braces, It helps to avoid erros in the code like
    Code:
    if(x)
       if(y)
          statement1;
    else
       statement2;
    as well as prevents adding errors when the additional statements are added, or when the statement is the macro resolved to some construction like
    if(p) puts(p);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Thanks for the replys. The dot inplace of the comma was a typo in my example. My ANSI C book does not show two levels of braces for multi dimensional arrays. I will try that and let you know.

  15. #15
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    You don't need all those extra braces for the initializer though. They are entirely optional, although gcc does complain about it from what I remember. Your compiler shouldn't be bombing for that, especially since you don't have too many.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array initialization
    By George2 in forum C Programming
    Replies: 3
    Last Post: 07-27-2007, 06:53 AM
  3. Difference between array initialization
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 11:51 PM
  4. enum constants as array indices
    By hzmonte in forum C Programming
    Replies: 2
    Last Post: 01-23-2006, 08:23 PM
  5. Basic Array Initialization in C
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 06:41 PM