
newbie needs help to strings, strings, strings
Quote:
> Please help newbie to c.
> It seems that there are two way to declare strings in c
Not exactly.
Quote:
> char *mymsg;
You have declared a pointer to a data object of type char. This could
point to a single character or the first character in a string. At the
moment, though, it is not pointing to anything. You have not yet
defined a string.
Now you can use malloc() to allocate some storage to give the pointer
something to point at:
mymsg = malloc( 81 ); /* Allocates 81 bytes of storage for a 'string'
*/
You can now input data into the 'string'.
Quote:
> or
> char mymsg[ ];
This style does in fact create an empty string. Declaring:
char mymsg[81];
allocates as much storage for character data as the previous method.
Another possibility:
char *msg_p;
char msg[] = "Hello out there.";
msg_p = msg;
I have now set the pointer to point to the start of the string "Hello
out there."
Quote:
> when should I declare one way over the other? If I declare the first
> way, I do I reinitialize it to null?
You need to study and understand the similarities and differences
between arrays and pointers. They are two very different animals that,
under special circumstances, happen to look just alike. Sometimes you
can use an array name as if it were a pointer, and sometimes you can use
a pointer as if it were an array. Never forget, though, they are two
very different animals.
This is a common mistake:
char *stra;
char *strb = "Hello World.";
strcpy( stra, strb ); /* This is an error. */
The problem is that you have not allocated any storage for stra.
strcpy() will copy strb to whereever stra (uninitialized) just happens
to point to. On most modern systems you will get some kind of
protection fault when you try to write to memory that your application
does not 'own' (has no read/write rights to). Once in a blue moon the
garbage in stra will equate to an address inside your data space and you
will trash some of your data. On DOS systems you can do all kinds of
damage. Problems, I might add, that might not show up until you run
some other program.
Read the C-FAQ
www.eskimo.com/~scs/C-faq/top.html.
Look at the `Net resources available:
C Programming `Net Resources
http://www.cit.ac.nz/smac/cprogram/cstart.htm
http://www.angelfire.com/sc/electron/
http://www.cm.cf.ac.uk/Dave/C/CE.html
http://www.lysator.liu.se/c/
http://www-isis.ecs.soton.ac.uk/computing/c/notes/tao.html
http://www.cast.msstate.edu/~billy/c-prog.html
And get yourself a good book (The C Programing Langauge, by Kernigan and
--
Education is a companion which no misfortune can depress,
no crime can destroy, and no enemy can alienate...
at home a friend, abroad an introduction,
in solitude a solace, and in society and ornament.
Joseph Addison.
************************************************
You can't use void main() I have a patent on it!
************************************************
* Remove the _JUNK_ when replying to me. *
************************************************