简体   繁体   中英

how to change part of argument passed to a function in C?

I have function

Oled_logo(const unsigned char *image)

so if i pass

unsigned char giphy_0 [] = { 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 };

eg -

Oled_logo(giphy_0);

it works

but i got 14 such images naming giphy_0 ,giphy_1 ... giphy_14

so i want to run these one by one to get an animation

i just want to pass arguments to Oled_logo like Oled_logo(giphy_0) or Oled_log(giphy_1) or Oleg_logo(giphy_2) ,so instead of doing it for 14 times ,i want to call Oled_logo function in loop and change part of argument giphy_imagenumber in loop.

So instead of writing code to call Oled_logo function for 14 times ,i want a loop where i pass giphy_imagenumber to function Oled_logo and this imagenumber is replaced by loop variable ,so i have to write only few lines in a loop. following i tried

unsigned char x[] = "giphy_0";
    volatile unsigned char y ;
    while(1)
    {
        for(int image_gif = 0 ; image_gif<=14;image_gif++)
        {

            x[6] = image_gif + 0x30;

        Oled_logo(x);

        ssd1306_UpdateScreen();
        HAL_Delay(4000);
        }
}

No error came ,but output is some garbage

I tried with strcat ,converted loop variable to string and concatanate with giphy_ and passed to Oled_logo function Same problem

What i am doing wrong ?

{ 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 }

is different from

"giphy_0"

What you are trying to achieve seems wrong or you are missing a piece.

My guess is you want an array like { giphy_0, giphy_1, ... } and then use the index in this array.

What you're attempting to do is pass in a string with the name of the variable in question an expecting the program to automatically replace this string with the contents of the variable of that name. That's not how C works.

The real problem here is how you've defined the arrays. Rather than having a set of arrays with names containing the array number:

unsigned char giphy_0 [] = { 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 };
unsigned char giphy_1 [] = { 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 };
unsigned char giphy_2 [] = { 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 };
...
unsigned char giphy_14 [] = { 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 };

Create an array of arrays:

unsigned char giphy[][11] = {
    { 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 },
    { 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 },
    { 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 },
    ...
    { 0x80, 0x81, 0x03, 0x07, 0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0 },
};

And pass loop through the outer dimension:

    for(int image_gif = 0 ; image_gif<=14; image_gif++)
    {
        Oled_logo(giphy[image_gif]);

        ssd1306_UpdateScreen();
        HAL_Delay(4000);
    }

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