使用参考打印字符值
我很难尝试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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要的是制造商品牌,其价值最大,然后打印出来。为了使我能够工作,这是我更改的位。
首先,我没有定义指向角色的指针(从根本上是与“ int”指针相同),而是定义了一个角色阵列,该阵列与最大的制造商名称一样大。可能还有其他方法可以初始化字符数组,但这是我知道的最简单的方法。然后,我将字符串名称作为指针引用到您的函数,如以下代码段中所述。
在您当前的“ FabricantDemoda”功能的迭代中,您将字符箭头引用作为参数,但由于目前的影响,它在该功能中没有被更新。因此,我添加了我相信您正在尝试做的事情,这是将制造商的名称存储在该字符串中。为此,我添加了一个“ strcpy”命令,以将制造商的名称放入您的主级字符阵列中。
传递和更新字符串(字符阵列)可能很棘手。我希望这为您澄清。
问候。
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.
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.
Passing and updating strings (character arrays) can be tricky. I hope this clarifies things for you.
Regards.
MARCA
应定义为char *
或更好的const char *
和Fabricantedemoda
应返回品牌名称,而不是将指针指向char
如张贴。要返回字符串,阵列
marcas
不应是char> char
的2D数组,带有自动存储(非静态local变量),其元素在函数返回后将无法访问其元素,您可以使用字符串指针数组。这是一个修改版本:
marca
should be defined as achar *
or betterconst char *
andfabricanteDeModa
should return the brand name, not take a pointer tochar
as posted.To return the string, the array
marcas
should not be a 2D array ofchar
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: