Solved

char char char char char char char char...

Posted on 2002-05-29
6
312 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
ID: 7043356
Will I get booted for my stupid questions?
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 7043366
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
ID: 7043379
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 84

Expert Comment

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

0
 
LVL 1

Expert Comment

by:Jan Louwerens
ID: 7043562
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
ID: 7043891
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 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