Register - Login
Views: 99871807
Main - Memberlist - Active users - Calendar - Wiki - IRC Chat - Online users
Ranks - Rules/FAQ - Stats - Latest Posts - Color Chart - Smilies
05-04-22 07:16:09 PM
Jul - Computers and Technology - Learning C++. New poll - New thread - New reply
Next newer thread | Next older thread
Ninji

Birdo
Why did my user title say I'm a toaster anyway
Level: 88


Posts: 91/2014
EXP: 6639502
For next: 11162

Since: 07-26-07

Pronouns: he/him or they/them
From: Glasgow, Scotland

Since last post: 115 days
Last activity: 7 days

Posted on 10-04-07 02:31:36 PM Link | Quote
At the time of writing this post, I had..
Posts: 91 ~ Level: 12 ~ Exp: 7266

I'm learning C++, since I decided it might be a good idea to start learning something other than VB. However, there's one thing that's bothering me: converting char variables to char* or string. How?

____________________


NSMB Editor 2.2
making awesome SMB remakes since 16/7/07
what? you want a new version already? are you joking?
The "Official" NSMB Hacking Links Page
Nitro Explorer: universal NDS ROM file extracter/inserter


GuyPerfect
Catgirl
Level: 68


Posts: 69/1096
EXP: 2666045
For next: 62755

Since: 07-23-07


Since last post: 1.7 years
Last activity: 221 days

Posted on 10-04-07 04:51:25 PM; last edit by GuyPerfect on 10-04-07 04:54 PM Link | Quote
You can usually get away with doing something like this:
#include <stdio.h>

#include <string.h>

int main() {
// Two halves of sentence
char String1[50] = "This is a ";
char String2[25] = "complete sentence.";

// Concatenation: String1 + String2 -> String1
strcat(String1, String2);

// Output
printf("The new string: %s\n", String1);
return 0;
}



EDIT:
Of course, the program there will work in C++ and C alike. I recommend you do C for the kind of programming you're going to be doing.
Ninji

Birdo
Why did my user title say I'm a toaster anyway
Level: 88


Posts: 92/2014
EXP: 6639502
For next: 11162

Since: 07-26-07

Pronouns: he/him or they/them
From: Glasgow, Scotland

Since last post: 115 days
Last activity: 7 days

Posted on 10-04-07 05:43:06 PM Link | Quote
At the time of writing this post, I had..
Posts: 92 ~ Level: 12 ~ Exp: 7393

Originally posted by GuyPerfect
You can usually get away with doing something like this:
#include 

#include

int main() {
// Two halves of sentence
char String1[50] = "This is a ";
char String2[25] = "complete sentence.";

// Concatenation: String1 + String2 -> String1
strcat(String1, String2);

// Output
printf("The new string: %s\n", String1);
return 0;
}



EDIT:
Of course, the program there will work in C++ and C alike. I recommend you do C for the kind of programming you're going to be doing.

No, I'm talking about something different. It may be a little hard to explain, but here's some code that shows what I want to do:
char* string1 = "This string has an exclamation mark after it";
char exclamation = 33;
string1 += exclamation;


The VB equivalent:
Dim String1 As String: String1 = "This string has an exclamation mark after it"
Dim Exclamation As Byte: Exclamation = 33
String1 = String1 + Chr(Exclamation)


____________________


NSMB Editor 2.2
making awesome SMB remakes since 16/7/07
what? you want a new version already? are you joking?
The "Official" NSMB Hacking Links Page
Nitro Explorer: universal NDS ROM file extracter/inserter


GuyPerfect
Catgirl
Level: 68


Posts: 70/1096
EXP: 2666045
For next: 62755

Since: 07-23-07


Since last post: 1.7 years
Last activity: 221 days

Posted on 10-04-07 08:51:40 PM; last edit by GuyPerfect on 10-04-07 09:02 PM Link | Quote
Ah, okay.

Firstly, strings in C, when done with character arrays, need to have enough space allocated to contain all the data. This must be done manually. If you know how much space you'll be using ahead of time, you can declare the array as-is at the top of the function. Otherwise, you'll need to allocate the memory dynamically (malloc() and free()) and write the data into there.
  • You really can't go wrong with declaring some 256 bytes in an array of unsigned char, so that's certainly a viable option.
  • You can assign the applicable ASCII character code to a char variable by specifying it in single quotes.
  • Strings are null-terminated (character 0), so keep that in mind.
  • Characters can be inserted into strings by simply setting the value of the desired element in the string array.
#include <stdio.h>

#include <string.h>

int main() {
// When this array is declared, String1 will be of type unsigned char*
unsigned char String1[256] = "This string has an exclamation mark after it";
unsigned char Exclamation = '!';

// A cheater way to do it
strncat(String1, &Exclamation, 1);

// Output
printf("String1 = \"%s\"\n", String1);
return 0;
}
I had a notice here about initializing all of a char array's elements to 0 before using it, but strncat() takes care of the null terminator for you.



EDIT:
I forgot about strncat(). I've edited my post. Enjoy.
Rena
I had one (1) message in Discord deleted and proceeded to make a huge, huge mess about how it was a violation of free speech and how moderators are supposed to be spam janitors and nobody should have the right to tell me not to talk about school shootings
Level: 135


Posts: 1237/5390
EXP: 29079617
For next: 255388

Since: 07-22-07

Pronouns: he/him/whatever
From: RSP Segment 6

Since last post: 343 days
Last activity: 343 days

Posted on 10-05-07 03:56:46 AM Link | Quote
JL2 - Post #1237 - 10-04-07 10:56:46 PM
Day 74, rank 7; Level 39 (15.3%)
5266/34324 (375713/404771)
GPP: 455; GT: 34.300
Originally posted by GuyPerfect
You really can't go wrong with declaring some 256 bytes in an array of unsigned char, so that's certainly a viable option.
Of course, if your string is less than 256 bytes, this is wasteful, and if the possibility exists of one more than 256 bytes being stored there, you introduce potential security holes, or at least crashes. C doesn't do any bounds checking, so you can go right on ahead and do something stupid like this:

char foo[256];
foo[300] = 4;


or even:
char* foo = 0;
foo[0] = 7;

and watch the program crash and burn.

____________________
GuyPerfect
Catgirl
Level: 68


Posts: 74/1096
EXP: 2666045
For next: 62755

Since: 07-23-07


Since last post: 1.7 years
Last activity: 221 days

Posted on 10-05-07 02:26:42 PM Link | Quote
Originally posted by HyperHacker
Of course, if your string is less than 256 bytes, this is wasteful [...]

I'm sure most modern computers can spare a hundred extra bytes for a string.

Originally posted by HyperHacker
C doesn't do any bounds checking, so you can go right on ahead and do something stupid like [...]

Well, that's... rudimentary, to say the least. Treeki, what's your level of proficiency so far?
Ninji

Birdo
Why did my user title say I'm a toaster anyway
Level: 88


Posts: 94/2014
EXP: 6639502
For next: 11162

Since: 07-26-07

Pronouns: he/him or they/them
From: Glasgow, Scotland

Since last post: 115 days
Last activity: 7 days

Posted on 10-05-07 03:30:28 PM Link | Quote
At the time of writing this post, I had..
Posts: 94 ~ Level: 12 ~ Exp: 7684

Originally posted by GuyPerfect
Originally posted by HyperHacker
Of course, if your string is less than 256 bytes, this is wasteful [...]

I'm sure most modern computers can spare a hundred extra bytes for a string.

Originally posted by HyperHacker
C doesn't do any bounds checking, so you can go right on ahead and do something stupid like [...]

Well, that's... rudimentary, to say the least. Treeki, what's your level of proficiency so far?

This small calculator I wrote probably describes my level quite well:
#include

int main() {
bool incalc = true;
while (incalc) {
puts("Enter a number and press Enter. [32767 to exit]");
int num1 = 0;
scanf("%d", &num1);
if (num1 == 32767) break;
puts("Now select the type of calculation you would like to do: (+-*/)");
char calctype = 0;
while (1) {
char calctypet = 0;
calctypet = (int)getchar();
if (calctypet == 10) {
if ((calctype == '+')||(calctype == '-')||(calctype == '*')||(calctype == '/')) break;
} else calctype = calctypet;
}
puts("Ok, now type the second number you want to use");
int num2 = 0;
scanf("%d", &num2);
if (calctype == '+') printf("%d + %d = %d ", num1, num2, num1 + num2);
if (calctype == '-') printf("%d - %d = %d ", num1, num2, num1 - num2);
if (calctype == '*') printf("%d * %d = %d ", num1, num2, num1 * num2);
if (calctype == '/') printf("%d / %d = %d ", num1, num2, num1 / num2);
puts(" - let's make another calculation!");
}
}


____________________


NSMB Editor 2.2
making awesome SMB remakes since 16/7/07
what? you want a new version already? are you joking?
The "Official" NSMB Hacking Links Page
Nitro Explorer: universal NDS ROM file extracter/inserter


GuyPerfect
Catgirl
Level: 68


Posts: 75/1096
EXP: 2666045
For next: 62755

Since: 07-23-07


Since last post: 1.7 years
Last activity: 221 days

Posted on 10-05-07 09:04:23 PM Link | Quote
Okay, then. Keep in mind what HyperHacker said: arrays in C are not bounds checked, which means ArrVar[-1] and ArrVar[UpperBound + 3] are both allowed.

The code that you used is compatible with C for the most part, but C++ allows a few things that C does not.
  • The bool data type is not native to C. You can use it, however, by #defining bool as char, false as 0 and true as 1.
  • C89, which is the version MSVC++ compiles .c files with, requires variables to be declared in the function before any statements are made. I recommend doing this anyway, as it keeps all the declarations in one place so people don't go "Wait, where did that variable come from?"
Ninji

Birdo
Why did my user title say I'm a toaster anyway
Level: 88


Posts: 95/2014
EXP: 6639502
For next: 11162

Since: 07-26-07

Pronouns: he/him or they/them
From: Glasgow, Scotland

Since last post: 115 days
Last activity: 7 days

Posted on 10-05-07 10:06:40 PM Link | Quote
At the time of writing this post, I had..
Posts: 95 ~ Level: 12 ~ Exp: 7822

Originally posted by GuyPerfect
Okay, then. Keep in mind what HyperHacker said: arrays in C are not bounds checked, which means ArrVar[-1] and ArrVar[UpperBound + 3] are both allowed.

The code that you used is compatible with C for the most part, but C++ allows a few things that C does not.
  • The bool data type is not native to C. You can use it, however, by #defining bool as char, false as 0 and true as 1.
  • C89, which is the version MSVC++ compiles .c files with, requires variables to be declared in the function before any statements are made. I recommend doing this anyway, as it keeps all the declarations in one place so people don't go "Wait, where did that variable come from?"


Currently, I'm using the G++ compiler under Cygwin, so do I need to worry about these?

____________________


NSMB Editor 2.2
making awesome SMB remakes since 16/7/07
what? you want a new version already? are you joking?
The "Official" NSMB Hacking Links Page
Nitro Explorer: universal NDS ROM file extracter/inserter


GuyPerfect
Catgirl
Level: 68


Posts: 76/1096
EXP: 2666045
For next: 62755

Since: 07-23-07


Since last post: 1.7 years
Last activity: 221 days

Posted on 10-05-07 10:21:50 PM Link | Quote
Depends on if you want your code to compile in C.
Next newer thread | Next older thread
Jul - Computers and Technology - Learning C++. New poll - New thread - New reply


Rusted Logic

Acmlmboard - commit 47be4dc [2021-08-23]
©2000-2022 Acmlm, Xkeeper, Kaito Sinclaire, et al.

29 database queries, 7 query cache hits.
Query execution time: 0.086051 seconds
Script execution time: 0.025556 seconds
Total render time: 0.111607 seconds