View previous topic :: View next topic |
Author |
Message |
kathir Guest
|
passing 2 dimensional array in a function |
Posted: Sat May 24, 2008 7:56 pm |
|
|
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;
} |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
ysaacb
Joined: 12 Jun 2006 Posts: 19
|
|
Posted: Wed Dec 31, 2008 9:25 am |
|
|
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 ? |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Dec 31, 2008 10:32 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
ysaacb
Joined: 12 Jun 2006 Posts: 19
|
|
Posted: Wed Dec 31, 2008 1:14 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
ysaacb
Joined: 12 Jun 2006 Posts: 19
|
|
Posted: Thu Jan 01, 2009 4:08 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 01, 2009 4:01 pm |
|
|
The test program shown below displays the following output:
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);
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Jan 07, 2009 12:12 pm |
|
|
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);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Jan 07, 2009 3:16 pm |
|
|
I can't see from your post what doesn't work. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 07, 2009 3:18 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Jan 08, 2009 12:22 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Thu Jan 08, 2009 7:15 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Jan 08, 2009 1:50 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
diogoc
Joined: 12 Feb 2008 Posts: 19
|
|
Posted: Wed Jul 15, 2009 7:38 am |
|
|
Hi,
I have the same problem. There are another way to pass a 2d array to a function?
thanks |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 15, 2009 11:32 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|