Advertisement

Weird problem with char *

Started by October 03, 2000 01:47 PM
9 comments, last by Tornado 24 years, 3 months ago
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
Goblineye EntertainmentThe road to success is always under construction
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

If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Advertisement
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.
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
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
- Houdini
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.
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
Goblineye EntertainmentThe road to success is always under construction
Advertisement
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?)
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.
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
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."
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

This topic is closed to new replies.

Advertisement