仅将Char值传递给函数的指针 [英] Passing only Char value to a Pointer of a function

查看:73
本文介绍了仅将Char值传递给函数的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在尝试将char值传递给参数为字符指针的函数.但是在执行过程中,程序异常终止,但是如果我传递一个字符串值,则说"Hello World"就可以了.下面是函数定义和伪代码FYI.

Hi I am trying to pass a char value to a function whose parameter is a character pointer. But during the execution the program terminates abnormally but if i pass a string value say "Hello World" it works fine. Below is the function definition and pseudo code FYI.

int MyFunc( int iParam1, TCHAR * pParam );



现在在主函数中,我以这种方式调用该函数.



now in main function i m calling the function this way.

int main()
{
  MyFunc( 1, 2, 'C' );
  return 0;
}




我是否正确传递了char值,还是需要遵循其他标准?
我正在Windows上通过C来执行此操作.如果我错了,请纠正我.




Am i passing the char value properly or do i need to follow a different standard.
I am doing this through C on windows. Please correct me if I am wrong.

推荐答案

好吧,您正在传递3个参数,而MyFunc仅接受2个,因此我们丢弃了2.

MyFunc期望第二个参数期望指向TCHAR的指针.您可以通过多种方式做到这一点,下面是一些示例
well you''re passing 3 arguments while MyFunc only accepts 2, so we throw away the 2.

the MyFunc expects second argument expects a pointer to a TCHAR. You can do that in multiple ways here are some examples
TCHAR ch = 'C';
MyFunc(1, &ch);

TCHAR* str = "C";
MyFunc(1, str);



这可能很有趣
C ++中的指针用法:从入门到高级 [



This might be of interest
Pointers Usage in C++: Beginners to Advanced[^]


在MyFunc中,您必须检查char指针以不要不为空

然后调试程序以查找谁使用空输入参数调用您的函数.
in MyFunc you must check your char pointer to don''t be null

and then debug your program to find who call your function with null input argument.


这篇关于仅将Char值传递给函数的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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