arrays

M

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

Alexander Bartolich

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*/

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

Andrew Poelstra

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

Richard Heathfield

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).
 
R

Richard Heathfield

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?
 
C

CBFalconer

mdh said:
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/>
 
D

Denis Kasak

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*/

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

Andrew Poelstra

Andrew said:
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.
 
F

Flash Gordon

Andrew said:
Andrew said:
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.
 
A

Andrew Poelstra

Andrew said:
Andrew Poelstra 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.
 
H

Herbert Rosenau

Andrew said:
On 2006-05-14, mdh <[email protected]> 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!
 
K

Keith Thompson

Herbert Rosenau said:
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)).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top