By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,494 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,494 IT Pros & Developers. It's quick & easy.

arrays and strings misunderstanding...

P: n/a
Hello,

I wrote the tiny progam below just to understand arrays and strings
better.

I have 2 questions about arrays and strings in C.

1. Why is it that when you want to assign a string to an character array
that you must use the strcpy() function?

Why doesn't a assignment like gender = "male" not work for a character
array but for a pointer it works fine?

2. I declared a character array for 10 characters including the NULL
character, but as you see below I put more characters and it worked
fine. Can somebody explain me that please....

Thanks a lot in advance....

#include <stdio.h>
#include <string.h>

int main(void)
{
char *name;
char gender[10];

name = "broeisi";
strcpy(gender,"superb male");

printf("%s is a %s.\n",name, gender);

return 0;
}

This is the output of the program

broeisi@kitana Misc $ gcc arraystring.c -o arraystring
broeisi@kitana Misc $ ./arraystring
broeisi is a superb male.

I use gentoo linux with gcc version 3.4.4
Feb 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Broeisi wrote:
Hello,

I wrote the tiny progam below just to understand arrays and strings
better.

I have 2 questions about arrays and strings in C.

1. Why is it that when you want to assign a string to an character array
that you must use the strcpy() function?
You cannot assign to a character array, but you can assign to the array
elements. So if you want a char array to hold certain string, you need
to copy the chars.
Why doesn't a assignment like gender = "male" not work for a character
array but for a pointer it works fine?
An array is not a modifiable lvalue, so you cannot assign to it. A
pointer is a modifiable lvalue, that's why in the case of a pointer the
assignment works without problems. However if gender is a pointer the
assignment does not magically copy the string "male" to some
automagically allocated portion of memory. What it does make is storing
the address of the unnamed string literal "male" in the gender
variable.
2. I declared a character array for 10 characters including the NULL
character, but as you see below I put more characters and it worked
fine. Can somebody explain me that please....
You were unlucky. Stepping outside the boundaries of an array or
outside a dinamically allocated block of memory invokes undefined
behaviour, including seeming to work correctly. But the result could've
been anything, from crushing your computer to sending you a thousand
years into the past. (I'm tired of nasal demons... it's been a slow day
at work.)

HTH

Thanks a lot in advance....

#include <stdio.h>
#include <string.h>

int main(void)
{
char *name;
char gender[10];

name = "broeisi";
strcpy(gender,"superb male");

printf("%s is a %s.\n",name, gender);

return 0;
}

This is the output of the program

broeisi@kitana Misc $ gcc arraystring.c -o arraystring
broeisi@kitana Misc $ ./arraystring
broeisi is a superb male.

I use gentoo linux with gcc version 3.4.4


Feb 16 '06 #2

P: n/a
Antonio,

Thank you very much for your explanation.
It surely helped.

Have a nice and thanks again for your help.

Broeisi

Antonio Contreras wrote:
Broeisi wrote:
Hello,

I wrote the tiny progam below just to understand arrays and strings
better.

I have 2 questions about arrays and strings in C.

1. Why is it that when you want to assign a string to an character array
that you must use the strcpy() function?


You cannot assign to a character array, but you can assign to the array
elements. So if you want a char array to hold certain string, you need
to copy the chars.
Why doesn't a assignment like gender = "male" not work for a character
array but for a pointer it works fine?


An array is not a modifiable lvalue, so you cannot assign to it. A
pointer is a modifiable lvalue, that's why in the case of a pointer the
assignment works without problems. However if gender is a pointer the
assignment does not magically copy the string "male" to some
automagically allocated portion of memory. What it does make is storing
the address of the unnamed string literal "male" in the gender
variable.
2. I declared a character array for 10 characters including the NULL
character, but as you see below I put more characters and it worked
fine. Can somebody explain me that please....


You were unlucky. Stepping outside the boundaries of an array or
outside a dinamically allocated block of memory invokes undefined
behaviour, including seeming to work correctly. But the result could've
been anything, from crushing your computer to sending you a thousand
years into the past. (I'm tired of nasal demons... it's been a slow day
at work.)

HTH

Thanks a lot in advance....

#include <stdio.h>
#include <string.h>

int main(void)
{
char *name;
char gender[10];

name = "broeisi";
strcpy(gender,"superb male");

printf("%s is a %s.\n",name, gender);

return 0;
}

This is the output of the program

broeisi@kitana Misc $ gcc arraystring.c -o arraystring
broeisi@kitana Misc $ ./arraystring
broeisi is a superb male.

I use gentoo linux with gcc version 3.4.4


Feb 16 '06 #3

P: n/a
Broeisi wrote:
I wrote the tiny progam below just to understand arrays and strings
better.

I have 2 questions about arrays and strings in C.

1. Why is it that when you want to assign a string to an character array
that you must use the strcpy() function?

Why doesn't a assignment like gender = "male" not work for a character
array but for a pointer it works fine?
Because the original language designers simply didn't want to put that
in there. This stems from the fact that "strings" are not first-class
values in the C language. Characters are primitives, but strings are
not. Strings are basically "simulated" in the C language (and not very
well at that.)

The only sort of "primitive" support that the C language has for
strings is that inline strings (sequences of C source parsable chars
delimited by double quote characters) are interpreted in the form of
this simulation with its own implicit storage.
2. I declared a character array for 10 characters including the NULL
character, but as you see below I put more characters and it worked
fine. Can somebody explain me that please....

Thanks a lot in advance....

#include <stdio.h>
#include <string.h>

int main(void)
{
char *name;
char gender[10];

name = "broeisi";
This is not an array assignment, but a pointer assignment. The key
difference is that the storage for the string is declared by the value,
not the variable. So you cannot, for example, modify the value
"broeisi" by following this with a like like:

name[0] = 'd'; /* Ill defined, and will crash in some
environments. */

This is because the storage used comes from the value which is
considered unmutable (essentially, its implicitely const).
strcpy (gender, "superb male");


Here there are two strings. gender starts out uninitialized but this
is overwritten by the contents of the string "superb male". You have
an additional problem here because gender has only storage for 10
characters, but "superb male" is actually the following twelve
characters: { 's', 'u', 'p', 'e', 'r', 'b', ' ', 'm', 'a', 'l', 'e',
'\0' }; This is a buffer overflow, which you can learn all about here:

http://en.wikipedia.org/wiki/Buffer_Overflow

So long as the C language is just "simulating" strings rather than
providing a real primitive for them, there is an oppotunity for anyone
to write their own "simulation" for strings in the C language. "The
Better String Library" is one such example, and you can find the URL
for it at the bottom of this post. Using this library things are a
little more clear:

#include <stdio.h>
#include "bstrlib.h"

int main () {
struct tagbstring name = bsStatic ("broeisi");
bstring gender = blk2bstr (bsStaticBlkParms ("superb male"));

printf ("%s claims to be a %s.\n", name.data, bdatae (gender,
"??"));
bdestroy (gender);
return 0;
}

For a problem this small, the code isn't really shorter or simpler,
however the storage issue is somewhat clearer. Inline strings are
wrapped in bsStatic* macros which makes their storage clear. Also
issues like "buffer overflows" essentially disappear, since dynamic
storage container size issues are dealt with automatically in the
Better String Library (notice there is no value 10 anywhere in this
sample code), which is closer to what you find in other modern
languages.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Feb 16 '06 #4

P: n/a
Broeisi wrote:
I wrote the tiny progam below just to understand arrays and strings
better.

2. I declared a character array for 10 characters including the NULL
character, but as you see below I put more characters and it worked
fine. Can somebody explain me that please....
#include <stdio.h>
#include <string.h>

int main(void)
{
char *name;
char gender[10];
Reserve 10 bytes of memory for gender,
so the memory goes something like this

...........##########..........
aaaaaa<-GENDER->bbbbbb

Where "aaaaaa" and "bbbbbb" is memory reserved for something else or not
reserved at all (reserved for future use).

name = "broeisi";
strcpy(gender,"superb male");


Now the memory goes something like

...........superb male0........
aaaaaa<-GENDER->bbbbbb

So you've used memory space that doesn't belong to gender.
You might have overwritten something that was already there or that
final 'e0' may be overwritten by something else in your program removing
the terminating 0 from the string gender.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Feb 16 '06 #5

P: n/a
On Thu, 16 Feb 2006 11:37:07 -0600, in comp.lang.c , Broeisi
<br*******@gmail.com> wrote:
1. Why is it that when you want to assign a string to an character array
that you must use the strcpy() function?
Because an array is an array, not a pointer. This is almost certainly
a FAQ by the way.
Why doesn't a assignment like gender = "male" not work for a character
array but for a pointer it works fine?
It depends what you mean by "work". In both cases, if you want to copy
the data, you must copy it, not assign a pointer to point to it.
2. I declared a character array for 10 characters including the NULL
character, but as you see below I put more characters and it worked
fine. Can somebody explain me that please....


You were unlucky. If your compiler and OS had been more robust, they
would have stopped your programme executing before it wrotte over
memory that did not belong to it.
By teh way, that was three questions, not two... :-)
Mark McIntyre
--
"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 Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 16 '06 #6

P: n/a
Broeisi wrote:

<snip Broeisi question and my answer>
Antonio,

Thank you very much for your explanation.
It surely helped.

Have a nice and thanks again for your help.

Broeisi


You're wellcome. Glad I could help.

BTW, try to avoid top-posting. Other groups may have a more relaxed
attitude concerning netiquette, but c.l.c. has literally hundreds of
posts a day and top-posts makes reading them harder.

Feb 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.