简体   繁体   中英

Decode array of 2 elements passed to a function using pointer in c

I want to implement a function where we pass an array of 2 elements. But while getting actual values I'm getting only one element of array. How to get the second element also?

#include<stdio.h>
#include<conio.h>
#include<stdint.h>

uint16_t a;
uint8_t arr[2];

void mcp_write(uint8_t reg, uint8_t *buffer, uint8_t length)
{
    uint16_t buff;

    printf("%d\n",reg);
    printf("%d\n",*buffer);
    printf("%d\n",length);
}

int main()
{
    arr[0]=15;
    arr[1]=255;
    mcp_write(1,arr,1);

    getch();
}

Output is

1
255
1
void mcp_write(uint8_t reg, uint8_t *buffer, uint8_t length)
{
    for (int i = 0; i < length; i++)
    {
        printf("%d\n",i);
        printf("%d\n",reg);
        printf("%d\n",buffer[i]);
        printf("%d\n",length);
    }
}

int main()
{
    arr[0]=15;
    arr[1]=255;
    mcp_write(1,arr,sizeof(arr)/sizeof(arr[0]));

    getch();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM