Like Tree2Likes
  • 1 Post By christop
  • 1 Post By Matticus

Some help needed with structures and pointer

This is a discussion on Some help needed with structures and pointer within the C Programming forums, part of the General Programming Boards category; Hello Guys and Gals, I am a new to the forum and haven't used C beyond basic programming before, hence ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    UK
    Posts
    3

    Unhappy Some help needed with structures and pointer

    Hello Guys and Gals,

    I am a new to the forum and haven't used C beyond basic programming before, hence I am still learning and need some help with a particular problem. I am trying to create a data structure that will be partially dimensioned at run time, hence using pointers instead of arrays or malloc functions.

    The data structure needs two dimensions, i.e. rows and columns.
    The number of columns are defined at run time but the number of rows are not, and as the program runs the number of rows will grow and be added to with sample values.
    I am compiling C using the Microchip C30 and MPLAB IDE for PIC microcontrollers. I would be very grateful if you could advise on the method for achieving this, since I run into various compile errors such as pointer not declared, error before = etc.

    The sort of code I am using is below:

    Code:
    //structure declarations
    
    
    //rows of this struct that can grow
    typedef struct rowStruct{             
    int index;
    int value;
    }row,*rowPtr;
    
    //columns 
    typedef int column, *columnPtr;               
    
    //combines row and column
    struct{                                               
    row *rowPtr;
    column *columnPtr;
    } dataSet, *dataSetPtr
    
    
    void structInit()
    {
    
    int i=1;
    //initialise the first row values for each column
       while (dataSetPtr->channel<=16)  //16 is actually an ext variable
       {
              dataSetPtr->channel=i;
             rowPtr=&(dataSetPtr->row);//get syntax error before '=' here.
             rowPtr->Index=0;
             rowPtr->value=0;
       
            i++;
         }
    }
    //end
    As said I can't get this to compile let alone test the two dimensionality that I need, and there may be much better ways of doing this, any help would be much appreciated!!
    Last edited by Engineria; 08-13-2012 at 06:36 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Look into doing this with pointers/structures

    (*dataSetPtr).channel

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    And where might elementPtr be declared?

  4. #4
    Registered User
    Join Date
    Aug 2012
    Location
    UK
    Posts
    3
    Sorry, I was writing this from the source code which has different names, I have corrected the structure fields and names accordingly.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    And what is rowPtr exactly?

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What is the data that you want to put into each element of your 2D array?
    I see this:
    Code:
    typedef struct rowStruct{
        int index;
        int value;
    } row, *rowPtr;
    Is that your data? index and value? Or do you have "index" there as part of your attempt to implement the array?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Aug 2012
    Location
    UK
    Posts
    3
    *rowPtr is just a pointer to the row structure containing the index and value fields. The idea is to append with time as new sample are recorded from an ADC.

    Therefore I would want to use the rowPtr++ to increment the pointer to the next fields of index and value and write these values. Index will effectively be the reference to the array element.
    This will be in a different function however.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I find the code you've posted impossible to understand. It's possible that you have a completely misunderstood how to accomplish what you want to do. My question: "is index a data value or just part of your scheme to implement the array"? If it's part of your implementation plan then there's probably a vastly simpler way to do it (if you could even do it the way you're imagining).

    Anyway, assuming that both index and value are actual data, this kind of thing would usually be done something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUMBER_OF_COLUMNS        10
    #define INITIAL_NUMBER_OF_ROWS  100
    
    struct Element {
        int index;
        int value;
    };
    
    // ROWTYPE is a ptr to an array of NUMBER_OF_COLUMNS Elements.
    typedef struct Element (* ROWTYPE )[NUMBER_OF_COLUMNS];
    
    int main(void) {
        int size = 0;
        int used = 0;
        int column;
        ROWTYPE a = NULL;
    
        while (used < 1000) {
            if (used >= size) {
                size += INITIAL_NUMBER_OF_ROWS;
                a = realloc(a, size * sizeof *a);
                if (!a) {
                    fprintf(stderr, "realloc failed\n");
                    exit(1);
                }
            }
            for (column = 0; column < NUMBER_OF_COLUMNS; column++) {
                a[used][column].index = 1;
                a[used][column].value = 2;
            }
            used++;
        }
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    (*dataSetPtr).channel is the data at dataSetPtr->channel

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by Click_here View Post
    (*dataSetPtr).channel is the data at dataSetPtr->channel
    Those two statements actually are equivalent, just as *(ptr + 1) and ptr[1] are equivalent.
    Matticus likes this.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    2,234
    "Engineria" ... the land of the Engineers, if I'm not mistaken. 'tis a nerdy place, if I recall. Good times all around.
    Click_here likes this.

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Quote Originally Posted by Engineria View Post
    *rowPtr is just a pointer to the row structure containing the index and value fields. The idea is to append with time as new sample are recorded from an ADC.

    Therefore I would want to use the rowPtr++ to increment the pointer to the next fields of index and value and write these values. Index will effectively be the reference to the array element.
    This will be in a different function however.
    My point, actually, was to get you to see that there are two rowPtrs in your code; one is a typedef and one is a member variable of a struct. Using the same name for both is bound to be confusing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed with complex array of pointer to structures
    By zahid990170 in forum C Programming
    Replies: 3
    Last Post: 06-14-2011, 01:46 PM
  2. [Solution Needed] Structures questions
    By yikchong in forum C Programming
    Replies: 3
    Last Post: 10-31-2009, 11:37 AM
  3. Help needed with structures
    By esmeco in forum C Programming
    Replies: 7
    Last Post: 03-08-2008, 12:29 PM
  4. MinPriority Queue and structures, assistance needed :)
    By trancekid in forum C Programming
    Replies: 6
    Last Post: 10-30-2007, 03:23 AM
  5. Replies: 3
    Last Post: 11-14-2005, 07:03 AM

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