Thread: Array of Constants Initialization

  1. #16
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Yes, it did compile when I got all the syntax right, but it gave me warnings about the array initialization. I put the internal braces betrween the array elements and now I have no warnings or errors.

    There is another problem. I get a data abort message when the programs runs. I may have some illegal math operation. I have to look into that before I can ask for help.

    Thanks for your help.

  2. #17
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    I got rid of the data abort message, it was due to a while(1) being cleared. It is valid in the actual code, but not for my testing.

    My original example was simplified. My actual program statements are more complex, and I am having a difficult time with Braces in:

    Code:
    int x;
    int y;
    const int p[4][2] = {(1,2), (3,4), (5,6), (7,8)};
    if (x >= p[0][0]) (y=p[0][1]);
    else {if (x>=p[1][0]) (y=(p[1][1] * (1+(x/(p[1][0]-p[0][0])))));
             else {if (x>=p[2][0]) (y=(p[2][1] * (1+(x/(p[2][0]-p[1][0])))));
                        else {if (x>=p[3][0]) (y=(p[3][1] * (1+(x/(p[3][0]-p[2][0])))));
                                  else {if (x>=p[4][0]) (y=(p[4][1] * (1+(x/(p[4][0]-p[3][0])))));
                                          else (x = 154);}}}}
    Last edited by danlee58; 08-30-2008 at 07:42 AM.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by danlee58 View Post
    I got rid of the data abort message, it was due to a while(1) being cleared. It is valid in the actual code, but not for my testing.

    My original example was simplified. My actual program statements are more complex, and I am having a difficult time with Braces in:

    Code:
    int x;
    int y;
    const int p[4][2] = {(1,2), (3,4), (5,6), (7,8)};
    if (x >= p[0][0]) (y=p[0][1]);
    else {if (x>=p[1][0]) (y=(p[1][1] * (1+(x/(p[1][0]-p[0][0])))));
             else {if (x>=p[2][0]) (y=(p[2][1] * (1+(x/(p[2][0]-p[1][0])))));
                        else {if (x>=p[3][0]) (y=(p[3][1] * (1+(x/(p[3][0]-p[2][0])))));
                                  else {if (x>=p[4][0]) (y=(p[4][1] * (1+(x/(p[4][0]-p[3][0])))));
                                          else (x = 154)}}}};
    It's really easy.
    Code:
    int x;
    int y;
    const int p[4][2] = { {1, 2}, {3, 4}, {5, 6}, {7, 8} };
    if (x >= p[0][0])
        y=p[0][1];
    else
    {
        if (x >= p[1][0])
        {
            y = (
                p[1][1] *
                (
                    1 + ( x / (p[1][0] - p[0][0]) )
                ) );
        }
        else
        {
            if (x >= p[2][0])
            {
                y = (
                    p[2][1] *
                    (
                        1 + ( x / (p[2][0] - p[1][0]) )
                    ) );
            }
            else
            {
                if (x >= p[3][0])
                {
                    y = (
                        p[3][1] *
                        (
                            1 + ( x / (p[3][0] - p[2][0]) )
                        ) );
                }
                else
                {
                    if (x >= p[4][0])
                    {
                        y = (
                            p[4][1] *
                            (
                                1 + ( x / (p[4][0] - p[3][0]) )
                            ) );
                    }
                    else
                        x = 154;
                }
            }
        }
    }
    Every IF begins with { and ends with }.
    Every line must be terminated with ;. Use more whitespace to make it more visible.
    This may seem like a huge amount of whitespace, but it's necessary to make your convulted ifs readable.
    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.

  4. #19
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Elysia,

    Thanks for the help. That worked. Part of my difficulty is that I have a hard time telling the difference between a Brace and a Parenthesis.

    I had some other problems in the code, but I was able to fix them. I had to declare the const array a floating point, since the results of a calculation is fractional, and would return zero and have no effect if left as an integer.

    Now that section of code is working, I will go on to the next, but much of what I have to do is similar to this section, but with larger arrays and with more dimensions.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia
    Every line must be terminated with ;
    Wilst most of the time a line and a statement is the same thing, it is not a line that must end with a ; - a statment ends with ;

    There are several examples in the posted code where the same statement covers more than one line - if-statements are commonly written over two or more lines, where the first line is the if (condition), and the second line contains the "then do" part of the if-statement. Note that an "else" is a separate statement [but it must follow immediately after the if-"then-do" block.

    --
    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.

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