-1

Write a program that asks the users to enter a value for array size “n” and fill up the array with n integers. Then reverse the array and print it on the screen. I'm using Visual Studio and have this so far: I am having a problem with "size" in "int arr1[size]". It says it must be a constant value.

#include <stdio.h>
int main(void)
{
int size, i;
printf("Enter the size of the arrays:\n");
scanf("%d", &size);

int arr1[size];
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
    scanf_s("%d", arr1[size]);
}
printf("The current array is:\n %d", arr1[i]);
}
Marcus Rogers
  • 31
  • 1
  • 1
  • 3
  • 4
    And what is your actual question? What *specific* problem is preventing you from progressing your work? – kaylum Apr 18 '17 at 04:43
  • 1
    You can't use `int arr1[size];` to declare a static array, if `size` has a dynamic value. https://www.tutorialspoint.com/cprogramming/c_memory_management.htm – Alexander Apr 18 '17 at 04:44
  • i'm having a problem with "size" in "int arr1[size]" – Marcus Rogers Apr 18 '17 at 04:45
  • Visual Studio has a somewhat older C dialect, and perhaps it doesn't have arrays with variable size? – Thomas Padron-McCarthy Apr 18 '17 at 04:46
  • I don't think thats the problem, my teacher assigned this problem to be done with visual studio. – Marcus Rogers Apr 18 '17 at 04:48
  • 2
    In addition `scanf_s("%d", arr1[size]);` is wrong for multiple reasons. `arr1[size]` is an `int` not a pointer to `int` as required. And it's out of bounds anyway as `size` is not a valid index. – kaylum Apr 18 '17 at 04:49
  • 1
    Did the teacher really tell you to do exactly `int arr1[size];`?? There are other ways to allocate memory that VS does support. – kaylum Apr 18 '17 at 04:49
  • She just gave us this assignment lol.. we never really had to do something like this in class. I'm open to other ways of doing this. – Marcus Rogers Apr 18 '17 at 04:50
  • 2
    Please, read your text book or notes. Memory allocation is well covered in any basic C book or tutorial. If you are going to learn then use the resources that are already available to you and only seek further help once you have done that basic research. – kaylum Apr 18 '17 at 04:51
  • we dont have a textbook, Ive asked on many forums and still no solution to my problem. I know I can use malloc but not to sure on how to use it. – Marcus Rogers Apr 18 '17 at 04:53
  • 1
    And that's *exactly* why I'm downvoting you. This is not a site where other people do your homework. This is a site where users describe their specific problem, what they've tried, and people provider pointers. If you can't do enough C research, trial and error coding and Googling to work this out, you are not going to get much out of StackOverflow. Sorry. – AlwaysLearning Apr 18 '17 at 04:53
  • lol i dont care if you downvote me. Im not asking you to do my homework. There is more to the problem. I am asking on how to allow the user to enter the size of an array. – Marcus Rogers Apr 18 '17 at 04:55
  • 3
    If you seriously could not find any explanation or examples of how to use `malloc` then you really need to hone your google fu skills. – kaylum Apr 18 '17 at 04:56
  • I have found examples. They did not work for me. Not sure if its visual studios or something else. – Marcus Rogers Apr 18 '17 at 04:58
  • OK, I understand. The examples did not work for you. Please supply link/s to a couple of those examples that did not work. – ThingyWotsit Apr 18 '17 at 06:55
  • Also, please fix your indentation in your originally posted code. – ThingyWotsit Apr 18 '17 at 06:58
  • Please... examples that did not work? Links? – ThingyWotsit Apr 18 '17 at 11:02

3 Answers3

5

Some dialects of C have variable-length arrays (VLA), notably C99 (and GCC accepts them....)

But your teacher probably wants you to understand C dynamic memory allocation, and you should also test when scanf fails, so you might code:

int main(void) {
    int size, i;
    printf("Enter the size of the arrays:\n");
    if (scanf("%d", &size)<1) {
      perror("scan size");
      exit(EXIT_FAILURE);
    };

Actually, you'll better test against the case of size being negative! I leave that up to you.

    int *arr1 = calloc(size, sizeof(int));
    if (arr1==NULL) {
       perror("calloc arr1");
       exit(EXIT_FAILURE);
    }

You always need to handle failure of calloc or malloc

 printf("Enter the elements of the array:\n");
 for (i = 0; i < size; i++) {
    if(scanf("%d", &arr1[size])<1) {
      perror("scanf arr1[size]");
      exit(EXIT_FAILURE);
    }
 }

scanf_s is only existing in C11, and on a C11 compliant implementation you could use VLAs

I leave the rest up to you. You want to code a for loop to print every element of the array.

Of course you should call free appropriately (and I am leaving that to you). Otherwise, you have a memory leak. Some systems have valgrind to help hunting them. Don't forget to compile with all warnings & debug info (with GCC use gcc -Wall -g) then use the debugger (e.g. gdb).

Don't forget that a programming language is a specification (written in some document), not a software. You may want to glance into n1570. And you need to understand what undefined behavior means.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
3

You can't use static memory allocation to allocate an array with a dynamic size. You'll need to dynamically request memory to be allocated to your program, by the operating system, of whatever dynamic size the use asks for.

Here's an example to get started:

#include <stdlib.h>
#include <stdio.h>

void printArray(int *array, int size) {
    // Sample array: [1, 2, 3, 4, 5]
    printf("["); // [
    for (int i = 0; i < size - 1; i++) { // [1, 2, 3, 4, 
        printf("%i, ", array[i]);
    }
    if (size >= 1) printf("%i", array[size-1]); // [1, 2, 3, 4, 5
    printf("]\n"); // [1, 2, 3, 4, 5]
}

int main(void) {
    int count;
    printf("Enter the size of the array:\n");
    scanf("%d", &count);

    // ask for enough memory to fit `count` elements,
    // each having the size of an `int`
    int *array = malloc(count * sizeof(*array));
    if (!array) {
        printf("There was a problem with malloc.");
        exit(EXIT_FAILURE);
    }

    printf("Enter the elements of the array:\n");
    for (int i = 0; i < count; i++) scanf("%d", &array[i]);

    printArray(array, count);

    //do stuff

    free(array);
}
Alexander
  • 59,041
  • 12
  • 98
  • 151
2

In C , arrays can not be defined with user defined size in this fashion .

Incorrect way :

int size, i;
printf("Enter the size of the arrays:\n");
scanf("%d", &size);   
int arr1[size];

Correct way :

int size ;
int *arr1 ;
printf("Enter the size of the arrays:\n");
scanf("%d", &size);  
 arr1 = (int *) malloc(sizeof(int) * size);
Tanmay Patil
  • 659
  • 1
  • 4
  • 21
  • 3
    Some dialects of C have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array), notably C99 (and [GCC](http://gcc.gnu.org/) accepts [them](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html)....) – Basile Starynkevitch Apr 18 '17 at 04:57