开发者

passing an array of string to a function

My program is

#define ARRLEN 10
#define SIZEALCO 128
#define MAX_STRING_LENGTH 12

in main function,

char TYPEDATA_XML_FN[ARRLEN][SIZEALCO];
char TYPEDATA_MD5_FN[ARRLEN][SIZEALCO]; 
char identifier[ARRLEN][MAX_STRING_LENGTH];
char Temppath[SIZEALCO];
int arraynum;

// ...

arraynum = 0;
for(arraynum = 0; arraynum <ARRLEN; arraynum++)
{
    /* Create the file name  with the path*/
    strcpy(Temppath,"/fw/TYPEDATA/");
    nameFil(Temppath,identifier[arraynum],TYPEDATA_XML_FN[arraynum],TYPEDATA_MD5_FN[arraynum]);
}

subfunction is :

void nameFil(char *SourPath,char *InPinName,char *FilePathNameXml,char *FilePathNameMd5)
{
    sprintf(FilePathNameXml, "%s\\%s_TYPEDATA.XML",SourPath,InPinName);
    sprintf(FilePathNameMd5, "%s\\%s_TYPEDATA.MD5",SourPath,InPinName); 
}

I checked with your example. I used (trial)

char** a = calloc(ARRLEN, sizeof(char *));
for(i = 0; i < ARRLEN ; ++i)
a[i] = ucmalloc(MAX_STRING_LENGTH);
pase(a);

subfunction :

void pase(char b[ARRLEN][MAX_STRING_LENGTH])
{
    // ...
}

Now I got the warning message as "warning: passing arg 1 of `pase' from incompatible pointer type".

Actually, I would like to pass the full string array identifier,TYPEDATA_XML_FN,TYPEDATA_MD5_FN. N开发者_开发技巧ow I am passing single string to the subfunction. Kindly guide me. Thank you


The prototype void pase(char b[ARRLEN][MAX_STRING_LENGTH]) is rather mis-leading,

void pase(char b[][MAX_STRING_LENGTH])

would be better, since otherwise there is the implication of bounds checking (the first array dimension is ignored).

The reason why you get "incompatible pointer type" is because a is an array of pointers. If a was incremented (as a pointer itself) then the address would increase by the size of a pointer. However, b is an array of arrays of size MAX_STRING_LENGTH, so if b was incremented then the value would increase by MAX_STRING_LENGTH.

The way you have allocated the array a will (probably) not give you contiguous memory, which is what is required here. You could achieve what you want using an array of pointers, but you really must decide what you want to do. If you want to use [][] notation then you need to

calloc(MAX_STRING_LENGTH,ARRLEN);


You are getting confused because although an one dimensional array char[] behaves like a pointer char *, in two dimensions a char[][N] is not convertible to a char **, being actually more like a (*char)[N] (pointer to arrays of length n of char).

So if you want to make a function that receives a two dimensional array, you have two choices:

  1. Use pointers to pointers:

    void f(char ** array, int nrows, int ncols);
    

    To create a char**, do like you are already doing now: create an array for pointers and call malloc for each one of them.

  2. Use two dimensional arrays:

    void f(char array[][NCOLS], int nrows);
    //note: NCOLS is a compile time constant now
    //NROWS is the first dimension and can be omited from array[NROWS][NCOLS]
    

    The tricky bit is malloc-ing a two dimensional array:

    char (*my_array)[NCOLS];
    //my_identifiers is a pointer to arrays of length NCOLS
    // it can be passed to any function expecting a car[][NCOLS]
    
    my_array = malloc(number_of_rows*(sizeof *my_array));
    

You can also make it easier to understand all of this with a good choice of typedefs:

typedef char MY_STRING[MAX_STR_LENGTH];
//define my strings as arrays of MAX_STRING_LENGTH

void f(MY_STRING array[]);

...    

MY_STRING *arr = malloc(nstrings*sizeof(MY_STRING));
f(arr);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜