Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Char PTR to char[]

Status
Not open for further replies.

bont

Programmer
Sep 7, 2000
200
US
Hello all, could someone please help?

Could someone please explain the differences between the following declarations?

CString MySring;

char * MyString;

char MyString[];

I am about write a program utilizing the 2 later, and I need to know their compatability.

Thanxs
 
ok...

CString is an MFC class that controls allocation of memory for the strings on its own

as for the later 2

char* MyString; is a pointer to a character array
char MyString[]; // this is usually set to a string
i.e. MyString[] = "SOME STRING"; this allocates enough space for "SOME STRING" and by saying MyString in the second case, it too behaves like char* MyString.

If you could explain the usage of how you will be implimenting these it could be helpful

Matt

 
Well, it is hard to say at the moment. Generally, I guess I need to know how to change from one to the other and vise versa.

I plan to do a lot of manipulation and comparison using the string.h library, which requires the use of char [] declaration. I think this works for one direction:

char *MyPTRSTR = "123";
char MySTR[256];

strcpy(MySTR,MyPTRSTR);

But, I do not know how to go the other directrion, * to array, and then how to print the * using cout (or do I always have to use printf, which of course is no problem)

Thanx
 
hi there,

I hate to say this, but you really should learn the absolute basics of a language before you try to program in it. If you don't know the differences in C++ between an array, a pointer and a class (object) you really should work through a beginner's text on the subject. Ignoring CString,

char str[10];

declares an array of chars (typically single bytes). It allocates the storage for this array on the stack if defined within a function. The stack being the working storage for each function in your program, where local variables and parameters passed from a calling routine are stored.

You can initialise this array with

char str[10] = "SomeData.";

for example, although the dimension (10) is not actually needed in this case. The compiler is smart enough to figure out that "SomeData." is nine characters, plus an extra one for a null byte (how C++ usually delimits strings - but you knew that, I'm sure).

char* cp.

Defines a pointer to a char string. It is just 4 bytes (in Windows) of storage that ccontain the storage address of some string (once it has been set). So I can say

cp = str;

(or I can allocate some heap storage dynamically for my pointer to point to

cp = new char[10];

And if I do that I need to free the storage later with delete [] cp;)

Now cp points to the start of str (or some storage from the heap), and so now

cout << *cp;

will write out the first byte of str (&quot;S&quot;); I could also say

cout << cp[0];

just like I could say

cout << str[0];

since there's an equivalence in C++ between pointers and arrays; but they are not the same. In fact I can also say

cout << *str;

I can also say

cout << cp;

or

cout << str;

both of which would write out the entire string. cout is a class with functions for outputting chars (as in the previous four cout examples) or zero delimited char arrays (as in the last two). The C++ compiler determines the type of the parameter and calls the appropriate function (the function in this case is the overloaded operator &quot;<<&quot;).
 
Thanks for your message. I am quite familiar with linear C++, and formerly used to use C. You would be amazed how hard it is to find the answer to my question:

char *MyPTRSTR = &quot;123&quot;; // character string pointer
char MySTR[256]; // character array

strcpy(MySTR,MyPTRSTR); // copy PTR to ARR
MySTR[0] = 'x'; // substitute x for 1 in ARR

MyPTRSTR = MySTR; // copy ARR to PTR

printf(&quot;MyPTRSTR NEW:%s\n&quot;, MyPTRSTR); // Print out PTR (can not use cout if there is no '\0' character)

I use 7 languages at work, and I am back and forth between them. I use this site as a reference, and you wouldn't believe how hard it is to find an answer as simple as this, without someone being smart.

Thanx :)

 
Hi,

sorry if I upset you; however since arrays and pointers are an extremely fundamental part of C and C++, then in my opinion someone familiar with these languages must understand them. Anyway, what's your question? You post a simple piece of C code, which would work quite happily as C++ code with cout, except you don't seem to think so.

Here it is as C++:

char *MyPTRSTR = &quot;123&quot;; // character string pointer
char MySTR[256]; // character array
strcpy(MySTR,MyPTRSTR); // copy PTR to ARR
MySTR[0] = 'x'; // substitute x for 1 in ARR
MyPTRSTR = MySTR;
cout << &quot;MyPTRSTR NEW:&quot; << MyPTRSTR;

Try it and you'll see it works. It writes out &quot;MyPTRSTR NEW:x23&quot; as one would expect. Did you actually try it? There is a final zero in MySTR since strcpy() puts it there. The C(++) compiler puts it at the end of &quot;123&quot;; that's a fairly basic part of C(++) too. And strcpy() copies it. Just read the description of strcpy() in any C(++) reference. Therefore the first four bytes of MySTR contain 1,2,3 and a null. The rest is garbage. For C(++) that's a three byte string (plus delimiter).

Since after MyPTRSTR = MySTR; MyPTRSTR points to your MySTR array there's a zero at the end there too, since it's the same thing. So cout works. printf() would be just as unhappy with an unlimited string for that matter. And I'm still wondering what the question is...
 
Thank you,

I replied to have the response in the DB for future reference. So if I was to just create a char *:

char *NewCP = &quot;123&quot;;
then:
cout << NewCP

would error, as there currently is no endSTR char. As you pointed out the strcpy() function has placed the endSTR character.

Thank you for your help. I just try to make things as clear, and there, so that others may follow, especially beginners.
 
Hi,

>>char *NewCP = &quot;123&quot;;
>>then:
>>cout << NewCP
>>would error, as there currently is no endSTR char.

I'm sorry, but that's wrong too. C(++) always terminates a standard string with a null. The string constant is four bytes including the null at the end. strcpy() doesn't &quot;put it there&quot;, it copies the null byte that must already be there (or strcpy() wouldn't know when the string ended, would it?)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top