Archived

This topic is now archived and is closed to further replies.

Tornado

Weird problem with char *

Recommended Posts

I''m trying to write a char* letter-by-letter. So instead of: char *text = "Some text"; I want to do: char *text; text[0] = ''S''; text[1] = ''o''; ... But it just doesn''t work! My app closes when I try to. Here is it exactly: char *text; text[0] = ''O''; // some char text[1] = ''\0''; // null char, do i really need this? The app closes at: text[0] = ''O''; I''m probably missing something simple, but I just can''t find it! Thanks for your help - Goblineye Entertainment The road to success is always under construction

Share this post


Link to post
Share on other sites
your pointer,text, dont point to anything, so where do you wanna write your text in?!

ok, really, your pointer has a value in, for example 0x34C3AB23, or something like this, but this part of memory is not resserved for your programm, so you dont have access to it, and, at the moment you wanna write in there, your programm crashes.

you have to allocate memory for your own use:

char* text=(char*)malloc(2/*2 bytes wide free memory to use for you*/);

then you have acces to text[0] and text[1], but just for this two

we wanna play, not watch the pictures

Share this post


Link to post
Share on other sites
Okay, lets review:
char *text;
This is a pointer to a char. Right now it points at nothing.
So if we say text[0] = 0 then were saying make what were pointing at (nothing) become 0.
Your computer may not like trying to make nothing = 0.

So, lets fix this by making it point at something real.
char text[10];
now it points at some memory 10 char''s long Problem is fixed.
You could also try:
char *text = new char[10];
text[0] = 0;
delete [] text;

Now I''m sorry if you already knew this stuff, I''m just feeling a little sarcastic today.

Share this post


Link to post
Share on other sites
What you gotta remember is that a char* variable is just a pointer to a memory address. You need to point it to valid memory, or initialize memory yourself (with new). So, basically you will need to know how big you need your array of chars to be and do it like so:

char *text;

text = new char[10];

text[0] = 'S';
text[1] = 'o';

- Houdini

- Bah, I hate it when people beat me to the punch


Edited by - Houdini on October 3, 2000 3:03:03 PM

Share this post


Link to post
Share on other sites
char *text = "Some text";

is a shortcut that allocates an array of characters and points text to the zero''th element of the array. When you write

char *text;

all you''re doing is making a pointer. It doesn''t point anywhere and no memory has been allocated. You have to either write

char text[10];
text[0] = ''S'';
etc.

or go about it the long way

char *text;
text = (char *)malloc(10*sizeof(char));
text[0] = ''S'';
etc.

I''d suggest you get a good book on basic C programming. I learned from ''Practical C Programming'' from O''Reilly & Associates. It''s a little dated but is pretty thorough.

Share this post


Link to post
Share on other sites
Actually I do
I want to use a pointer so I won''t have to limit the size of the array.
Nevermind, I think I found the problem now
Btw, your fast! I just posted 5 minutes ago!
Thanks

Share this post


Link to post
Share on other sites
Whoa!

I''ve been beaten to the post before but never by three people at once.

Hot topic! (Aren''t the easy ones fun to answer?)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You should allocate data to your pointer before you start writing to the location it points to. Rather than just declaring a char* and putting memory at that location, you should either declare a char stringname[arraysize] and then use the stringname as your pointer-name; or alternatively you can create your char*, then based on circumstances you can allocate memory to it with malloc() (or new [] if you''re using C++). Just be sure to release the memory you''re pointing to when you''re done with it by calling free() (or delete [] if you used new [] from above).

This is kinda compiler-dependant, but in most cases (AFAIK) you are expected to have a block of memory associated to a pointer, largely because of how the compiler handles the initialization of your pointers. Some compilers point uninitialized pointers to NULL, and writing to this area can be devastating. Other compilers give arbitrary values to uninitialized data, like "cdcdcdcd". In debug mode especially, compilers will not let you write outside the little sandbox that it gives you, and often trying to write to an address pointed to by an uninitialized pointer will do this (go outside the bounds), even if the value is pure garbage and wasn''t set by the compiler.

Even if your compiler allows you to cast a pointer, then write to it without giving it a block of memory (ahh!), you should be sure to give it a block of memory, because writing to random places on your drive is a Very Bad Thing, and should be avoided at all costs, especially if you want this to run on someone else''s machine. (While you''re at it, be sure that you never write outside these bounds that you give when creating your memory block... memory leaks are bad things, too... though many of us have been susceptible to causing them)

I hope you don''t think I went on for too long about this, but I wanted to make sure it was well explained... pointers should be used with caution, and always used as if you''re really just writing to physical data using an alternative representation. Otherwise you''re likely to shoot yourself in the foot.

Share this post


Link to post
Share on other sites
i was the last poster, sorry about the anon name...

wow, a lot of people beat me to replying, i probably shouldn''t be taking so long with writing my response. oops.

"Man is the only animal that laughs and weeps; for he is the only animal that is struck with the difference between what things are and what they ought to be."
        --William Hazlitt

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i was the last poster, sorry about the anon name...

wow, a lot of people beat me to replying, i probably shouldn''t be taking so long with writing my response. oops.

"Man is the only animal that laughs and weeps; for he is the only animal that is struck with the difference between what things are and what they ought to be."
        --William Hazlitt

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster

i was the last poster, sorry about the anon name...

wow, a lot of people beat me to replying, i probably shouldn''t be taking so long with writing my response. oops.




Dunno why, but I just found that damn funny

- Houdini

Share this post


Link to post
Share on other sites