user8038216
user8038216

Reputation:

Are arrays variables or constants?

I just want to know: Are arrays in C variables or constants?

I am specially confused about char arrays.

Upvotes: 1

Views: 1651

Answers (2)

Pouyan
Pouyan

Reputation: 373

Array Definition:

Like any other variables, uninitialized array elements contain garbage values. An array is a group of contiguous memory locations that all have the same type.To refer to a particular location or element in the array, we specify the array’s name and the position number of the particular element in the array.

"C How To Program _ Deitel" book.

So as you can see arrays are memory location, thus you can initialize them and make them constants or you can use them as variables.

Array Specifying:

Are like variables definition. You should specify their type and their name and their length(in this case they are different from variables) e.g. int average[10] are sequence of memory that store 10 variable in it.

Character arrays:

In C are null_terminated and always end with '/0' or simply 0. For instance if you want to enter a name with 5 character you must define an array with length 5 + 1('\0'). If you don't you'll encounter an undefined behavior(UB).

You can initialize array in these ways:

char string[] ="computer" 
char string[12]="algorthm"
char string[]={'s','t','a','c','k','/0'}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

According to the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

1 An lvalue is an expression (with an object type other than void) that potentially designates an object;64) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a constqualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a constqualified type.

So arrays are non-modifiable lvalues. that is you may not write for example

char s1[] = "hello";
char s2[] = "hello";

s1 = s2;

The compiler will issue a diagnostic message that the code is invalid.

As for string literals then they have static storage duration and any attempt to modify a string literal results in undefined behavior.

From the C Standard (6.4.5 String literals)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

Compare these two code snippets.

char s[] = "hello";
s[0] = 'H';

and

char *s = "hello";
s[0] = 'H';

In the first code snippet there is declared a character array that is initialized by a string literal. That is the characters of the string literal are used to initialize the elements of the array. And you may to change the created array.

In the second code snippet there is declared a pointer to a strig literal. And in the second statement there is an attempt to change the string literal using the pointer that results in undefined behavior.

As for qualifiers like the const qualifier then (6.7.3 Type qualifiers)

9 If the specification of an array type includes any type qualifiers, the element type is so qualified, not the array type. If the specification of a function type includes any type qualifiers, the behavior is undefined

So this declaration

const char s[] = "hello";

means that each element of the array has the qualifier const in its type specification that is each element has the type const char.

Upvotes: 6

Related Questions