473,655 Members | 3,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I send char as array argument?

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_CH AR, 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?

Nov 13 '05 #1
12 5981
"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_CH AR, 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(&(cha r){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
Nov 13 '05 #2
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_CH AR, 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(&CONS T_CHAR, 1);
return 0;
}
Nov 13 '05 #3

"Ben Pfaff" <bl*@cs.stanfor d.edu> wrote in message
news:87******** ****@pfaff.stan ford.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_CH AR, 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(&(cha r){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_CH AR ) the compiler complained:
"left hand side can't be assigned to."
I'm sure it wouldn't understand the braces at all.

Nov 13 '05 #4
"Uncle" <no@spam.com> writes:
"Ben Pfaff" <bl*@cs.stanfor d.edu> wrote in message
news:87******** ****@pfaff.stan ford.edu...
In C99, you can write
some_func((char[]){CONST_CHAR});
or
some_func(&(cha r){CONST_CHAR}) ;
But you probably don't have a C99 compiler.


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


The braces are essential. Without the braces, it's a cast; with
the braces, it's a compound literal.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz
Nov 13 '05 #5
Uncle wrote:
I am an untrained hobbyist. Everything about programming I have learned
from the internet. Thank you all for your gracious support.
That's a very poor way to learn. Most of us work our asses off to learn
from real sources, and you are putting yourself way behind the game by
using the (mostly completely inadequate) sources available on the 'net.

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.
char single_characte r = 's';
some_func(&sing le_character, 1);

Or create a separate function that takes a single char.

Don't laugh, I know this doesn't work:
void main( void )
main returns int. void is not and never has been an acceptable return
type for main.

int main(void)
{
some_func( (char*)CONST_CH AR, 1 );
This cast isn't even necessary, but the result (with or without the
cast) is to pass the function a null pointer.
}

This does, and it is what I'm doing now:
void main( void )
int 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?


A single char (or rather, the address of a single char) is mostly
indistinguishab le from a 1 element char array.

Beware, though. Many functions taking a char* actually expect a pointer
to a (properly terminated) string. In which case, the best you can do
with a single char is pass the empty string:

char c = '\0';
some_func(&c, 1);

(Usually a function taking a string wouldn't also take its length, of
course.)

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #6
"Uncle" <no@spam.com> wrote in
news:fL******** ******@bignews4 .bellsouth.net on Fri 05 Dec 2003 05:53:21p:

"Ben Pfaff" <bl*@cs.stanfor d.edu> wrote in message
news:87******** ****@pfaff.stan ford.edu...
"Uncle" <no@spam.com> writes:

[for passing a pointer to a single character]
> 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(&(cha r){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. )


gcc implements a surprising amount of C99, but it isn't a conformant C99
compiler in any of its modes. C89 and C90 are more common these days, and
one could argue that they are the `true' standard in lieu of wide
acceptance of C99.

(gcc also implements some odd extensions to C, such as nested subroutines
(that is, subroutines declared inside subroutines, as per Pascal and Ada),
but such things are not on-topic here. I mention it simply because I
cannot understand why people writing a C compiler felt compelled to
implement nested subroutines.)

I don't know how conformant lcc is to anything, but I don't use it.

Nov 13 '05 #7
"Uncle" <no@spam.com> wrote in message news:<xS******* *******@bignews 6.bellsouth.net >...
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_CH AR, 1 );
} Presumably, you are passing the address of this character to modify
its contents. However if it is a constant ...is it? It sure seems so
(from CONST_CHAR) ... then you may not modify a constant. The results
of such
a thing is undefined.
For instance :
char *a="abc";
a[0]='k';
This is undefined behaviour.

This does, and it is what I'm doing now:
void main( void )
{
char array[1]; /* Or char ch; */
array[0] = CONST_CHAR; /* Or ch=CONST_CHAR; */
some_func( array, 1 ); /* Or some_func(&ch,1 );
} Here you are storing it in a variable and passing its address. Quite
different from casting a 0 to a char*, I would say.
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?


You just have to store your literal someplace and pass the address
I'm
afraid.
Regards,
Anupam
Nov 13 '05 #8

"Anupam" <an************ **@persistent.c o.in> wrote in message
news:aa******** *************** **@posting.goog le.com...
This is what I have:

#define CONST_CHAR 0
void some_func( char* arg, int len )
{
// stuff
} Presumably, you are passing the address of this character to modify
its contents. However if it is a constant ...is it? It sure seems so
(from CONST_CHAR) ... then you may not modify a constant. The results
of such
a thing is undefined.
Actually some_func is basically just this:
void some_func( char* arg, int len )
{
mutex_release() ;
try_send( fd, arg, len, 0 );
mutex_aquire();
}
If the thread doesn't have the mutex it won't get here,
and try_send is just a socket send that closes fd and sets an error flag
on failure.
I put the mutex stuff in because I was afraid send might take a little time
to return.
"arg" is not modified.
For instance :
char *a="abc";
a[0]='k';
This is undefined behaviour.


I don't understand why this is undefined.
int main( void )
{
char *a="abc";
a[0]='k';
printf( a );
}
ouputs "kbc" on both of my systems. I would like to know where it might not
work, since I have quite a bit of this sort of thing in my code.

Just wondering, too; ( remember-- I'm untrained )
Is char*a above null terminated by the assignment? Is there a valid a[4] ==
'\0' ?
Nov 13 '05 #9
"Uncle" <no@spam.com> wrote in
news:kd******** *****@bignews3. bellsouth.net:

"Anupam" <an************ **@persistent.c o.in> wrote in message
news:aa******** *************** **@posting.goog le.com...
For instance :
char *a="abc";
a[0]='k';
This is undefined behaviour.

I don't understand why this is undefined.


http://www.eskimo.com/~scs/C-faq/q16.6.html
int main( void )
{
char *a="abc";
a[0]='k';
printf( a );
}
ouputs "kbc" on both of my systems. I would like to know where it
might not work, since I have quite a bit of this sort of thing in my
code.
also http://www.eskimo.com/~scs/C-faq/q1.32.html
Just wondering, too; ( remember-- I'm untrained )
Is char*a above null terminated by the assignment? Is there a valid
a[4] == '\0' ?


You are forgetting that C arrays are zero-based. Hence:

char *a = "abc";

means

a[0] == 'a' && a[1] == 'b' && a[2] == 'c'; && a[3] == '\0'

a[4] is out of bounds.

Also,

http://www.eskimo.com/~scs/C-faq/q4.10.html

seems relevant to your subject line.
--
A. Sinan Unur
as**@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uc*@ftc. gov
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
20939
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
5
4544
by: spoilsport | last post by:
Ive got to write a multi-function program and I'm plugging in the functions but I keep getting this error from line 40. Im new to this and cant find an answer anywhere. Sam #include <stdio.h> int main (void)
5
3962
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
8
10086
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) When I asked why, I was told that it facilitated calling it as:
9
7624
by: rob.kirkpatrick | last post by:
Hello I need to populate an array of char arrays at run-time. A very simplifed version of the code is below. char ** list should contain cnt char arrays. The values of char ** list are set by the function foo(). A pointer to char ** list is passed to foo() as an argument. The problem is that when foo() returns, char ** list contains rubbish
18
4044
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
9
2255
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
17
2273
by: mdh | last post by:
In trying to understand the issue, I wrote this; #include <stdio.h> void f_output(char arg1, int limit); int main () { f(); return 0; }
8
16242
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc = {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.