Solved

more elegant way to load characters?

Posted on 2009-04-14
8
381 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
Technology Partners: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Header of docx file 17 145
Computer slow / BSOD 10 79
Please explain C language error codes ? 5 52
C++ Code Issue 4 48
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 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