Solved

more elegant way to load characters?

Posted on 2009-04-14
8
380 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

763 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