1

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

I am specially confused about char arrays.

alk
  • 69,737
  • 10
  • 105
  • 255
  • Arrays are objects. Specifically objects of *array types*, which are a subset of *derived types*. – EOF Jul 21 '17 at 11:35
  • If not a duplicate, then a very close relative of https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer?noredirect=1&lq=1 – Ilja Everilä Jul 21 '17 at 11:36
  • 1
    A constant is something whose value cannot be changed during the execution of the program. An array is a data structure that can hold many values of the same type. Many times an array's "cells" start as having an unknown value, then they are "filled" with useful values. It doesn't look like something that cannot be changed to me :-) – axiac Jul 21 '17 at 11:45
  • 1
    That's a nonsensical question - are integers variables or constants? I suspect that what's confusing you is string *literals*, which is a special notation for describing a read-only, zero-terminated `char` array. – molbdnilo Jul 21 '17 at 11:46
  • `const` arrays are constant (you cannot modify their contents), non-`const` arrays are variable (you can modify them accessing the element cells) – Luis Colorado Jul 24 '17 at 05:09

2 Answers2

7

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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'}
Community
  • 1
  • 1
Pouyan
  • 363
  • 5
  • 22
  • "*Character arrays: In C are null_terminated ...*" no, no, just C-"strings" are! Best example you give yourself with `char string[]={'s','t','a','c','k'}`, which is a `char`-array of 5 elements, with ***no*** `0`-terminator. – alk Jul 22 '17 at 07:49
  • @alk I've mentioned that it is only for C language. You're right I should include 0 too. I forgot. thanks. – Pouyan Jul 22 '17 at 11:44
  • Again, your answer is partly ambiguous (to not say partly wrong) in terms of that a `char` ***not necessarily*** is a string, so not *each* `char`-array would be `0`-terminated. Also the `NUL` character is `'\0'`. – alk Jul 22 '17 at 11:47