Solved

more elegant way to load characters?

Posted on 2009-04-14
8
376 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
  • 5
  • 2
8 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
Comment Utility
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
Comment Utility

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
Comment Utility
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
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:riceman0
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now