麻烦破译函数参数:指针和指向指针的指针 [英] Trouble deciphering function parameters: pointer and pointer to pointer

查看:249
本文介绍了麻烦破译函数参数:指针和指向指针的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

$ b $

我试图使用一个函数来代替C中的scanf()。函数是由第三方编写的, b

ScanDecimal16uNumber - 扫描十进制16位无符号数
ANSIC原型:字节ScanDecimal16uNumber(const unsigned char ** str,word * val)

str:字节 - 要扫描的字符串,从0x开始。它还会返回到它已经扫描的位置。

val:指针 - 指向值的指针

返回值:字节 - 错误代码


我遇到的麻烦是两个输入,在与更熟悉C我想我已经掌握了第二个输入。* val是我想要获得的值(16位十进制数),val是它的地址。我对字如何转换为int有点模糊,是我所需要的,但感觉非常舒服。



第一个输入实际上给我带来麻烦,无论是语法还是它的功能。 **?它是一个地址到一个地址?什么?以及如何在没有首先建立地址的情况下拥有一个地址到地址?怎么样的声明看起来像这样?



真的很感谢函数和语法的一些建议,我重新认识了C a这感觉有点像深刻的结局。



预先感谢您,
Yusif Nurizade


$ b 更新1.0:我用你的建议修改了代码,目前没有错误只有三个警告(最好是这样!)我发布了下面的代码:

  const unsigned char * addressPrime =0x1f307100; 
const unsigned char * addressOfAddressPrime = addressPrime;
word dutyCycle;


PWM1_Enable();
UTIL1_ScanDecimal16uNumber(& addressPrime,& dutyCycle);
PWM1_SetRatio16(dutyCycle);

此代码正在用于飞思卡尔FRDM KE02Z板上,并且您正在使用的新功能正在使用中通过实用程序扫描得到的值来控制PWM占空比。我收到的警告是:
$ b


  1. 在addressPrime的声明中,初始化中的指针目标在签名方面有所不同。

2/3。未使用的变量DeviceDataPrv / rtval是PWM函数的一部分,我现在不太担心。



我现在的问题是:


  1. 它们所指的签名是什么?

  2. 我应该多关注我选择的号码还是0x1f307100的工作?

  3. 由于这是为了动态更新PWM占空比,因此我应该使用一段时间循环扫描功能?


解决方案

以下是如何调用函数并使用结果它返回两个参数。

  int main(void){
const unsigned char * input =0x1f30 0x7100 ;
const unsigned char * p =输入;
单词val1,val2; / *单词应该与uint16_t相同。 /

字节r = ScanDecimal16uNumber(& p,& val1);

/ * p现在应指向
*输入字符串中两个数字之间的空格。这将允许您立即扫描下一个号码。 * /
字节q = ScanDecimal16uNumber(& p,& val2);

printf(%d%d \ n,val1,val2);
返回0;
}


I am trying to use a function meant to substitute scanf() in C. The function was written by a third party and is defined accordingly:

ScanDecimal16uNumber - Scans a decimal 16bit unsigned number ANSIC prototype: byte ScanDecimal16uNumber(const unsigned char **str, word *val)

str:byte - String to scan, starting with 0x. It returns as well until where it has scanned

val: Pointer to word - Pointer to value

Return value:byte - Error code"

The trouble I am having is with the two inputs. After conferring with someone much more well versed in C than I am, I think I have a grasp on the second input. *val is the value I am trying to obtain (the 16bit decimal number) and val is its address. I am a bit murky on how word transitions to int, which is what I need, but feel pretty comfortable with it.

The first input is really giving me trouble, both with syntax and what it does. First off, what is the **? Its an address to an address? Of what? And how can one have an address to an address without first establishing the address by itself? How would the declaration for something like that look like?

Would really appreciate some advice both with function and syntax. I'm getting reacquainted with C and this feels a bit like the deep end.

Thanks in advance, Yusif Nurizade

Update 1.0: Ive modified the code with your suggestions and currently have no errors with just three warnings (best it's been!) I am posting the code below:

  const unsigned char *addressPrime                 = "0x1f307100"; 
  const unsigned char *addressOfAddressPrime        = addressPrime; 
  word                dutyCycle;                                


  PWM1_Enable();
  UTIL1_ScanDecimal16uNumber(&addressPrime, &dutyCycle);
  PWM1_SetRatio16(dutyCycle);

This code is being used on a Freescale FRDM KE02Z board and the new functions you are seeing are being used to control a PWM duty cycle with the value I get from the utility scan. The warnings I am getting are:

  1. At the declaration for addressPrime "Pointer targets in initialization differ in signedness.

2/3. unused variables DeviceDataPrv/rtval which are part of the PWM function and I'm not too worried about now.

My questions at this point are:

  1. What is the signedness they are referring to?
  2. Should I give more attention to the number I choose or does 0x1f307100 work?
  3. Since this is meant to dynamically update a PWM duty cycle, should I be using a while loop for the scan function?

解决方案

Here is how to call the function and use the results it returns in both parameters.

int main(void) {
  const unsigned char *input = "0x1f30 0x7100";
  const unsigned char *p = input;
  word val1, val2;  /* A word should be the same as a uint16_t. */

  byte r = ScanDecimal16uNumber(&p, &val1);

  /* p should now point at the space between the two numbers in the
   *input string.  This will allow you to scan the next number immediately. */
  byte q = ScanDecimal16uNumber(&p, &val2);

  printf("%d %d\n", val1, val2);
  return 0;
}

这篇关于麻烦破译函数参数:指针和指向指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆