Solved

char char char char char char char char...

Posted on 2002-05-29
6
309 Views
Last Modified: 2010-04-02
char *mychar;
char a = '1';
char b = '2';
char c = '3';
char d = '4';
I want to put a,b,c and d in the mychar. So, mychar should be equal "1234". I don't want it like
char *mychar="1234";
I want somehow put these 4 chars seperatly.
Thats not my homework :)
0
Comment
Question by:rv_man
6 Comments
 

Author Comment

by:rv_man
Comment Utility
Will I get booted for my stupid questions?
0
 
LVL 5

Expert Comment

by:Jan Louwerens
Comment Utility
char a = '1';
char b = '2';
char c = '3';
char d = '4';
char* mychar = new char[5];
mychar[0] = a;
mychar[1] = b;
mychar[2] = c;
mychar[3] = d;
mychar[4] = '\0';
...
delete [] mychar;
0
 
LVL 4

Accepted Solution

by:
ChenChen earned 50 total points
Comment Utility
Try this:
#include <stdio.h>

     char *mychar;
     char a='a';
     char b='b';
     char c='c';
     char d='d';

     mychar=(char *)malloc((4+1)*sizeof(char));
     sprintf(mychar,"%c%c%c%c",a,b,c,d);
     printf("MyChar is %s\n",mychar);
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84

Expert Comment

by:ozo
Comment Utility
char mychar[] = {a,b,c,d};

0
 
LVL 1

Expert Comment

by:jansuper
Comment Utility
That accepted answer actually has a bug in which the string is not properly ended with a NULL terminator
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
char szCombined[5];
char* p= szCombined;
*p++ = a;
*p++ = b;
*p++ = c;
*p++ = d;
*p= '\0;
printf("szCombined is %s\n", szCombined );
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now