1

i am writing a simple program and would like to use the strlen function to get the size of the string in question.

Then i would like to create an array for example testarray[strlen(somestring)];

But it cannot compile, the error says "error: variable-sized object may not be initialized".

However, the length of that string will not change so i guess i have to tell my machine that somehow.

I have only access to these headers.

stdio.h string.h

Would be great if somebody could drop some knowledge bombs. :)

edit: I was wrong testarray[strlen(somestring)]; can compile but testarray[strlen(somestring)] = {}; cannot. With testarray[strlen(somestring)]; i have garbage if i try to use it.

Thanks guys, it is working now with memset!

Susan
  • 301
  • 1
  • 5
  • 17
  • `strlen` will return the number of character from the starting pointer given to the nearest character that is NUL character (0). It doesn't return the size of the buffer/array. – nhahtdh Nov 06 '12 at 03:53
  • Turns out this is a duplicate of http://stackoverflow.com/questions/3082914/c-compile-error-variable-sized-object-may-not-be-initialized . – Jamey Sharp Nov 06 '12 at 03:57
  • I think most modern compilers will allow variable array initialization, if `somestring` remains fixed at compile-time. – Aniket Inge Nov 06 '12 at 03:59

4 Answers4

2

Your best bet is to use "malloc()"

char *testarray = (char *)malloc(strlen(somestring)+1);

If you were programming in C++, I'd encourage you to use a vector<>.

C99 introduced "variable length arrays", but they're not portable to all C compilers. Personally, I'd discourage you from using them.

Which leaves you with "malloc ()" ;)

And, nhahtdh is quite right, be sure you always do a corresponding "free()".

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks but malloc doesn't seem to be in those two libraries. If i write it like this char temp[strlen(str) + 1]; it can compile but there appears to be garbage inside, if i want to write char temp[strlen(str) + 1] = {}; i cannot compile. – Susan Nov 06 '12 at 04:04
  • @user1801930: `malloc` is in the C standard library. A C implementation cannot be complete without providing it. It can be found in `stdlib.h`. Also, your snippet doesn't make sense, that's why it doesn't compile. What were you intending that to do? Those header files are not "libraries", but if you can't use `malloc` (sure about that?) and you don't have a C99 implementation which allows for variable length arrays you will need to allocate an array with a maximum size up front. – Ed S. Nov 06 '12 at 04:12
  • Also, @paulsm4, don't cast the return value of `malloc`, http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Ed S. Nov 06 '12 at 04:13
  • 1
    Even if VLA worked universally, using it would be a very bad idea because there's no way to know if space is actually available for storing a large VLA, and no way for the implementation to report failure. – R.. GitHub STOP HELPING ICE Nov 06 '12 at 04:19
  • Sorry, i thought they were called libraries. – Susan Nov 06 '12 at 04:20
  • @user1801930: No problem, terminology sets in with time, but that was probably the least relevant part of my comment :) – Ed S. Nov 06 '12 at 04:21
  • @R..: Agreed, I'm not actually convinced VLA's were ever a good idea. – Ed S. Nov 06 '12 at 04:22
  • VLA is a horrible idea. Pointer-to-VLA is an excellent idea. As in `double (*A)[cols] = calloc(sizeof *A, rows);` Unfortunately C99 required both and C11 makes both optional. – R.. GitHub STOP HELPING ICE Nov 06 '12 at 04:44
  • @EdS.: `malloc` is only required for hosted implementations of C. Freestanding implementations are not required to provide `malloc` (section 4, paragraph 6). – dreamlax Jun 27 '13 at 03:51
  • @dreamlax: Yes, but that's going to a very rare situation. If you find yourself in such an environment you make do with what you have, but it's hardly common. – Ed S. Jun 27 '13 at 04:07
0

You need to initialize the memory in temp to 0. This is what the empty brackets {} did in your original code. Either use memset

memset(temp, 0, sizeof(temp));

Or a for loop (since you don't have malloc/free you may not have memset).

int length = sizeof(temp);
int i;
for (i = 0; i < length; ++i)
{
   temp[i] = 0;
}
Jason
  • 1,612
  • 16
  • 23
0

testarray[strlen(somestring)] = {};

The above initialization not possible. Because the string size will be calculated dynamically(May vary). For static array initialization we should provide the exact size of the array.

You can initialize your dynamic array by using 'memset'.

Sankar Mani
  • 168
  • 3
  • In C99 it is possible using variable-length arrays. These are array types whose size is determined at runtime. – dreamlax Jun 27 '13 at 03:54
0

I hope this helps

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

int main(int argc, const char * argv[])
{
    char array[256];
    printf ("Enter a sentence: ");
    fgets(array, 256, stdin);
    printf ("The sentence entered is %zu characters long.\n", strlen(array));
    return 0;
}