簡體   English   中英

C代碼可在Windows(使用Visual Studio編譯)上運行,但不能在Linux(使用gcc)上編譯

[英]C code works on Windows (compiled with Visual Studio) but won't compile on Linux (using gcc)

我目前正在嘗試為學校創建一個非常簡單的C程序,該程序創建m個整數n的數組(均由用戶輸入定義),並返回該數組的起始位置,或者返回錯誤消息(如果該數組可以)未創建。 當使用Visual Studio進行編譯時,它工作得很好,但是當我嘗試使用gcc對其進行編譯時,它會拋出大量錯誤消息,而我根本不知道是什么導致了這些錯誤消息。

源代碼:

#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;
}

來自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

看到奇怪的字符“ânot”,我懷疑您正在使用錯誤的文件編碼。 嘗試將代碼粘貼到Linux文件編輯器中,然后將其從該編輯器保存到新文件中。 嘗試編譯。

您的代碼與您發布的代碼完全一樣,並通過我的gcc生成了這些消息

6398652.c:4:5: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
6398652.c: In function ‘main’:
6398652.c:4:5: error: old-style function definition [-Werror=old-style-definition]
6398652.c:18:3: error: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int *’ [-Werror=format]
6398652.c: In function ‘create_array’:
6398652.c:25:3: error: implicit declaration of function ‘malloc’ [-Werror=implicit-function-declaration]
6398652.c:25:21: error: incompatible implicit declaration of built-in function ‘malloc’ [-Werror]
cc1: all warnings being treated as errors

此更改的版本可以干凈地編譯

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

int *create_array(int n, int initial_value);

int main(void) {
  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\n");
  } else {
    printf("Array stored in this location: %p\n", (void*)arr);
  }
  return 0;
}

int *create_array(int n, int initial_value) {
  int *pointer;
  int i;

  pointer = malloc(10 * sizeof *pointer);

  for (i = 0; i < n; i++) {
    int *p;
    p = pointer;
    p += n*(sizeof *p);
    *p = initial_value;
  }

  return pointer;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM