Solved

char char char char char char char char...

Posted on 2002-05-29
6
316 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
[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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

617 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