1

When I use a different compiler (i.e. Visual Studio), I can't declare an Array without a constant argument. I know that when declaring an Array without a constant, I must use a pointer. On the other hand, the CS50 IDE compiler doesn't have this problem. Why does this happen?

My code is below:

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

int main(void)
{
     int n;
     printf("Enter the Number: ");
     
     n = GetInt();
     
     int age[n];
     
     for (int i=0; i<n; i++)
     {
        printf("Enter #%i Value: ", i+1);
        age[i] = GetInt();
     }
     
    printf("\nYour Entered Sequence is: ");
    
     for (int i=0; i<n; i++)
     {
        printf(" %i", age[i]);
     }
     
     printf("\n");

    
    return 0;
}
1

If you are coding this in Visual Studio, you cannot use GetInt() to get an integer. This is because this function is only in the CS50 library. You may have included the proper header file on the top, but the file does not exist in the Visual Studio library files.

scanf() is a possible alternative. You can use:

scanf("%d",n);

Instead of:

n = GetInt();
1
  • I have the same issue, it's not about using GetInt, it's about initialising an array using a non-const value for its size. The CS50 IDE will allow int qty =3; int myArray[qty]; whereas MSVS will insist qty needs to be const Oct 26 '20 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .