Solved

more elegant way to load characters?

Posted on 2009-04-14
8
382 Views
Last Modified: 2012-05-06
Hey, I'm a little rusty in C.  Is there a more elegant way to load a series of chars of the caller's choice into an outgoing array?  I don't want to use up RAM to allocate a separate char array, nor use string manipulation functions; but I'd prefer to write

outmsg_cmd("ABC");
instead of
outmsg_cmd('A', 'B', 'C');

to get data loaded into the characters 2-4 of my array.  Simply because the former is more readable.

Is there a way to achieve this?  

Thanks.

char out_buf[50];
 
void outmsg_cmd(char a, char b, char c)
{
  out_buf[2] = a;
  out_buf[3] = b;
  out_buf[4] = c;
}
 
void foo()
{
  outmsg_cmd('A', 'B', 'C');
}

Open in new window

0
Comment
Question by:riceman0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 24138331
Hi riceman0,

IMO you could do it like this:

> void outmsg_cmd( char* pBuf )
> {
>  strncpy( out_buf + 1, pBuf, 3 );
> }

Hope that helps,

ZOPPO
0
 

Author Comment

by:riceman0
ID: 24138394

For nuanced reasons (this is an interrupt service routine) I don't want to use string manipulation functions.  

But I guess this would work, wouldn't it:

> void outmsg_cmd( char* pBuf )
> {
  out_buf[2] = *pBuf;
  out_buf[3] = *(pBuf+1);
  out_buf[4] = *(pBuf+2);

> }
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24138446
Without using string functions, memcpy is your best bet.
http://www.cplusplus.com/reference/clibrary/cstring/memcpy/

Frankly, I'd use strncpy as Zoppo suggests.
#include <cstdio>
 
char out_buf[50];
 
void outmsg_cmd(char const * p)
{
	memcpy(&out_buf[2], p, sizeof(char) * 3);
}
 
void foo()
{
  outmsg_cmd("ABC");
}
 
int main()
{
	foo();
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40

Expert Comment

by:evilrix
ID: 24138486
out_buf[2] = *pBuf;
out_buf[3] = *(pBuf+1);
out_buf[4] = *(pBuf+2);

is identical to

out_buf[2] = pBuf[0];
out_buf[3] = pBuf[1];
out_buf[4] = pBuf[2];

as far as the compiler is concerned since the index operator is just syntactic sugar. To prove this to yourself try the little bit of code below, either look as the value of 'c' in the debugger or output it.

int main()
{
	char const a[] = "ABC";
	size_t x = 1;
 
	char c = x[a]; // c == B
}

Open in new window

0
 

Author Comment

by:riceman0
ID: 24138567
Oops, my counterproposal doesn't work:

> void outmsg_cmd( char* pBuf )
> {
  out_buf[2] = *pBuf;
  out_buf[3] = *(pBuf+1);
  out_buf[4] = *(pBuf+2);

> }

If I call

outmsg_cmd("ABC");

I get the error
 function argument #1 of type 'flash unsigned char [4]' is incompatible with required parameter of type 'unsigned char *'
0
 

Author Comment

by:riceman0
ID: 24138600
evilrix: I agree strncpy is preferable to memcpy.  Also, yeah I'm not so rusty that I've forgotten those are identical.
0
 

Author Comment

by:riceman0
ID: 24138939

Ah, I think the "flash" error is because I'm programming a chip with harvard architecture (i.e., volatile and nonvolatile memory are not compatible), am trying this on a desktop app.
0
 

Author Closing Comment

by:riceman0
ID: 31569906
I think the problem is specific to my chip, and using a pointer parameter as first suggested by Zoppo would otherwise be the right way to go.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question