newbie needs help to strings, strings, strings 
Author Message
 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

> char *mymsg;

This is not a string.  This is simply a pointer to char.  It may point to a
string, or it may point to a single character, but until you specifically
assign something to it, it points to nothing.

Quote:
> or
> char mymsg[ ];

This declares mymsg as an incomplete array type, since the array size is
not specified.  It must be declared as a complete type (i.e., have the size
specified) before it may be assigned to.

Quote:

> when should I declare one way over the other?  If I declare the first
> way, I do I reinitialize it to null?

Use the first case when you want to allocate the array dynamically, like so:

   #include <stdlib.h> /* required for malloc */
   ...
   char *mymsg;
   ...
   mymsg = malloc (sizeof (char) * <some number of elements>);
   if (mymsg)
   {
       /* do something interesting here */
   }

If the buffer is static in size (that is, you know how long it needs to be), use

   char mymsg[<some number of elements>]

Note you can do something like

   char *mymsg = "Hello";
   char yourmsg[] = "Goodbye";

In both cases, the array size is computed from the initializer string.  The
difference is that mymsg may or may not be writable, depending on the
implementation, while yourmsg will be writable.

--
John Bode
"Paranoia is just reality on a finer scale" -- Strange Days

To email me directly, remove the 'nospam.' from my address.



Thu, 23 Mar 2000 03:00:00 GMT  
 newbie needs help to strings, strings, strings

Quote:



> > Please help newbie to c.

<snip>

Quote:
> Hi Quoc Nguyen,

> Section 6 of the FAQ (Frequently Asked Questions list) for
> comp.lang.c has a good discussion on the difference between
> pointers and arrays in C.  It also has a lot of other useful
> information on common C programming issues.  You can get the FAQ
> at www.eskimo.com/~scs/C-faq/top.html.

> If you still have questions about defining strings after you
> read the FAQ, post them here.

> Happy Programming
> Jack

Hi Quoc,

In addition to the above if you can get your hands on a copy of The C
Programming Language by Kernighan and Richie read pages 104 thru 107.
The FAQ, in Section 6.2, references these pages  in K&R and I find it
really helps to read both!

Good luck,

Kevin



Thu, 23 Mar 2000 03:00:00 GMT  
 newbie needs help to strings, strings, strings

Please help newbie to c.

It seems that there are two way to declare strings in c

char *mymsg;
or
char mymsg[ ];

when should I declare one way over the other?  If I declare the first
way, I do I reinitialize it to null?

Thanks in advance




Fri, 24 Mar 2000 03:00:00 GMT  
 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

> char *mymsg;
> or
> char mymsg[ ];

> when should I declare one way over the other?  If I declare
the first
> way, I do I reinitialize it to null?

> Thanks in advance



Hi Quoc Nguyen,

Section 6 of the FAQ (Frequently Asked Questions list) for
comp.lang.c has a good discussion on the difference between
pointers and arrays in C.  It also has a lot of other useful
information on common C programming issues.  You can get the FAQ
at www.eskimo.com/~scs/C-faq/top.html.

If you still have questions about defining strings after you
read the FAQ, post them here.

Happy Programming

Jack



Fri, 24 Mar 2000 03:00:00 GMT  
 newbie needs help to strings, strings, strings

: Please help newbie to c.

: It seems that there are two way to declare strings in c

There is no 'string' type in C.

: char *mymsg;

That is a pointer to char,not a string.

: or
: char mymsg[ ];

That would be an array of char, you are missing the size, however.
It would be OK if you declared it extern and defined it elsewhere.

: when should I declare one way over the other?  If I declare the first
: way, I do I reinitialize it to null?

Well it depends on what you want.  With a pointer you can allocate
memory dynamically, with an array the size is static.

gabor.
--
    If you want your program to be readable, consider supplying the argument.
        -- Larry Wall in the perl man page



Fri, 24 Mar 2000 03:00:00 GMT  
 newbie needs help to strings, strings, strings

<snip>

Quote:
> Note you can do something like

>    char *mymsg = "Hello";
>    char yourmsg[] = "Goodbye";

> In both cases, the array size is computed from the initializer string.
> The
> difference is that mymsg may or may not be writable, depending on the
> implementation, while yourmsg will be writable.

I like your answer, but you are partially wrong.

Remember that the compiler creates a separate null-terminated string for
"Hello" and "Goodbye". As you said earlier, char *mymsg is a pointer to
a char. Therefore, the result of char *mymsg = "Hello" is that mymsg
contains a pointer to the letter 'H'. The string is not copied. The
result of char yourmsg[] = "Goodbye" is that yourmsg contains a copy of
"Goodbye". You now have two copies of the string.

Have fun,
Zev Griner



Fri, 24 Mar 2000 03:00:00 GMT  
 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.    *
************************************************



Sat, 25 Mar 2000 03:00:00 GMT  
 newbie needs help to strings, strings, strings


: > 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.

The rest of your explanations was cool, however, the above is a syntax
error.  You cannot declare an 'empty' string.  mymsg needs to be
either :
defined elsewhere and then declared extern in your current file
or must include the size of the array.

gabor.
--
    Does the same as the system call of that name.
    If you don't know what it does, don't worry about it.
        -- Larry Wall in the perl man page regarding chroot(2)



Sun, 26 Mar 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Newbie question: Strings and string manipulation.

2. newbie needs string help

3. eval(string)?, or, 'nother newbie needs help

4. newbie needs inputting string help

5. Help about string & sub-string

6. Nee help with a string and temp string

7. please help newbie string replacement in big file

8. Newbie: Help with Strings

9. newbie help with strings.

10. newbie: help with a string

11. newbie help with strings

12. newbie: help with string reversal

 

 
Powered by phpBB® Forum Software