Welcome! Log In Create A New Profile

Advanced

C Programming, Combining chars

Posted by Matando 
C Programming, Combining chars
February 11, 2010 10:44PM
Okay, Although I've been learning c for years(lol), I still don't know how to do this.

What I'm trying to do is take say char1, char2, and char3 and make them one big char string, one piece after the other.

Basically I want the same effect as the windows command " copy /b file1 + file2 + file3 file" in C.

I just have no idea how to do it. All I need is a simple point in the right direction :)
Re: C Programming, Combining chars
February 11, 2010 11:06PM
You can do it in the C++ standard library like so.

#include(iostream)
#include(string)

std::string basestring = "";

char1 = 'H';
char2 = 'i';

basestring += char1;
basestring += char2;

int main(){

std::cout << basestring;
int pause;
std::cin >> pause;

return 0;

}

For a strictly C solution consider a linked list created via structs (I actually dont 100% know if that's possible).

For a strictly C solution see Daid's post below.



Edited 5 time(s). Last edit at 02/12/2010 10:48PM by Arikado.
Re: C Programming, Combining chars
February 11, 2010 11:09PM
I think (but I'm not sure) it'd be something like the following:

string sChar = char1 char2 char3;

I'll try this and see if it works, and post back. Bear in mind I'm quite a n00b at coding.


EDIT: Actually, this probably wouldn't work, I am a complete n00b after all.

EDIT: Yeah it doesn't work. Never mind.



Edited 2 time(s). Last edit at 02/11/2010 11:27PM by SifJar.
Re: C Programming, Combining chars
February 12, 2010 12:38AM
< include string.h >
char *string;
string=strcat( str1, str2 );
string=strcat(string, str3);
Re: C Programming, Combining chars
February 12, 2010 11:00AM
@chris: You should use malloc() or change char* to char array (so you have a definite length) to avoid the risk of string being randomly overwritten.

You can also do:
char char1= "H";
char char2="i"
char baseString[MAX_PATH];
char baseString[0]=char1;
char baseString[1]=char2;
char baseString[2]="\n";

I'm not 100 % sure the final line is needed.

EDIT: Or you could use sprintf().



Edited 1 time(s). Last edit at 02/12/2010 11:01AM by profetylen.
Re: C Programming, Combining chars
February 12, 2010 03:14PM
This topic makes me sad. There is not a SINGLE right post here.

Arikado has a good C++ solution, but his comment about linked lists after that ruined it.

SifJar fails at basic C syntax. 'string' is not a C type. Nor does putting some variable after each other make them magically work.

chris uses the default C library, but wrong. And doesn't mention a lot of important things. Like side effects of those functions.

profetylen is heading the right way. But also fails at the C syntax. Chars are single quotes, not double. And you lack the NUL terminator.


First, If you are spending years learning C already. And you cannot do this... then I wonder what kind of C you have been learning. Because this is BASIC stuff.
Second, C does not know 'strings' What people call strings in C are really arrays of chars. And you should think of them as arrays of chars. There are no safeguards, no protections. When someone says "string" in C context, they mean "array of chars that end with a char with the value NUL"

Without any pointer magic, standard functions. What you are trying to do is:
#include <stdio.h>

int main()
{
  char c1 = 'c';
  char c2 = 'd';
  char c3 = 'c';
  char resultBuffer[32];
  resultBuffer[0] = c1;
  resultBuffer[1] = c2;
  resultBuffer[2] = c3;
  resultBuffer[3] = 0; //Don't forget to end the new 'string' with a NUL char.

  printf("%s\n", resultBuffer);
}



Edited 1 time(s). Last edit at 02/12/2010 03:14PM by Daid.
Re: C Programming, Combining chars
February 12, 2010 03:32PM
Quote
Daid
SifJar fails at basic C syntax. 'string' is not a C type. Nor does putting some variable after each other make them magically work.

Apologies, like I said I am a coding n00b.
Re: C Programming, Combining chars
February 13, 2010 02:59PM
Quote
Daid
profetylen is heading the right way. But also fails at the C syntax. Chars are single quotes, not double. And you lack the NUL terminator.
Woops, by "\n" I meant "\0" or '\0' actually. Thanks for telling me that chars are single quotes! However, what's the difference between "H" and 'H' then? My guess is that "H" includes the null terminator. Am I right?
Re: C Programming, Combining chars
February 14, 2010 02:24AM
Wow... Thanks Daid. It's not really that I've been learning for years, It's that I havent written enough programs to have gotten really good at it yea i guess? Or I'm just horrible...(No comments on that please, lol)

Quote
SifJar
Quote
Daid
SifJar fails at basic C syntax. 'string' is not a C type. Nor does putting some variable after each other make them magically work.

Apologies, like I said I am a coding n00b.

apparently me too. :p
Re: C Programming, Combining chars
February 16, 2010 11:03AM
Quote
profetylen
Quote
Daid
profetylen is heading the right way. But also fails at the C syntax. Chars are single quotes, not double. And you lack the NUL terminator.
Woops, by "\n" I meant "\0" or '\0' actually. Thanks for telling me that chars are single quotes! However, what's the difference between "H" and 'H' then? My guess is that "H" includes the null terminator. Am I right?
'H' is of the type [char] and has the value 72
while
"H" is of the type [char*] which is a pointer to a 'string' it points to the memory location where there is a [char] stored with the value 72 followed by a [char] with the value 0.

[char] is a single character. (Or the value -128 to 127)
while
[char*] is a pointer to a [char]. And by conversion the [char]s starting at that location are what we use as 'string' until you encounter 0 [char] (but that's not forced)


char c = 'H'; //This make a [char] called 'c' with the value 72.
char s[] = "H"; //This makes a [char*] called 's' pointing somewhere in memory where a 72 followed by a 0 are stored.

There are many standard functions that work on [char*] and assume that the [char*] contains a valid 'string'. A valid string is a number of [chars] with values ended with a [char] containing 0. The basic ones are listed here: [cplusplus.com]

As this is C and it does not really know strings you can do the following:
#include 

int main()
{
  char resultBuffer[4]; //Make a 'string' that contains a max of 4 [char]s, including the 0
  strcpy(resultBuffer, "1234567890");
  printf("%s\n", resultBuffer);
}
This code will compile, and it will run. And there is a chance that it won't even crash. But it is wrong, as it writes 11 chars into a buffer only holding 4 chars.
Re: C Programming, Combining chars
February 16, 2010 11:08AM
@Daid: Thanks for a good and detailed exlpanation! It is very much apprecieated!
Sorry, only registered users may post in this forum.

Click here to login