How to store bulk data and/or point to array?

This is a discussion on How to store bulk data and/or point to array? within the C Programming forums, part of the General Programming Boards category; Hi, My first post - I look forward to learning a lot here. I am getting back into C programming ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    How to store bulk data and/or point to array?

    Hi,

    My first post - I look forward to learning a lot here.

    I am getting back into C programming after about 20+ years away from it. This is for an embedded project running on a microcontroller.

    To explain what I am doing, I have a 3x3 array of lights, and I want to take a key press from the user and each time the key is pressed, I cycle to the next in a series of patterns which get displayed on the array of lights.

    The patterns are made up of a series of frames. Each frame is 9 elements (basically 1 or 0 depending on if the light will be on), and there may be anywhere from a single frame to, say, 20 frames in one pattern. I am hoping to have maybe 100 patterns in total for the final project.

    Right now, I am just storing each pattern in an array and the first digit of the array denotes how many frames are contained in that array.

    Code:
    char P1[] = {3, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    char P2[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    char P3[] = {2, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
    char P4[] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
    I have a function which runs in the background and constantly updates the display. So when the user presses a key, I want to switch from P1 to P2. I am trying to avoid a giant switch statement in my function that updates the display. So what I would like to do is have a global variable that denotes the memory location which holds the pattern, and then the function would just start reading it and display the data.


    My questions are, does storing my data in arrays make sense? I am very much a noob with C, so I am not sure if there is some "bulk data" format that I can just put a bunch of data in my program more efficiently than storing it in arrays?

    And what syntax would I use to denote the memory address of the first element in an array? I realize this is what pointers are, but I am getting confused reading tutorials when they say that arrays are essentially pointers right off the bat = but I can't pass the address of, for example, the P1[] array just by writing P1, or maybe I can? I get compile errors when I try.


    Any information is greatly appreciated!
    Last edited by GuyIncognito; 05-21-2011 at 12:36 AM. Reason: clarify

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char patterns[][9] = {
        {0, 0, 0, 1, 1, 1, 0, 0, 0},
        {1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 1, 0, 1, 0, 1, 0, 1},
        {0, 1, 0, 1, 0, 1, 0, 1, 0},
        ...
    };
    ...
    void showpattern( char pattern[9] )
    {
        ...
    }
    ...
    
    for( x = 0; x < sizeof patterns / sizeof patterns[0]; x++ )
        showpattern( patterns[ x ] );
    That looks about right.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-14-2011, 12:15 PM
  2. Pick data from file store in array
    By swgh in forum C Programming
    Replies: 1
    Last Post: 07-10-2009, 10:57 AM
  3. How to find the middle element of a bulk array?
    By void_mehboob in forum C Programming
    Replies: 4
    Last Post: 04-20-2009, 12:37 AM
  4. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 09:50 AM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 01:05 PM

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