0

I want to make a simple variable for number of the round for a loop, so I tried my code

int size,counter,marks[size];
scanf("enter %d/n",&size);
for(counter=0;counter<size;counter++)
{
    scanf("%d",&marks[counter]);
}

and compiled with no error but in run, it just shows "process returned -1073741571 <0*c00000FD>.

so I tried gets function and it shows "too many arguments to function 'gets' ".

int size;
int counter;
int marks[size];
scanf("enter %d/n",&size);
for(counter=0;counter<size;counter++)
{
    gets("%d",&marks[counter]);
}

I'm using code::blocks 17.12 and the gnu compiler.

ggorlen
  • 44,755
  • 7
  • 76
  • 106

3 Answers3

1

size must have a defined value, for example:

#include <stdio.h>
int main()
{
    int size;
    size = 5; // size must be constant
    int counter, marks[size];
    for (counter = 0; counter < size; counter++)
    {
        scanf("%d", &marks[counter]);
    }
    //Printing it returns correct values:
    for (counter = 0; counter < size; counter++)
    {
        printf("%d\n", marks[counter]);
    }
}

You can instead input it's value from the user if you want.

However, if for some reason, size is to be defined after the array is declared, use pointers:

#include <stdio.h>
#include "stdlib.h"
int main()
{
    int size;
    int counter, *marks;
    size = 5; //declared after the array
    marks = (int *)malloc(size * sizeof(int));
    for (counter = 0; counter < size; counter++)
    {
        scanf("%d", &marks[counter]);
    }
    //Printing it returns correct values:
    for (counter = 0; counter < size; counter++)
    {
        printf("%d\n", marks[counter]);
    }
    //Don't forget to free the array in the end
    free(marks);
}
Xosrov
  • 719
  • 4
  • 22
  • so if i put a const value and then put an scanf for it the problem will be solved? – arash tarakmeh Jun 07 '19 at 18:44
  • This doesn't solve either problem in OP's code, unfortunately. The `scanf` format is wrong, and even if it was correct, the user's `size` input would crash if it was anything greater than 5. No need to [cast the return value of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – ggorlen Jun 07 '19 at 18:45
  • @ggorlen Why would it crash? And why is the format wrong? I would be happy to learn if there's something wrong with the code :) – Xosrov Jun 07 '19 at 18:48
  • Sure. This is for the top example. You allocate `5 * sizeof(int)` bytes for the `marks` array. Then you presumably ask the user to specify `size`. if the user enters anything larger than 5, the `for` loop will allow the user to write past the size of the array. This is fixed in the below example with `malloc`, but there's no good reason to `malloc` here, and the size is no longer specifiable for the user--you're stuck with 5. – ggorlen Jun 07 '19 at 18:51
  • @ggorlen You can substitute ```scanf``` for the numeral declarations here and that won't really break anything, I just meant to show they need to be declared a value first( 5 being a value). But I do agree it was confusing. Thanks for pointing that out :D – Xosrov Jun 07 '19 at 18:59
  • @ggorlen but that's exactly what you've done in your answer :P – Xosrov Jun 07 '19 at 19:09
  • `"enter %d/n"`? This doesn't work--you'll get a garbage `size` value back. – ggorlen Jun 07 '19 at 19:20
1

Please don't use gets. It's dangerous.

As for your error in the scanf example, the first problem is the line

int size,counter,marks[size];

which declares marks with the uninitialized size value. Try initializing size first, then declaring the marks array.

Your second problem is scanf formatting string. Use scanf to read formatted input, not output a prompt. Use puts or printf for that.

Here's a full example:

#include <stdio.h>

int main(void) {  
    int size;
    printf("Enter a size value: ");
    scanf("%d", &size);
    int marks[size];

    for (int i = 0; i < size; i++) {
        printf("Enter element %d: ", i);
        scanf("%d", &marks[i]);
    }

    printf("You entered: ");

    for (int i = 0; i < size; i++) {
        printf("%d ", marks[i]);
    }

    puts("");
    return 0;
}

Here's a sample run:

Enter a size value: 4
Enter element 0: 88
Enter element 1: 77
Enter element 2: 66
Enter element 3: 55
You entered: 88 77 66 55

If you're writing ANSI C-compatible code you can use dynamic memory with malloc:

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

int main(void) {  
    int i, size, *marks;
    printf("Enter a size value: ");
    scanf("%d", &size);

    if (size < 1) {
        fprintf(stderr, "Invalid size specified\n");
        exit(1);
    }

    marks = malloc(size * sizeof(int));

    if (!marks) {
        fprintf(stderr, "malloc failed\n");
        exit(1);
    }

    for (i = 0; i < size; i++) {
        printf("Enter element %d: ", i);
        scanf("%d", &marks[i]);
    }

    printf("You entered: ");

    for (i = 0; i < size; i++) {
        printf("%d ", marks[i]);
    }

    free(marks);
    puts("");
    return 0;
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106
1

size can have any value when the array marks is allocated because it is not initialized. The array might be smaller than the entered size and so marks are stored in non-allocated memory, giving you the error.

This is a possible solution, but it doesn't compile with strict ISO C90. Presumably your CodeBlocks uses GCC that accepts variable length arrays and mixed declarations and code.

#include <stdio.h>

int main(void) {
    int size;
    printf("enter size: ");
    scanf("%d",&size);
    int marks[size];
    int counter;
    for (counter = 0; counter < size; counter++) {
        scanf("%d", &marks[counter]);
    }
    for (counter = 0; counter < size; counter++) {
        printf("%d: %d\n", counter, marks[counter]);
    }
    return 0;
}

BTW, please don't say "build error" if you have a runtime error. ;-)

the busybee
  • 10,755
  • 3
  • 13
  • 30