array size

S

s4tanu

Hi,

char arr_one[] = {'a','b','c','d','\0'};

i want to make another array with same size as 'arr_one'
if i do like this -

size_t len= strlen(arr);
char arr_two[len];

when i complied this code
my gcc (ver 4) gives warning

warning: ISO C90 forbids variable-size array ‘arr_one’

it means i should not put variable name inside array brackets ?
if i write code like this -> arr_two[len] is this the wrong way to
write ?
Thanks for your suggestion
 
S

s4tanu

Hi,

char arr_one[] = {'a','b','c','d','\0'};

i want to make another array with same size as 'arr_one'
if i do like this -

size_t len= strlen(arr);
char arr_two[len];

when i complied this code
my gcc (ver 4) gives warning

warning: ISO C90 forbids variable-size array ‘arr_one’

it means i should not put variable name inside array brackets ?
if i write code like this ->  arr_two[len]   is this the wrong way to
write ?
Thanks for your suggestion

sorry for mistaken in fourth line it was strlen(arr_one)
 
B

Ben Bacarisse

Hi,

char arr_one[] = {'a','b','c','d','\0'};

i want to make another array with same size as 'arr_one'
if i do like this -

size_t len= strlen(arr);
char arr_two[len];

Note that this does not give you the size of arr_one. strlen does not
include the null at the end and, anyway, arr_one might be larger than
its string length suggests:

char arr_one[] = { 'a', 0, 0, 0, 0, 0 };
when i complied this code
my gcc (ver 4) gives warning

warning: ISO C90 forbids variable-size array ‘arr_one’

it means i should not put variable name inside array brackets ?
if i write code like this -> arr_two[len] is this the wrong way to
write ?

If you want C90, yes. C99 permits variable length arrays (not a good
descriptive term, but we are stuck with it).

In C90 (and C99) you can just write:

char arr_two[sizeof arr_one];
 
S

s4tanu

char arr_one[] = {'a','b','c','d','\0'};
i want to make another array with same size as 'arr_one'
if i do like this -
size_t len= strlen(arr);
char arr_two[len];

Note that this does not give you the size of arr_one.  strlen does not
include the null at the end and, anyway, arr_one might be larger than
its string length suggests:

  char arr_one[] = { 'a', 0, 0, 0, 0, 0 };
when i complied this code
my gcc (ver 4) gives warning
warning: ISO C90 forbids variable-size array ‘arr_one’
it means i should not put variable name inside array brackets ?
if i write code like this ->  arr_two[len]   is this the wrong way to
write ?

If you want C90, yes.  C99 permits variable length arrays (not a good
descriptive term, but we are stuck with it).

In C90 (and C99) you can just write:

  char arr_two[sizeof arr_one];

Thanks to teach me new thing that sizeof operator use in array bracket
 
K

Keith Thompson

char arr_one[] = {'a','b','c','d','\0'};

i want to make another array with same size as 'arr_one'
if i do like this -

size_t len= strlen(arr);
char arr_two[len];

when i complied this code
my gcc (ver 4) gives warning

warning: ISO C90 forbids variable-size array ‘arr_one’

it means i should not put variable name inside array brackets ?
if i write code like this -> arr_two[len] is this the wrong way to
write ?

strlen() doesn't give you the size of an array; it gives you the
number of characters preceding (i.e., *not* including) the first '\0'.
Using strlen to determine the size to allocate for an array is a
fairly common error.

You probably want this:

char arr_one[] = {'a','b','c','d','\0'};
char arr_two[sizeof arr_one];

Note that this takes advantage of the guarantee that sizeof char == 1.
For arrays of non-character types, you need to allow for the element size,
e.g.:

int arr_one[] = { 10, 20, 30, 40, 50 };
int arr_two[sizeof arr_one / sizeof arr_one[0]];

As for the warning, C90 requires an array size to be a constant
expression; this means, roughly, that it must be a value that can be
computed at compilation time. The result of a function call cannot be
a constant expression. C99 does allow variable-length arrays, so your
original code is legal in C99 -- but it's still probably not what you
want.
 
M

Martin Ambuhl

Hi,

char arr_one[] = {'a','b','c','d','\0'};

i want to make another array with same size as 'arr_one'

#include <stdio.h>
#include <string.h>

/* remove '#if 0'/'#endif' pairs and compile with -std=c99 to see that
* they both work. */

void c89_version(void)
{
char arr_one[] = { 'a', 'b', 'c', 'd', '\0' }; /* could have been
"abcd" */
char arr_two[sizeof arr_one];
printf("C89 version\n");
printf("sizeof arr_one = %lu, sizeof arr_two = %lu.\n",
(unsigned long) sizeof arr_one,
(unsigned long) sizeof arr_two);
strcpy(arr_two, arr_one);
printf("strlen(arr_one) = %lu, strlen(arr_two) = %lu.\n\n",
(unsigned long) strlen(arr_one),
(unsigned long) strlen(arr_two));
}

#if 0
void c99_additional_version(void)
{
char arr_one[] = { 'a', 'b', 'c', 'd', '\0' }; /* could have been
"abcd" */
char arr_two[strlen(arr_one) + 1]; /* note '+1' */
printf("C99 version designed to mimic OP's try.\n");
printf("sizeof arr_one = %zu, sizeof arr_two = %zu.\n",
sizeof arr_one,
sizeof arr_two);
strcpy(arr_two, arr_one);
printf("strlen(arr_one) = %zu, strlen(arr_two) = %zu.\n\n",
strlen(arr_one),
strlen(arr_two));
}
#endif

int main(void)
{
c89_version();
#if 0
c99_additional_version();
#endif
return 0;
}


[output with '$if 0','#endif' pairs removed, compiled with -std=c99]
C89 version
sizeof arr_one = 5, sizeof arr_two = 5.
strlen(arr_one) = 4, strlen(arr_two) = 4.

C99 version designed to mimic OP's try.
sizeof arr_one = 5, sizeof arr_two = 5.
strlen(arr_one) = 4, strlen(arr_two) = 4.
 
J

James Kuyper

Thanks to teach me new thing that sizeof operator use in array bracket

In C99, you can use any integer expression with a value greater than 0
as the dimension of an array. If that expression is not an integer
constant expression (ICE), then you're declaring a variable length array
(VLA). For arrays with static storage duration (and for all arrays in
C90), VLA's are not an option, so in those cases the expression has to
be an ICE.

As far as ordinary English usage is concerned, an ICE should be any
expression of integer type and a constant value. However, the standard
defines "integer constant expression" as a special piece of C jargon
with a more restricted meaning than that. Section 6.6p6 says:

"An _integer constant expression_ shall have integer type and shall only
have operands that are integer constants, enumeration constants,
character constants, sizeof expressions whose results are integer
constants, and floating constants that are the immediate operands of
casts. Cast operators in an integer constant expression shall only
convert arithmetic types to integer types, except as part of an operand
to the sizeof operator."
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top