Solved

char char char char char char char char...

Posted on 2002-05-29
6
311 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

867 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

19 Experts available now in Live!

Get 1:1 Help Now