I am currently trying to make a very simple C program for school that creates an array of m integer n (both of which are defined by user input) and either returns the location of the starts of the array or an error message if the array could not be created. It works perfectly well when compiled using Visual Studio, but when I tried to compile it using gcc it throws me a heap of error messages and I simply have no idea what is causing them.
Source code:
#include <stdio.h>
int *create_array(int n, int initial_value);
int main(){
int *arr;
int num;
int numOfNum;
printf("Store this integer:\n");
scanf("%d", &num);
printf("Store the integer this amount of time:\n");
scanf("%d", &numOfNum);
arr = create_array(num, 1);
if(arr == NULL) printf("ERROR");
else printf("Array stored in this location: %p", arr);
return 0;
}
int *create_array(int n, int initial_value){
int *pointer;
int i;
pointer = (int *) malloc(sizeof(int) * 10);
for(i = 0; i < n; i++){
int *p;
p = pointer;
p += n*(sizeof(int));
*p = initial_value;
}
return pointer;
}
Error from gcc:
q1.c: In function âmainâ:
q1.c:18: error: missing terminating " character
q1.c:19: error: ânotâ undeclared (first use in this function)
q1.c:19: error: (Each undeclared identifier is reported only once
q1.c:19: error: for each function it appears in.)
q1.c:19: error: expected â)â before âbeâ
q1.c:19: error: missing terminating " character
q1.c:20: error: missing terminating " character
q1.c:21: error: missing terminating " character
q1.c:39: error: expected declaration or statement at end of input
Seeing the weird characters “ânotâ” I suspect that you are using a bad file encoding. Try copy pasting the code into a linux file editor and save it from that editor into a new file. Try compiling that.