C 中结构体数组的指针

发布于 2024-11-17 04:39:35 字数 1080 浏览 3 评论 0原文

请让我知道如何将 C 中结构数组的指针作为函数参数传递。

下面是我的代码。

#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);
}

错误信息

"3.c", line 34: argument #1 is incompatible with prototype:
        prototype: pointer to pointer 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

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 pointer 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

反目相谮 2024-11-24 04:39:35

您只需传递数组,它就会衰减到指向第一个数组元素的指针:

set( alert, &index, p );

请注意,我还纠正了将整数作为第二个参数的指针传递的第二个错误。

编辑0:

我错过了PALERT的声明 - 你的函数定义是错误的,它应该是这样的:

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

我知道,数组和指针在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 :)

╄→承喏 2024-11-24 04:39:35

实际上您不能将数组传递给函数。当你这样做时,会发生什么,而是传入一个指向数组中第一个元素的指针。 (该过程通常被描述为“数组衰减为指针”)。

也就是说,

set(alert, index, p);

与以下内容相同:(

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

请注意,您将其称为 set(alert[5], index, p); ,这只是传入数组的 6. 元素,顺便说一句是无效的,因为你的数组只有 5 个元素的空间。)

因此,当你想要将数组传递给函数时,你要做的就是

  1. 传递一个指向数组中第一个元素的指针(这可以通过编写 <代码>数组名称或&name_of_array[0]
  2. 添加另一个参数,即数组的长度,您可能需要这个参数,就好像您的数组可以有不同的大小,并且您无法知道数组有多少个元素(如果有的话)。你得到的是一个指向它的第一个元素的指针:

让我们暂时跳过上面的第 2 项,你可以这样做:

    //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;
    }

并像这样调用它:

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

顺便说一句,除非你有充分的理由,否则尽量不要像你那样隐藏 typedef 中的指针在 typedef Alert 中*PALERT; 这样做常常会让人感到困惑。

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.

巷雨优美回忆 2024-11-24 04:39:35

删除数组括号,它应该可以工作。
原因是数组表示法是表示内存中项目序列的更简单的方法。例如,在数组 a[5] 中,您可以将第三个元素作为 a[3] 或 *(a+3) 访问。

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).

忆伤 2024-11-24 04:39:35

您的函数采用 PALERT *[5] 类型。您将传入 Alert[5]。您的代码还存在其他问题需要修复才能成功运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文