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

arrays

P: n/a
mdh
I am still early on with K&R, but every now and again, I try to assign
to an array like this:

char arr[]="I am a string"; /* works*/

but

char arr[MAX]; /* MAX == some number */

char arr[]="I am a string"; / * Does not work */

or arr[]="I am a string"; / * Does not work */

Not done pointers yet. Could someone explain what is going on.

Thanks in advance.

May 14 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
mdh wrote:
I am still early on with K&R, but every now and again, I try to assign
to an array like this:

char arr[]="I am a string"; /* works*/
This is not an assignment but an initialization.
but

char arr[MAX]; /* MAX == some number */

char arr[]="I am a string"; / * Does not work */
This is an attempt to define a variable twice in the same scope.
or arr[]="I am a string"; / * Does not work */
arr[] is not a valid expression for the left side of an assignment.
Not done pointers yet. Could someone explain what is going on.


There is special syntax to initialize arrays, i.e. to provide
values on definition. For Example:

int arr[] = { 72, 45, 76, 76, 79 };

or

int arr[] = { 'H', 'E', 'L', 'L', 'O' };

For the special case of an array of characters there is the very
special syntax to initialize with a string literal, e.g:

char arr[] = "HELLO";

This will define an array of six char, the last item being a 0.
To omit this terminating zero you can specify exact array size:

char arr[5] = "HELLO";

In contrast, assignment of arrays involves pointers.

--
Ich kenne die Mißverständnisse-FAQ, und sie wird oft mißverstanden.
-- Andreas M. Kirchwitz <sl****************@krell.zikzak.de>
May 14 '06 #2

P: n/a
On 2006-05-14, mdh <md**@comcast.net> wrote:
I am still early on with K&R, but every now and again, I try to assign
to an array like this:

char arr[]="I am a string"; /* works*/
Valid C.
but

char arr[MAX]; /* MAX == some number */
Now arr points to an array of char or size MAX. Remember though that
arr is /not/ a pointer; it merely behaves similarly.
char arr[]="I am a string"; / * Does not work */ If you've already defined arr, "char arr" is a double definition.
or arr[]="I am a string"; / * Does not work */ If you've already defined arr to point to an array of size MAX, when
you attempt to point it to a string literal of size 14, it will bomb.
Remember, the address of your array is different from the address of
a string literal.

Basically: String literals are pointers. Arrays are not.

(Also, "arr[] =" is invalid.)
Not done pointers yet. Could someone explain what is going on.
You're going to need to finish pointers before you can do too much C.
Thanks in advance.

May 14 '06 #3

P: n/a
mdh

Andrew Poelstra wrote:
You're going to need to finish pointers before you can do too much C.

I agree, but this will help.

Thanks to both of you.

May 14 '06 #4

P: n/a
mdh said:
I am still early on with K&R, but every now and again, I try to assign
to an array like this:

char arr[]="I am a string"; /* works*/
Yes.

but

char arr[MAX]; /* MAX == some number */

char arr[]="I am a string"; / * Does not work */
No, because you already defined it. But:

char arr[MAX] = "I am a string"; /* works */
or arr[]="I am a string"; / * Does not work */

Not done pointers yet. Could someone explain what is going on.


As you have discovered, you can initialise arrays (i.e. give them a value at
the same time that you define them), but you can't assign to them.

But you /can/ assign to their members. That's how string functions work - by
dealing with the individual elements in the array, rather than the whole
array. So:

#include <string.h>

#define MAX 32

int main(void)
{
char arr[MAX];
strcpy(arr, "I am a string"); /* works */
return 0;
}

Just keep plugging away with K&R - all will become clear(er).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 14 '06 #5

P: n/a
mdh

Richard Heathfield wrote:
Just keep plugging away with K&R - all will become clear(er).

thanks...I will...but this board's encouragement certainly helps.

May 14 '06 #6

P: n/a
Andrew Poelstra said:
char arr[MAX]; /* MAX == some number */
Now arr points to an array of char or size MAX.


No, it doesn't. arr is an array, not a pointer. It doesn't point anywhere.
Basically: String literals are pointers.
No, a string literal with N characters is an array of N + 1 char.
Arrays are not [pointers].


Right - so they don't point, do they?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 14 '06 #7

P: n/a
mdh wrote:
Richard Heathfield wrote:
Just keep plugging away with K&R - all will become clear(er).


thanks...I will...but this board's encouragement certainly helps.


Just to keep things clear, 'this' is not a board, but a flawed
interface to the worldwide usenet news system. This allows you to
exchange knowledge with people all over the world, and is a system
that has existed for something like 25 years.

Congratulations on using the google interface intelligently.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

May 14 '06 #8

P: n/a
mdh wrote:
I am still early on with K&R, but every now and again, I try to assign
to an array like this:

char arr[]="I am a string"; /* works*/
Here you are defining an array 'arr' whose elements are of type char.
The part after the '=' character denotes initialisation. You can
initialise arrays with content while defining them by using the syntax
above. Note that this is *not* an assignment.
but

char arr[MAX]; /* MAX == some number */
You are defining an array of MAX chars which is named 'arr'.
char arr[]="I am a string"; / * Does not work */
You are attempting to redefine the identifier 'arr'. This is not allowed
because you have already defined it in the current scope.
or arr[]="I am a string"; / * Does not work */
Now you may wonder why this doesn't work when your first example does.
The reason for this is that, even though the same '=' character is used
here as in your first example, it doesn't have the same meaning. Here it
denotes the assigment operator and arrays can't be assigned to.

The proper way of copying "I am a string" into arr[] would be by using
strcpy().
Not done pointers yet. Could someone explain what is going on.


Just keep on reading and you will reach enlightement.

--
Denis Kasak

May 14 '06 #9

P: n/a
Andrew Poelstra wrote:

On 2006-05-14, mdh <md**@comcast.net> wrote:
char arr[]="I am a string"; /* works*/

Basically: String literals are pointers. Arrays are not.


There is no pointer in the line of code quoted above.

--
pete
May 15 '06 #10

P: n/a
On 2006-05-15, pete <pf*****@mindspring.com> wrote:
Andrew Poelstra wrote:

On 2006-05-14, mdh <md**@comcast.net> wrote:

> char arr[]="I am a string"; /* works*/

Basically: String literals are pointers. Arrays are not.


There is no pointer in the line of code quoted above.

I swear I read somewhere that string literals and/or arrays are pointers.
I've gotta get TYC21 out of my head.

An array name points to its first element. String literals have addresses,
which is what is pointed to by 'char *str = "This is a string.";'

I appreciate that arrays and pointers are different, but I've never
understood /why/ we make that distinction, other than in 3 specific cases.
May 15 '06 #11

P: n/a
Andrew Poelstra wrote:
On 2006-05-15, pete <pf*****@mindspring.com> wrote:
Andrew Poelstra wrote:
On 2006-05-14, mdh <md**@comcast.net> wrote:
char arr[]="I am a string"; /* works*/
Basically: String literals are pointers. Arrays are not. There is no pointer in the line of code quoted above.

I swear I read somewhere that string literals and/or arrays are pointers.
I've gotta get TYC21 out of my head.

An array name points to its first element.


No, it refers to the entire array, it just happens to decay to a pointer
to the first element most of the time.
String literals have addresses,
So does an int.
which is what is pointed to by 'char *str = "This is a string.";'

I appreciate that arrays and pointers are different, but I've never
understood /why/ we make that distinction, other than in 3 specific cases.


You mean as operands of &, sizeof, or as an initialiser for an array?
How about assignment operators? Or increment/decrement operators?

How about:
{
char *p;
char a[10];
p[0] = 'x';
a[0] = 'x';
}
Does the distinction matter in the above code fragment?

The distinction is made so heavily a around here because so many
mistakes are made by people due to them not understanding the distinction.

See section 6 of the comp.lang.c FAQ at http://c-faq.com/ and consider
not only the answers but why the questions ended up being in the FAQ in
the first place, noting the FAQ stand for *Frequently* Asked Questions.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 15 '06 #12

P: n/a
On 2006-05-15, Flash Gordon <sp**@flash-gordon.me.uk> wrote:
Andrew Poelstra wrote:
On 2006-05-15, pete <pf*****@mindspring.com> wrote:
Andrew Poelstra wrote:
On 2006-05-14, mdh <md**@comcast.net> wrote:
> char arr[]="I am a string"; /* works*/
Basically: String literals are pointers. Arrays are not.
There is no pointer in the line of code quoted above.
I swear I read somewhere that string literals and/or arrays are pointers.
I've gotta get TYC21 out of my head.

An array name points to its first element.


No, it refers to the entire array, it just happens to decay to a pointer
to the first element most of the time.
String literals have addresses,


So does an int.
which is what is pointed to by 'char *str = "This is a string.";'

I appreciate that arrays and pointers are different, but I've never
understood /why/ we make that distinction, other than in 3 specific cases.


You mean as operands of &, sizeof, or as an initialiser for an array?
How about assignment operators? Or increment/decrement operators?

How about:
{
char *p;
char a[10];
p[0] = 'x';
a[0] = 'x';
}
Does the distinction matter in the above code fragment?

Well, yeah, because p has not been allocated.
The distinction is made so heavily a around here because so many
mistakes are made by people due to them not understanding the distinction.
I can understand that the above might not be obvious to a beginner.
See section 6 of the comp.lang.c FAQ at http://c-faq.com/ and consider
not only the answers but why the questions ended up being in the FAQ in
the first place, noting the FAQ stand for *Frequently* Asked Questions.

Thank you for your informative and kind answer.

May 15 '06 #13

P: n/a
On Mon, 15 May 2006 14:26:11 UTC, Andrew Poelstra
<ap*******@localhost.localdomain> wrote:
On 2006-05-15, pete <pf*****@mindspring.com> wrote:
Andrew Poelstra wrote:

On 2006-05-14, mdh <md**@comcast.net> wrote:
> char arr[]="I am a string"; /* works*/

Basically: String literals are pointers. Arrays are not.


There is no pointer in the line of code quoted above.

I swear I read somewhere that string literals and/or arrays are pointers.
I've gotta get TYC21 out of my head.


Maybe, there are enough idiots around who are unable to understund C.

An array name is NOT a pointer but can decide behave like one in some
contect.
An array name points to its first element. String literals have addresses,
which is what is pointed to by 'char *str = "This is a string.";'
True, but nothing makes them to be a pointer. A string literal is an
array not a pointer. An array can decite to deliver its address in
pointer context but it is noways an pointer on itself.
I appreciate that arrays and pointers are different, but I've never
understood /why/ we make that distinction, other than in 3 specific cases.


An array is the name of the base of its sequence of objects. A string
literal is an unnamed array, both are able to deliver its base address
(aka pointer to its first object) but they are nowasy a pointer.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
May 16 '06 #14

P: n/a
"Herbert Rosenau" <os****@pc-rosenau.de> writes:
[...]
An array is the name of the base of its sequence of objects. A string
literal is an unnamed array, both are able to deliver its base address
(aka pointer to its first object) but they are nowasy a pointer.


Given:
int arr[42];
arr is the name of the entire array object, not just of its base.
For example, "sizeof arr" yields the size of the entire object
(42*sizeof(int)).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 16 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.