473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

arrays

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
14 1702
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
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
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
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
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
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
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.