Solved

char char char char char char char char...

Posted on 2002-05-29
6
315 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
Industry Leaders: 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!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is sub-make ? 2 102
Precision Problem in C++ 7 65
Android development question 2 107
I could not set window to top 4 60
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…
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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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