0

I want that array (marks) size increases by 1 with each input of user:

#include <stdio.h>
main() {
    int size=1;
    int marks[size];
    int i;
    printf("Enter marks\n");
    for(i=0;i<size;i++) {
        scanf("%d",&marks[i]);
        size++;
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Because `marks` is *not* being "increased" dynamically. It is of size `1`. And you get undefined behavior. – Eugene Sh. Aug 09 '18 at 16:54
  • 1
    I would suggest you to read some basic books/tutorial on C programming. Specially arrays and dynamic memory allocations. – haccks Aug 09 '18 at 16:54
  • 1
    Because `int marks[size];` is not dynamically resized. You were unlucky to get as far as `4` iterations. – Weather Vane Aug 09 '18 at 16:54
  • Come on OP, if the program had worked it would run until the machine explodes. – Weather Vane Aug 09 '18 at 16:55
  • Since you're using a VLA, you must be using at least C99. However, C99 forbids the use of plain `main()`; you must specify the return type, and that should be `int`, hence [`int main(void)`](http://stackoverflow.com/questions/204476/) for preference and `int main()` if you're lazy. However, this isn't an immediate cause of trouble (though it should be). You have multiple other problems in the code — serious ones — and many of them have been pointed out by others. – Jonathan Leffler Aug 09 '18 at 17:33

5 Answers5

1

I want that array (marks) size increases by 1 with each input of user.

That's not how arrays work in C.

To get that functionality, you'll have to use dynamically allocated memory using malloc and realloc.

Also, your code has a logic flaw. If you look at the variables that control the loop, you have:

int size=1;
int i;
for(i=0;i<size;i++) { // i will never catch up to size before size overflows
    size++;
}

For your needs, you can use a variable length array (VLA) or use malloc only once.

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

int main()
{
   int size;
   int i;

   printf("Enter size\n");
   if ( scanf("%d", &size) != 1 )
   {
      // Problem. Deal with error.
   }

   // Use a VLA
   int marks[size];

   // or
   // Allocate memory.
   // int* marks = malloc(sizeof(*marks)*size);


   printf("Enter marks\n");
   for ( i = 0; i < size; i++)
   {
      if ( scanf("%d", &marks[i]) != 1)
      {
         // Problem. Deal with error.
      }
   }

   // Deallocate the memory. Needed when using malloc
   // free(marks);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Because you have statically declared marks array to size 1. Since you are incrementing size in loop will lead to undefined behavior. Better approach would be allocate marks dynamically. Consider below example for reference.

#include <stdio.h> 
#include<stdlib.h>
void main() {
 int size=0; 
 int *marks = NULL;
 int i; 

printf("Enter number of marks\n");
scanf ("%d",&size);
marks = (int *) malloc(size*sizeof(int));

printf("Enter marks\n");
 for(i=0;i<size;i++)
 { 
     scanf("%d",&marks[i]); 
 } 
}
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • Main issues 1. You do not check the result of `malloc` 2. You do not check result of scanf. 3. sizeof(type) is considered a bed practice you should use the object instead. 4, Most of the users here consider the cast of the malloc as dangerous and bad. – 0___________ Aug 09 '18 at 18:19
1

You can't increase the size of your array dynamically. But if you know how many scores will be entered, then you can set size to that value and then accept values until that size is reached.

#include <stdio.h>
int main()
{
  int size = 10;
  int marks[size];
  int i;
  printf("Enter marks\n");
  for(i = 0; i < size; i++)
  {
    scanf("%d",&marks[i]);
  }
}

Resizing dynamically will increase the complexity of your program significantly, but if that's exactly what you're trying to do, check out R Sahu's answer here or this answer: increasing array size dynamically in c.

Alex Johnson
  • 958
  • 8
  • 23
1

Your code assumes an increase in size will increase the size of the native array. That isn't how arrays work in C.

Native arrays in C are fixed-length after their definition. If you want to dynamically grow a sequence of things, then you need to manage a dynamic sequence, doing it inner-loop as valid data is received.

Code

The following prompts (or lack thereof) the user in the same fashion your code apparently desired. However, a loop termination condition has been added (a mark entry of -1 will terminate the loop, as will any invalid non-convertible input).

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

int main()
{
    int *marks = NULL, mark = 0;
    int size = 0;

    printf("Enter marks\n");

    while (scanf("%d", &mark) == 1 && mark != -1)
    {
        // expand marks
        void *tmp = realloc(marks, (size+1) * sizeof *marks);
        if (tmp == NULL)
        {
            perror("Failed to expand marks array");
            exit(EXIT_FAILURE);
        }

        marks = tmp;
        marks[size++] = mark;
    }

    // TODO: use marks[0] through marks[size-1] here
    for (int i=0; i<size; ++i)
        printf("%d ", marks[i]);
    fputc('\n', stdout);


    // then free marks
    free(marks);

    return EXIT_SUCCESS;
}

Sample Input

1 2 3 4 
5 6 
7 8 9
10 -1

Output

1 2 3 4 5 6 7 8 9 10

Notes: There are more efficient geometric growth algorithms that considerably reduce the number of realloc calls, For example, doubling the prior sequence size with each realloc and tracking both size and capacity would reduce your number of allocations from n to log(n). However, for the basic understanding of inline sequence growth, this example should suffice.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
-1
#include <stdio.h>
#include <stdlib.h>
main() {
    int size=1;
    int *marks = NULL;
    int i;
    printf("Enter marks\n");
    for(i=0;i<size;i++) 
    {
        int *tmpptr = realloc(marks, size * sizeof(*mark));

        if(!tmpptr)
        {
            printf("Memeory allocation error\n");
            free(marks);
            break;
        }
        marks = tmpptr;
        if(scanf("%d",marks + i) != 1) break;
        size++;
    }
    /* some code */
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    If `i` starts at 0 and `size` starts at 1, and both are incremented on each iteration of the loop, when does the loop condition `i < size` evaluate to false? – Jonathan Leffler Aug 09 '18 at 17:36
  • Never :) Maybe that was the OPs intention as I have not found any loop breaking idea. But this part is his idea - I do not know if hew wants to break the loop at all. – 0___________ Aug 09 '18 at 17:41
  • Loop is never going to end. Maybe bad example for beginners. – kiran Biradar Aug 09 '18 at 18:02
  • 1
    @kiranBiradar do not be so quick in judging me as the code your answer do not even check the `malloc` result - which an **extremely bad and dangerous example** for beginners – 0___________ Aug 09 '18 at 18:09