CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

passing 2 dimensional array in a function
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kathir
Guest







passing 2 dimensional array in a function
PostPosted: Sat May 24, 2008 7:56 pm     Reply with quote

I am trying to port HTTP.c of Microchip stack to CCS TCP/IP. I came to
final stage. But I am unable to pass 2-dimensional array in a function.
It says two many subscripts as error. If I find a solution for this I can complete my task. Here is the function:
Code:

static HTTP_COMMAND HTTPParse(BYTE *string, [b]BYTE* *arg, BYTE [/b]* argc, BYTE* type)
{
    BYTE i;
    BYTE smParse;
    HTTP_COMMAND cmd;
    BYTE *ext;
    BYTE c;
    char* fileType;
   
    enum
    {
        SM_PARSE_IDLE,
        SM_PARSE_ARG,
        SM_PARSE_ARG_FORMAT
    };

    smParse = SM_PARSE_IDLE;
    ext = NULL;
    i = 0;

    // Only "GET" is supported for time being.
    if ( !memcmp(string, (void*) HTTP_GET_STRING, HTTP_GET_STRING_LEN) )
    {
        string += (HTTP_GET_STRING_LEN + 1);
        cmd = HTTP_GET;
    }
    else
    {
        return HTTP_NOT_SUPPORTED;
    }

    // Skip white spaces.
    while( *string == ' ' )
        string++;

    c = *string;

    while ( c != ' ' &&  c != '\0' && c != '\r' && c != '\n' )

    {
        // Do not accept any more arguments than we haved designed to.
        if ( i >= *argc )
            break;

        switch(smParse)
        {
        case SM_PARSE_IDLE:
            arg[i] = string;
            c = *string;
            if ( c == '/' || c == '\\' )
                smParse = SM_PARSE_ARG;
            break;

        case SM_PARSE_ARG:
            arg[i++] = string;
            smParse = SM_PARSE_ARG_FORMAT;
            /*
             * Do not break.
             * Parameter may be empty.
             */

        case SM_PARSE_ARG_FORMAT:
            c = *string;
            if ( c == '?' || c == '&' )
            {
                *string = '\0';
                smParse = SM_PARSE_ARG;
            }
            else
            {
                // Recover space characters.
                if ( c == '+' )
                    *string = ' ';

                // Remember where file extension starts.
                else if ( c == '.' && i == 1u )
                {
                    ext = ++string;
                }

                else if ( c == '=' )
                {
                    *string = '\0';
                    smParse = SM_PARSE_ARG;
                }

                // Only interested in file name - not a path.
                else if ( c == '/' || c == '\\' )
                    arg[i-1] = string+1;

            }
            break;
        }
        string++;
        c = *string;
    }
    *string = '\0';

    *type = HTTP_UNKNOWN;
    if ( ext != NULL )
    {
        ext = (BYTE*)strupr((char*)ext);

        fileType = httpFiles[0].fileExt;
        for ( c = 0; c < TOTAL_FILE_TYPES; c++ )
        {
            if ( !memcmp((void*)ext, (void*)fileType, FILE_EXT_LEN) )
            {
                *type = c;
                break;
            }
            fileType += sizeof(FILE_TYPES);

        }
    }

    if ( i == 0u )
    {
        memcpy(arg[0],(void*)HTTP_DEFAULT_FILE_STRING,HTTP_DEFAULT_FILE_STRING_LEN);
        arg[HTTP_DEFAULT_FILE_STRING_LEN] = '\0';
        *type = HTTP_HTML;
        i++;
    }
    *argc = i;

    return cmd;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 24, 2008 9:47 pm     Reply with quote

Use Matro's technique to declare a pointer to a pointer. The compiler
will then compile it without errors. See his post in this thread.
http://www.ccsinfo.com/forum/viewtopic.php?t=34794
ysaacb



Joined: 12 Jun 2006
Posts: 19

View user's profile Send private message

PostPosted: Wed Dec 31, 2008 9:25 am     Reply with quote

I'm also trying to pass a 2 dim array to a function. I try to use pointer to pointer using the Matro technique.
But the only way a find it can work is passing the array as a one dimention array (single pointer), the row and column size and doing the calculation that is confusing.

Have anyone pass to a function a 2 dim array as a pointer to pointer?, How can I access the array inside the function ?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Dec 31, 2008 10:32 am     Reply with quote

I must admit, that I don't understand the problem. I see two possibilities

1. If the array type is known through the project, than the pointer can be simply de-referenced using a global typedef.

2. If the array structure is supposed to change, more exactly, the innermost dimension, than an additional dimension size parameter must be passed to the function and used to access the indivdual array elements.
ysaacb



Joined: 12 Jun 2006
Posts: 19

View user's profile Send private message

PostPosted: Wed Dec 31, 2008 1:14 pm     Reply with quote

let say I want to do the following, just an example, nothing usefull:

Code:
void myfunc(char arr[row][col])
{
      for (i = 0, i<row; i++)
            for (i = 0, i<col; i++)
                 arr[i][j]=0;
}


How can I do it with pointers?
ysaacb



Joined: 12 Jun 2006
Posts: 19

View user's profile Send private message

PostPosted: Thu Jan 01, 2009 4:08 am     Reply with quote

This is my code:

Code:
void rotate_buffer(MESSAGE_BUFFER[7][40])
   {
         for (row=0;row<7;row++)
            {
            FIRST_BYTE=MESSAGE_BUFFER[row][0];
            FIRST_BIT_OUT=bit_test(FIRST_BYTE,7);
            for (j=0;j<TOTAL_BYTES;j++)
               
               {
               if(j==TOTAL_BYTES-1) bit_out=FIRST_BIT_OUT;
               else bit_out=bit_test(MESSAGE_BUFFER[row][j+1],7);
               shift_left(&MESSAGE_BUFFER[row][j],1,bit_out);
               }
            }
   }

This function shift all the array one bit left.
I need to pass the array MESSAGE_BUFFER to the function.
Anyone know how?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 01, 2009 4:01 pm     Reply with quote

The test program shown below displays the following output:
Quote:
1 2 3
A B C

Code:
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define NUM_ROWS 2
#define NUM_COLS 3


void myfunc(char array[NUM_ROWS][NUM_COLS])
{
int8 row, col;
 
for(row=0; row < NUM_ROWS; row++)
   {
    for(col = 0; col < NUM_COLS; col++)
        printf("%c ", array[row][col]);

    printf("\n\r");
   }

}

//======================================
void main(void)
{
int8 data[NUM_ROWS][NUM_COLS] = { {'1', '2', '3'},
                                  {'A', 'B', 'C'} };
myfunc(data);

while(1);
}
                 
Guest








PostPosted: Wed Jan 07, 2009 12:12 pm     Reply with quote

The same code PCM post, but declaring another array doesn't work, why?
Code:
#include<18F2620.h>
#device icd=true
#use delay(clock=19660800)
#fuses HS,NOWDT
#use rs232(baud=9600,parity=N,xmit=PIN_c6)
#DEFINE LED_MARTIX_8X8 4
#DEFINE ROW_HEIGTH 8
#DEFINE MAX_MESSAGE_SIZE 40


char message2[ROW_HEIGTH][MAX_MESSAGE_SIZE];
char MESSAGE_BUFFER_TEMP[ROW_HEIGTH][LED_MARTIX_8X8];





void init_buffer (char array[ROW_HEIGTH][LED_MARTIX_8X8])
{
char row,col;
for (row=0;row<ROW_HEIGTH;row++)
   for (col=0;col<LED_MARTIX_8X8;col++)
      array[row][col]=0xFF;
}

void main(void)
{


   init_buffer(MESSAGE_BUFFER_TEMP);


}
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Jan 07, 2009 3:16 pm     Reply with quote

I can't see from your post what doesn't work.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 07, 2009 3:18 pm     Reply with quote

It works if the first array declaration (which is not used) is commented out.
Example:
Quote:
// char message2[ROW_HEIGTH][MAX_MESSAGE_SIZE];
char MESSAGE_BUFFER_TEMP[ROW_HEIGTH][LED_MARTIX_8X8];

So there must be some kind of bug, which will need to be investigated.

This was tested with PCH vs. 4.079 and vs. 4.084.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jan 08, 2009 12:22 am     Reply with quote

Still unclear.
Quote:
I can't see from your post what doesn't work.

Would be nice to know, without compiling and testing the code by myself.
Guest








PostPosted: Thu Jan 08, 2009 7:15 am     Reply with quote

After hours trying, I found that if declare the array like this, it works:

Code:
char message2[ROW_HEIGTH][ROW_HEIGTH];


but in this way i'm missusing the RAM. I understand that it is a bug but I can't find any work around.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jan 08, 2009 1:50 pm     Reply with quote

What did you try? The array message2 isn't used at all in your posted code. I also still wonder, what not working means? An error reported by the compiler? Unexpected behaviour of the application? If so, what happens exactly?

Best regards,

Frank
diogoc



Joined: 12 Feb 2008
Posts: 19

View user's profile Send private message Visit poster's website MSN Messenger

PostPosted: Wed Jul 15, 2009 7:38 am     Reply with quote

Hi,

I have the same problem. There are another way to pass a 2d array to a function?

thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 15, 2009 11:32 am     Reply with quote

Quote:
I have the same problem. There are another way to pass a 2d array to a function?

1. Post a small but complete test program that shows the problem.
See the test program that I posted earlier in this thread.

2. Post your expected output, and post what you're getting.

3. Tell us how you are testing it. Are you using a simulator (which one ?)
or a hardware board (give the manufacturer and model number).

4. Post your PIC.

5. Post your compiler version.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group