Velocity Reviews > arrays

# arrays

mdh
Guest
Posts: n/a

 05-14-2006
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.

Alexander Bartolich
Guest
Posts: n/a

 05-14-2006
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 <(E-Mail Removed)>

Andrew Poelstra
Guest
Posts: n/a

 05-14-2006
On 2006-05-14, mdh <(E-Mail Removed)> 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.
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.

>

mdh
Guest
Posts: n/a

 05-14-2006

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.

Richard Heathfield
Guest
Posts: n/a

 05-14-2006
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)

mdh
Guest
Posts: n/a

 05-14-2006

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.

Richard Heathfield
Guest
Posts: n/a

 05-14-2006
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)

CBFalconer
Guest
Posts: n/a

 05-14-2006
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

Denis Kasak
Guest
Posts: n/a

 05-14-2006
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

pete
Guest
Posts: n/a

 05-15-2006
Andrew Poelstra wrote:
>
> On 2006-05-14, mdh <(E-Mail Removed)> 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