|
-
Sep 8th, 2000, 07:45 PM
#1
what does this mean?
Code:
using namespace std;
and I don't have a referance right now, but which one is declaring a string?
Code:
char* cString = "hello!";
//or
char *cString = "hello!";
I Know what pointers are, I just forgot which was which....
and when you use an array of chars, like this
Code:
chr cString[6] = "hello!";
//it can only hold a max of 7
chr cString[] = "hello!";
//I didnt specify what size the array was
//but it can only hold 7 now because thats what I
//assigned to it
//if i use the method above(char * .... etc)
//will I be able to change the length(make it longer)
//after the first time I change it?
//and finally
//how do I redim an array?
Thanks.......
-
Sep 9th, 2000, 11:33 AM
#2
Monday Morning Lunatic
In C++, a namespace is simply a logical area where data and functions are stored:
Code:
namespace Mike {
void function() {
}
int another(int a) {
return a*2;
}
};
To use these, you reference them with the Scope Operator (: :
Code:
Mike::function();
cout << Mike::another(50) << endl;
Namespaces are usually used to ensure that no function names are used which are already taken.
--------------------------------------------------------
Both of these declare a string. (hehe). Technically, though, you should use:
Code:
char *pcString = "Hello!";
...because the * operator is being applied to the variable.
--------------------------------------------------------
To redimension an array, you have to make a new buffer that is longer, copy the original contents into it, and set the pointer. BEWARE!!! You cannot do this with a statically-initialised variable:
Code:
char *pcString = "Hello!"; // cannot be redimensioned
To redimension, you must do something like this, which concatenates pcHello and pcWorld:
Code:
char *pcHello = "Hello ";
char *pcWorld = "World!";
char *pcString;
pcString = new char[strlen(pcHello) + strlen(pcWorld) + 1]; // The +1 is important
strncpy(pcString, pcHello, strlen(pcHello));
strncpy(pcString + strlen(pcHello), pcWorld, strlen(pcWorld));
pcString[strlen(strlen(pcHello) + strlen(pcWorld) + 1] = 0; // Terminator
cout << pcString << endl;
delete pcString;
pcString now contains "Hello World!". That last strncpy demonstrates one of the useful features of pointers - you can add to them. In this case, all it does is to reference the second half of pcString. The final line frees the memory allocated by new.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 9th, 2000, 11:45 AM
#3
Ok, but I don't think you answered one of my questions...
when you do
Code:
char *mystring = "whatever";
can it be changed to
Code:
mystring = "longer text than before"
?
-
Sep 9th, 2000, 11:50 AM
#4
Monday Morning Lunatic
I did answer that one. You can't do it that way. Once you initialise it like that, you can't change it around. Anyway, you can use the STL string class:
Code:
#include <iostream>
#include <string>
using namespace std;
void main() {
string sMyString = "This is a string";
sMyString = "A different string"; // Allowed
sMyString = sMyString + " and another one";
cout << sMyString << endl;
}
This will print:
Code:
A different string and another one
Oh yeah, I forgot in my last post...namespaces. To avoid having to reference them explicitly, there is the using namespace Mike; code. Otherwise, the code about would be:
Code:
#include <iostream>
#include <string>
void main() {
stl::string sMyString = "This is a string";
sMyString = "A different string"; // Allowed
sMyString = sMyString + " and another one";
stl::cout << sMyString << stl::endl;
}
I think you can see the disadvantage here .
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 9th, 2000, 12:42 PM
#5
Frenzied Member
As far as I know, namespaces are useful when you have more than one person writing code for a project. If each person uses their own namespace then there can be no variable naming conflicts. It's a way of making your own scope - that's why you use the scope resolution operator to use identifiers in other namespaces.
Parksie, what's the function template for the strncpy function? I'm pretty sure I followed the code there, but I'd like to make sure.
Also,
Code:
pcString[strlen(strlen(pcHello) + strlen(pcWorld) + 1] = 0; // Terminator
Aren't you meant to assign the character '\0' as a string terminator for platform independance or something? (Been a while since I looked in my big book o' C)
Harry.
"From one thing, know ten thousand things."
-
Sep 9th, 2000, 12:50 PM
#6
Monday Morning Lunatic
I think it's
Code:
char* strncpy(char *dest, const char *source, size_t length);
It copies length characters from source to dest, without adding a terminator.
char is an integral type, so these are equivalent:
Code:
char c;
c = '\0';
c = 0;
It's not platform independence, just convention that strings end with NULL. Although in DOS, they used to end with a '$' symbol (don't ask why - this is still needed in asm).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 9th, 2000, 12:57 PM
#7
Frenzied Member
Oh right, so the escape character '\0' is just for convenience when writing string literals in your code then? I thought that the string terminator for different platforms could be different.
'Integral type' - not a term I've come across before. I knew that char was just an integer data type really and you could assign ascii codes to it, but I didn't realise there was some terminology involved. I'll go look it up...
Cheers
Harry.
"From one thing, know ten thousand things."
-
Sep 9th, 2000, 01:05 PM
#8
Monday Morning Lunatic
"Integral" just means that it stores integers... .
As for the string terminators, it's convention in C, because since a null character ('\0') cannot be displayed, it is used as the terminator. It also allows things like this:
Code:
void myprint(char *pcString) {
char *p = pcString;
while(*(p++))
putc(*p);
}
In Basic, I think strings are stored as a count followed by the string. (BBC BASIC terminated strings with the RETURN character (Char 10?).
Back to C...all implementations of C use the null-terminator format.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|