Advertisement

Angry question of death!

Started by June 17, 2002 05:11 AM
8 comments, last by the Chef 22 years, 6 months ago
Now im getting real pissed!! Why the *PIP* does this code not work?!?!

#include <iostream.h>

char *List[256];

int main() {
    List[0] = "Hello there!";          // None of theese
    strcpy(List[0], "Hello there!");   // two works! :/
    cout<< List[0];
return 0;
}
 
The cout-statement gives no output, so it seems like the "Hello there" string is not copied. Ive tried both strcpy() and my own String-class which supports declarations like the first one. What is wrong!? Thanks for all your help! [edited by - the Chef on June 17, 2002 6:13:30 AM]
No way! I will never write a profile signature! :p
Okay couple things you gotta learn. C/C++ is good/bad in this. If you declare an array named foo of ints:
int foo[200];
Foo is now a pointer. When you use the [] operator it will dereference foo for you.

When you declared your char *List[256] you created an array (char *) with 256 elements. =P

So to make it simple, you want:
char List[256];
strcpy(List, "Hello there!");

VERY MAJOR WARNING! When you create the array its contents are undetermined! You should always (i think) "zeroize" the memory before using it! strcpy is nice and will append a \0 (null character) for you, but you shouldn't come to expect this. You can clear out the memory by using:
memset(List, 0, 256);

Sorry if it seems brief. 5:30 AM and quite sick Sort of an opininated post =P

BTW, its considered pretty weird to combine strcpy() and cout. strcpy() is a C function while cout is C++. Its not technically important but other people can be totally anal about it (read: Teachers!).

Edit: Yea after reading fruny's post about the C/C++ thing it really would be a good idea to learn 1 first and then the other. Otherwise you will probably have a good deal mixed up.


[edited by - Elixir on June 17, 2002 6:41:14 AM]
Advertisement
First thing, stop using <iostream.h> and use <iostream> instead. Don''t expect much help otherwise.

<monotone>The pre-standard iostream and fstream classes have been deprecated. Support for them has been discontinued. Please upgrade to a standard-compliant library. We suggest the use of the new and improved std::iostream and std::fstream found in <iostream> and <fstream> Use only approved manuals and headers. Failure to comply is a breach of contract and will void your warranty. Thank you for using ISO-C++.</monotone>

Secondly, use the C++ std::string class, its more convenient.

Thirdly, if you really want to use strcpy, make sure your memory is allocated first.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by Elixir
BTW, its considered pretty weird to combine strcpy() and cout. strcpy() is a C function while cout is C++. Its not technically important but other people can be totally anal about it (read: Teachers!).


Just tell them the C library is officially a part of the C++ library (C++ ISO Standard, Section 17, clause 1, p311 if you want to be anal too )

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 17, 2002 6:40:34 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I have not tried using the std::string class, maby I should but anyway.

I DO really want the List as char *List[256], an array with 256 char* elements. I want to write into a certain pointer when I accses List[n].

So when I write

List[0] = "Hello there";

I want the pointer at List[0] contain "Hello there", but as you see - i get no output! What to do?
Thanks anyway for the prev posts!
No way! I will never write a profile signature! :p
The problem is that you''re trying to assign a character array ("Hello there") to a single character variable (List[0]), and that''s just not possible.

It sounds like there might be two options, depending upon what you''re ultimately trying to do. The previous posts in this topic show how you can assign the string to your character array List. Note the lack of an array index, which means we''re not dereferencing to get an individual array element, rather using the pointer the points to the start of the whole array.

The second possibility is that you''re trying to create an array of strings, which would effectively be a two-dimensional character array. One way to do that would be:

char List[5][256];
List[0] = "Hello there!";

That would assign your string to the first element of the array, and you have space for four additional strings. It''s a very simple example and there are probably a dozen other ways you copuld accomplish something similar.

--Roderick Smith
--Roderick Smith
Advertisement
You cannot assign C-strings with the = operator, hence the reason that List[0] = "Hello there!"; shouldn''t work. If you want to assign a C-string you should use strcpy();


I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
you are mostly correct in your code. however you need to do this:

List[0] = new char[50];strcpy(List[0], "hello there!");cout << List[0];//release the memory you allocated for List[0]delete[] List[0];//if you allocated space for all the 256 pointers do this insteadfor (int i = 0; i < 256; i++)    delete[] List;  


the key is "new char[50]". in your code you have sucessfully created 256 char * variables. however, you have not allocated any space for the character arrays that any of those dealios point to. as far as i know you cannot strcpy into an unallocated array.

remember to delete[] all arrays that you allocate with "new" at the end of your program or you'll get memory leaks.

-me

[edited by - Palidine on June 17, 2002 1:57:11 PM]
Er, since I don''t think it''s been mentioned yet...
using namespace std;

------------
aud.vze.com - The Audacious Engine <-- It''s not much, yet. But it''s mine... my own... my preciousssss...
MSN: nmaster42@hotmail.com, AIM: LockePick42, ICQ: 74128155
_______________________________________Pixelante Game Studios - Fowl Language
RdrckSmith, Invader X : He''s assigning a constant string litteral to a char*. That''s OK.

LockePick: No std namespace with the old iostreams.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement