Array of Constants Initialization

This is a discussion on Array of Constants Initialization within the C Programming forums, part of the General Programming Boards category; I am trying to initialize an array of Constants for an embedded computer. It's a lookup table for a non-linear ...

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

    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
    Posts
    23,033
    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
    22,312
    why the extra braces
    To improve clarity.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,312
    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 09:05 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    22,312
    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 11:59 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    35
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. two dimensional array initialization
    By George2 in forum C Programming
    Replies: 3
    Last Post: 07-27-2007, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21