使用参考打印字符值

发布于 2025-01-30 19:10:36 字数 1268 浏览 2 评论 0原文

我很难尝试printf() a char变量。

我声明了两个数组,一个是品牌名称,另一个用于值,基于哪个值是最大的,我根据数组中的位置获得了适当的品牌名称。

好吧,打印marca在这里工作得很好。但是,当我尝试在功能外面打印它时,它不会。

有什么想法吗?

最小可重现的例子:

#include <stdio.h>
#include <string.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

void fabricanteDeModa(char *);

int main()
{
    char marca;
    fabricanteDeModa(&marca);
    printf("%s", marca); //Not working (already tried changing to %c)

    return 0;
}

void fabricanteDeModa(char *marca)
{
    int apple = 5;
    int xiaomi = 10;
    int samsung = 7;
    int htc = 12;
    int lg = 15;
    int zte = 10;
    int nokia = 2;

    char marcas[7][10] = {
        "apple",
        "xiaomi",
        "samsung",
        "htc",
        "lg",
        "zte",
        "nokia"
    };

    int valores[7] = { apple, xiaomi, samsung, htc, lg, zte, nokia };
    int i;
    int maxVal = valores[0];
    int pos = 0;

    for (i = 1; i < 7; ++i)
    {
        if (valores[i] > maxVal)
        {
            maxVal = valores[i];
            // Find the index of the max value
            pos = i;
        }
    }
    marca = marcas[pos];
    printf("%s\n", marca); //This Works
}

I'm having trouble trying to printf() a char variable.

I declare two arrays, one for the brand names and the other for the values, based on which value is the biggest I get the appropriate brand name based on the position in the array.

Well, printing marca here works perfectly. But when I try to print it outside the function it doesn't.

Any idea?

Minimal reproducible example:

#include <stdio.h>
#include <string.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

void fabricanteDeModa(char *);

int main()
{
    char marca;
    fabricanteDeModa(&marca);
    printf("%s", marca); //Not working (already tried changing to %c)

    return 0;
}

void fabricanteDeModa(char *marca)
{
    int apple = 5;
    int xiaomi = 10;
    int samsung = 7;
    int htc = 12;
    int lg = 15;
    int zte = 10;
    int nokia = 2;

    char marcas[7][10] = {
        "apple",
        "xiaomi",
        "samsung",
        "htc",
        "lg",
        "zte",
        "nokia"
    };

    int valores[7] = { apple, xiaomi, samsung, htc, lg, zte, nokia };
    int i;
    int maxVal = valores[0];
    int pos = 0;

    for (i = 1; i < 7; ++i)
    {
        if (valores[i] > maxVal)
        {
            maxVal = valores[i];
            // Find the index of the max value
            pos = i;
        }
    }
    marca = marcas[pos];
    printf("%s\n", marca); //This Works
}

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

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

发布评论

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

评论(2

情魔剑神 2025-02-06 19:10:36

您想要的是制造商品牌,其价值最大,然后打印出来。为了使我能够工作,这是我更改的位。

首先,我没有定义指向角色的指针(从根本上是与“ int”指针相同),而是定义了一个角色阵列,该阵列与最大的制造商名称一样大。可能还有其他方法可以初始化字符数组,但这是我知道的最简单的方法。然后,我将字符串名称作为指针引用到您的函数,如以下代码段中所述。

char marca[10];
fabricanteDeModa(marca);

在您当前的“ FabricantDemoda”功能的迭代中,您将字符箭头引用作为参数,但由于目前的影响,它在该功能中没有被更新。因此,我添加了我相信您正在尝试做的事情,这是将制造商的名称存储在该字符串中。为此,我添加了一个“ strcpy”命令,以将制造商的名称放入您的主级字符阵列中。

    }
    strcpy(marca, marcas[pos]);   /* Added this code */
    printf("Largest element = %d\n", pos);
    printf("La marca de moda es : %s\n", marcas[pos]); //This Works
}

传递和更新字符串(字符阵列)可能很棘手。我希望这为您澄清。

问候。

What it looks like you want is the manufacturer brand with the largest value returned to your string and then printed. In order for me to get that to work, here are the bits I changed.

First, instead of defining a pointer to a character (which is fundamentally the same as a pointer to an "int") I defined a character array that was as large as the largest possible manufacturer name. There are probably other ways to initialize the character array, but this is the simplest way I know. I then pass the string name as the pointer reference to your function as noted in the following code snippet.

char marca[10];
fabricanteDeModa(marca);

In your current iteration of the "fabricantDeModa" function, you have the character arrary reference as your parameter, but as it currently stands, it was not getting updated within the function. So, I added what I believe you were attempting to do which is store the manufacturer's name within that string. To do that I added a "strcpy" command to place the manufacturer's name into your main level character array.

    }
    strcpy(marca, marcas[pos]);   /* Added this code */
    printf("Largest element = %d\n", pos);
    printf("La marca de moda es : %s\n", marcas[pos]); //This Works
}

Passing and updating strings (character arrays) can be tricky. I hope this clarifies things for you.

Regards.

韬韬不绝 2025-02-06 19:10:36

MARCA应定义为char *或更好的const char *Fabricantedemoda应返回品牌名称,而不是将指针指向char如张贴。

要返回字符串,阵列marcas不应是char> char的2D数组,带有自动存储(非静态local变量),其元素在函数返回后将无法访问其元素,您可以使用字符串指针数组。

这是一个修改版本:

#include <stdio.h>

const char *fabricanteDeModa(void);

int main() {
    const char *marca = fabricanteDeModa();
    printf("%s\n", marca);
    return 0;
}

const char *fabricanteDeModa(void) {
    int apple = 5;
    int xiaomi = 10;
    int samsung = 7;
    int htc = 12;
    int lg = 15;
    int zte = 10;
    int nokia = 2;

    const char *marcas[7] = { "apple", "xiaomi", "samsung", "htc", "lg", "zte", "nokia" };
    int valores[7] = { apple, xiaomi, samsung, htc, lg, zte, nokia };
    int i;
    int maxVal = valores[0];
    int pos = 0;

    for (i = 1; i < 7; ++i) {
        if (valores[i] > maxVal) {
            maxVal = valores[i];
            // Update the index of the max value
            pos = i;
        }
    }
    printf("Largest element = %d\n", pos);
    printf("La marca de moda es : %s\n", marcas[pos]); //This Works
    return marcas[pos];
}

marca should be defined as a char * or better const char * and fabricanteDeModa should return the brand name, not take a pointer to char as posted.

To return the string, the array marcas should not be a 2D array of char with automatic storage (non-static local variable) whose elements will not be accessible after the function returns, you can instead use an array of string pointers.

Here is a modified version:

#include <stdio.h>

const char *fabricanteDeModa(void);

int main() {
    const char *marca = fabricanteDeModa();
    printf("%s\n", marca);
    return 0;
}

const char *fabricanteDeModa(void) {
    int apple = 5;
    int xiaomi = 10;
    int samsung = 7;
    int htc = 12;
    int lg = 15;
    int zte = 10;
    int nokia = 2;

    const char *marcas[7] = { "apple", "xiaomi", "samsung", "htc", "lg", "zte", "nokia" };
    int valores[7] = { apple, xiaomi, samsung, htc, lg, zte, nokia };
    int i;
    int maxVal = valores[0];
    int pos = 0;

    for (i = 1; i < 7; ++i) {
        if (valores[i] > maxVal) {
            maxVal = valores[i];
            // Update the index of the max value
            pos = i;
        }
    }
    printf("Largest element = %d\n", pos);
    printf("La marca de moda es : %s\n", marcas[pos]); //This Works
    return marcas[pos];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文