我可以将char作为数组参数发送吗? [英] Can I send char as array argument?

查看:67
本文介绍了我可以将char作为数组参数发送吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个未受过训练的业余爱好者。关于编程的一切我都从互联网上学到了b $ b。感谢大家的慷慨支持。


这就是我所拥有的:


#define CONST_CHAR 0

void some_func(char * arg,int len)

{

// stuff

}


大多数时候有一个字符数组作为arg发送,但有时我只需要
需要发送一个字符。


不要笑,我知道这不行:

void main(无效)

{

some_func((char *)CONST_CHAR,1 );

}


这样做,这就是我现在正在做的事情:

void main(void)

{

char array [1];

array [0] = CONST_CHAR;

some_func(array, 1);

}


我的问题是:

有没有办法构建看起来更像第一个的arg

非工作的例子?如果有的话,是否会产生任何差别,而不是必须分配一个数组(尽管是一个字节)来发送一个

字符?

解决方案

" Uncle" < no@spam.com>写道:


[用于传递指向单个字符的指针]

不要笑,我知道这不起作用:
void main(void)
{
some_func((char *)CONST_CHAR,1);
}
这就是,它是什么我现在在做:
void main(void)
{char array [1];
array [0] = CONST_CHAR;
some_func(array, 1);
}


或者,您不需要数组语法:


char c = CONST_CHAR;

some_func(& c,1);

我的问题是:
有没有办法构建看起来更像第一个的arg
非工作的例子?




在C99中,你可以写

some_func((char []){CONST_CHAR});



some_func(&(char){CONST_CHAR});

但你可能没有C99编译器。

-

"这是一个很棒的答案。

它'偏离主题,这是不正确的,它没有回答这个问题。

- 理查德希思菲尔德


在星期五,2003年12月5日18:53:19 -0500,叔叔写道:

我是一个未受过训练的业余爱好者。关于编程的一切我都是从互联网上学到的。感谢大家的慷慨支持。

这就是我所拥有的:

#define CONST_CHAR 0
void some_func(char * arg,int len)
{
// stuff
}

大多数时候有一个字符数组作为arg发送,但有时我只需要发送一个性格。

不要笑,我知道这不起作用:
void main(void)
{
some_func((char *)CONST_CHAR ,1);
}

这样做,这就是我现在正在做的事情:
void main(void)
{
char array [1];
array [0] = CONST_CHAR;
some_func(array,1);
}

我的问题是:
有吗构建arg的方法看起来更像是第一个非工作的例子?如果有的话,是否会分配一个数组(尽管只有一个字节)来发送一个
字符会有什么不同吗?




#include< stdio.h>


void some_func(const char * arg,int len)

{

int i;

for(i = 0; i< len; ++ i)

printf("%c",arg [i]); < br $>
}


int main(无效)

{

const char CONST_CHAR =''0' ';

some_func(& CONST_CHAR,1);

返回0;

}



" Ben Pfaff" < bl*@cs.stanford.edu>在消息中写道

news:87 ************ @ pfaff.stanford.edu ...

" Uncle" < no@spam.com>写道:

[用于传递指向单个角色的指针]

不要笑,我知道这不起作用:
void main(void)
{
some_func((char *)CONST_CHAR,1);
}
这样做,这就是我的意思现在做:
void main(void)
{char array [1];
array [0] = CONST_CHAR;
some_func(array,1);
}



或者,您不需要数组语法:

char c = CONST_CHAR;
some_func(& c) ,1);

我的问题是:
有没有办法构建看起来更像第一个非工作示例的arg?


在C99中,你可以写
some_func((char []){CONST_CHAR});

some_func(&(char){CONST_CHAR} );
但你可能没有C99编译器。
-
这是一个很好的答案。
它已经关闭了 - 主题,这是不正确的,它没有回答这个问题。
- 理查德希思菲尔德




谢谢。


我想我没有C99。我正在使用免费赠品(lcc和gcc。)

当我使用some_func(&(char)CONST_CHAR)时,编译器抱怨:

左手边可以不会被分配到。

我确定它根本不会理解大括号。


I am an untrained hobbyist. Everything about programming I have learned
from the internet. Thank you all for your gracious support.

This is what I have:

#define CONST_CHAR 0
void some_func( char* arg, int len )
{
// stuff
}

Most times there is a character array sent as "arg", but sometimes I only
need to send a single character.

Don''t laugh, I know this doesn''t work:
void main( void )
{
some_func( (char*)CONST_CHAR, 1 );
}

This does, and it is what I''m doing now:
void main( void )
{
char array[1];
array[0] = CONST_CHAR;
some_func( array, 1 );
}

My questions are:
Is there a way to construct the arg that looks more like the first
non-working example? If there is, would it make any difference in not
having to allocate an array, ( albeit one byte, ) to send a single
character?

解决方案

"Uncle" <no@spam.com> writes:

[for passing a pointer to a single character]

Don''t laugh, I know this doesn''t work:
void main( void )
{
some_func( (char*)CONST_CHAR, 1 );
}

This does, and it is what I''m doing now:
void main( void )
{
char array[1];
array[0] = CONST_CHAR;
some_func( array, 1 );
}
Alternatively, you don''t need the array syntax:

char c = CONST_CHAR;
some_func(&c, 1);
My questions are:
Is there a way to construct the arg that looks more like the first
non-working example?



In C99, you can write
some_func((char[]){CONST_CHAR});
or
some_func(&(char){CONST_CHAR});
But you probably don''t have a C99 compiler.
--
"This is a wonderful answer.
It''s off-topic, it''s incorrect, and it doesn''t answer the question."
--Richard Heathfield


On Fri, 05 Dec 2003 18:53:19 -0500, Uncle wrote:

I am an untrained hobbyist. Everything about programming I have learned
from the internet. Thank you all for your gracious support.

This is what I have:

#define CONST_CHAR 0
void some_func( char* arg, int len )
{
// stuff
}

Most times there is a character array sent as "arg", but sometimes I only
need to send a single character.

Don''t laugh, I know this doesn''t work:
void main( void )
{
some_func( (char*)CONST_CHAR, 1 );
}

This does, and it is what I''m doing now:
void main( void )
{
char array[1];
array[0] = CONST_CHAR;
some_func( array, 1 );
}

My questions are:
Is there a way to construct the arg that looks more like the first
non-working example? If there is, would it make any difference in not
having to allocate an array, ( albeit one byte, ) to send a single
character?



#include <stdio.h>

void some_func(const char* arg, int len )
{
int i;
for(i = 0; i < len; ++i)
printf("%c", arg[i]);
}

int main(void)
{
const char CONST_CHAR = ''0'';
some_func(&CONST_CHAR, 1);
return 0;
}



"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...

"Uncle" <no@spam.com> writes:

[for passing a pointer to a single character]

Don''t laugh, I know this doesn''t work:
void main( void )
{
some_func( (char*)CONST_CHAR, 1 );
}

This does, and it is what I''m doing now:
void main( void )
{
char array[1];
array[0] = CONST_CHAR;
some_func( array, 1 );
}



Alternatively, you don''t need the array syntax:

char c = CONST_CHAR;
some_func(&c, 1);

My questions are:
Is there a way to construct the arg that looks more like the first
non-working example?



In C99, you can write
some_func((char[]){CONST_CHAR});
or
some_func(&(char){CONST_CHAR});
But you probably don''t have a C99 compiler.
--
"This is a wonderful answer.
It''s off-topic, it''s incorrect, and it doesn''t answer the question."
--Richard Heathfield



Thanks.

I guess I don''t have C99. I''m using freebies ( lcc and gcc. )
When I used some_func( &(char)CONST_CHAR ) the compiler complained:
"left hand side can''t be assigned to."
I''m sure it wouldn''t understand the braces at all.


这篇关于我可以将char作为数组参数发送吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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