开发者

Pointer to array of structures in C

开发者 https://www.devze.com 2023-03-15 17:32 出处:网络
Please let me know how can I pass a pointer to the array of structures in C as a function argument. Below is my code.

Please let me know how can I pass a pointer to the array of structures in C as a function argument.

Below is my code.

#include <stdio.h>
#include<strings.h>



typedef struct _Alert
{
    char   MerchantNo[21];
    time_t last_update;
} Alert;
typedef Alert *PALERT;


int set(PALERT palertMerch[5], int *merchnoIndex, char * txnMerchant)
{
    strcpy(palertMerch[*merchnoIndex]->MerchantNo, txnMerchant);
    *(merchnoIndex) = *(merchnoIndex) + 1 ;

    return 0;
}

int main()
{
    Alert alert[5];

    for(int i =0; i<5;i++)
    {
        memset(alert[i].MerchantNo, 0x00, 21);
        alert[i].last_update = (time_t)0;
    }

    char *p = "SACHIN";
    int index = 0;
    set(alert[5], index, p);
}

Error message

"3.c", line 34: argument #1 is incompatible with prototype:
        prototype: pointer to po开发者_如何学Gointer to struct _Alert {array[21] of char MerchantNo, long last_update} : "3.c", line 14
        argument : struct _Alert {array[21] of char MerchantNo, long last_update}
"3.c", line 34: warning: improper pointer/integer combination: arg #2
cc: acomp failed for 3.c


You just pass the array, it'll get decayed to the pointer to the first array element:

set( alert, &index, p );

Note that I also corrected your second error of passing integer as a pointer for the second argument.

Edit 0:

I missed the declaration of PALERT - your function definition is wrong, it should be something like:

int set( PALERT palertMerch, int* merchnoIndex, const char* txnMerchant )
{
    assert( *merchnoIndex >= 0 && *merchnoIndex < 5 );
    strcpy( palertMerch[*merchnoIndex].MerchantNo, txnMerchant );
    ...
}

I know, arrays and pointers are a bit confusing in C, and you were trying to jump to arrays of pointers already :)


You actually cannot pass an array to a function. What happens when you do, is that a pointer to the first element in an array is passed in instead. (That proccess is often described as "an array decays into a pointer").

That is,

set(alert, index, p);

Is just the same as:

set(&alert[0], index, p);

(Note that you called it as set(alert[5], index, p); , this just passes in the 6. element of your array , which btw is invalid, as your array only have room for 5 elements.)

So, what you do when you want to pass an array to a function is you

  1. Pass a pointer to the first element in the array (which can be done by just writing name_of_array or &name_of_array[0]
  2. Add another argument that is the length of the array. You might need this as if your array can have different sizes, and you cannot know how many elements an array have, if all you got is a pointer to its first element:

Let's skip item 2. above for now, you can just do:

    //PALERT is already a pointer, otherwise specify the first argument as:
    //ALERT *palertMerch
    int set(PALERT palertMerch, int *merchnoIndex, char * txnMerchant) 
    {
        strcpy(palertMerch[*merchnoIndex]->MerchantNo, txnMerchant);
        *(merchnoIndex) = *(merchnoIndex) + 1 ;

        return 0;
    }

And call it like:

char *p = "SACHIN";
int index = 0;
set(alert, index, p);

btw, unless you have a good reason, try not to hide a pointer in a typedef as you do in typedef Alert *PALERT; doing so often gets confusing.


remove the array brackets and it should work. The reason for this is that array notation is an easier way to represent sequences of items in memory. For example, in an array a[5], you can access the third element as a[3] or *(a+3).


Your function takes type PALERT *[5]. You are passing in Alert[5] instead. There are other problems with your code that need fixing before it will successfully run.

0

精彩评论

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