-5

EDIT

i want to create a program which will work with a long number. the user will be asked to input the length (in digits) of the number they want to work with. there is a

char aa [the chosen length goes here];

where in the [] brackets should be the value that was input by the user. it could look like this:

"please input the length in digits" there is a scanf and you input, for example, 17 then a char aa [17] is created

i just don't know how can i use a variable to input a value in the [] brackets defining the memory size that is saved for a char

lenka
  • 7
  • 4

2 Answers2

1

You want a variable lenght array, you can do it like this:

int digit;
scanf("%d", &digit);
char test [digit+1];

This will work for your purposes. Usual restrictions of course apply. Keep in mind this functionality was only added in the C99 Standard. So if your compiler supports only previous standards, this will not work.

However, the more appropriate and better practice method to use is to use malloc to allocate the char array properly

int digit;
scanf("%d", &digit);
char* aa = malloc(digit + 1);
//Do what you want with the char array aa
free(aa);

Do not forget to check the result of malloc() against NULL and free the variable afterwards to prevent memory leaks if you want to do this.

Also be aware that the "array" malloc returns is actually just a pointer, which is very relevant when trying to determine the size/length of it, using sizeof:

char real_array[5];
sizeof(real_array); // == 5

char* dynamic_array = malloc(5);
sizeof(dynamic_array) // == sizeof(char*) == on most systems = 8
sehe
  • 374,641
  • 47
  • 450
  • 633
Magisch
  • 7,312
  • 9
  • 36
  • 52
-1

What you are asking is variable length array which is possible in C99. See Dynamic array allocation on stack in C

Look at the following code snippet:

int size;
scanf("%d", &size);
char array[size + 1];  //1 extra space to accommodate `\0` character

printf("%d\n", sizeof(array));  

This will print sizeof(char) * (size+1) proving the array creation dynamically.

The only disadvantage of this way over malloc way is after declaring an array (which is of user-input size) you can't change the array size in program. It's fixed.

While using dynamic memory allocation (malloc, calloc) you can change the array size whenever possible in a program by freeing the memory.

Since variable length array is possible in c99 you may have to use gcc --std=c99 -c program.c to compile your source code. For other compiler refer the proper compiler manual.

Community
  • 1
  • 1
rootkea
  • 1,474
  • 2
  • 12
  • 32
  • when i input a value, the output is the value times 4 so this cannot be right – lenka Jan 07 '16 at 09:17
  • Also you don't have to wonder to get downvoted, when just posting a stub and then editing it after reading significant facts from the other answers. – Superlokkus Jan 07 '16 at 09:18
  • @lenka: what did you enter? If you enter `5` then you will get `20` as `sizeof(int)` is most probably `4` (machine dependent). So what you get is sizeof(array) which is = `sizeof(member) * array_size` – rootkea Jan 07 '16 at 09:20
  • @Superlokkus: Even if that "stub" contains the gist of answer? – rootkea Jan 07 '16 at 09:22
  • Also your answer IMHO didn't really targeted the question, but rather just throw a solution without any context. – Superlokkus Jan 07 '16 at 09:26
  • I downvoted this because it didn't answer the question. Now that it does answer the question, I'm leaving the downvote, so you have a reason to post a good answer from the start, instead of posting a very bad answer then improving it. – user253751 Jan 07 '16 at 09:35
  • A VLA is not a _dynamic_ array. The reason is given exactly in your text. – too honest for this site Jan 07 '16 at 10:16