或者,您不需要数组语法: char c = CONST_CHAR; some_func(& c) ,1); 谢谢。 我想我没有C99。我正在使用免费赠品(lcc和gcc。) 当我使用some_func(&(char)CONST_CHAR)时,编译器抱怨: 左手边可以不会被分配到。 我确定它根本不会理解大括号。 I am an untrained hobbyist. Everything about programming I have learnedfrom the internet. Thank you all for your gracious support.This is what I have:#define CONST_CHAR 0void some_func( char* arg, int len ){// stuff}Most times there is a character array sent as "arg", but sometimes I onlyneed 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 firstnon-working example? If there is, would it make any difference in nothaving to allocate an array, ( albeit one byte, ) to send a singlecharacter? 解决方案In C99, you can writesome_func((char[]){CONST_CHAR});orsome_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#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;} Alternatively, you don''t need the array syntax: char c = CONST_CHAR; some_func(&c, 1); 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 HeathfieldThanks.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作为数组参数发送吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 07:55
查看更多