我試圖使用函式創建一個陣列作為指標,并將隨機整數分配給該陣列。
但是在 for 回圈中,我在等號上收到以下警告訊息:
Warning Here ↓↓↓ - Incompatible integer to pointer conversion.
array[index] = rand() % 100;
從“int”分配給“int *”的不兼容整數到指標轉換
當我執行時,我收到以下錯誤:
信號:分段錯誤(核心轉儲)
下面是我的代碼,有很多注釋來說明發生了什么。
#include <stdio.h>
#include <stdlib.h> // Lib for rand() to work
// Function: Create array and its lenght, save to a pointer.
int createArray(
int **array,
size_t *size) { // Initiate pointers of array and array size
printf("What will be the size of the array?: ");
scanf("%ld", &*size); // For formatting using "size_t" we use %ld
for (int index = 0; index != *size; index ) {
/*
* Warning Message on Equals Sign below: incompatible integer to pointer
* conversion assigning to 'int *' from 'int' */
array[index] = rand() % 100; // Equal sign is giving me a warning. (??)
}
printf("\n --- Array created. Pointers: *size, *array --- \n");
return 0;
}
// Function: Print elements of pointer array specifying its size from pointer.
void *arrayElements(int array[], size_t size) {
printf("\n --- Printing Elements of array --- \n");
// Loop through the array by incrementing value of i
for (int i = 0; i < size; i ) {
printf("%d ", array[i]);
}
}
int main(void) {
int *array; // Instance pointer (Persistent variable -- For remembering what it does)
size_t size;
createArray(&array, &size);
arrayElements(array, size);
return 0;
}
如果我改變array[index] = rand() % 100; to * array[index] = rand() % 100;,警告消失,但執行時出現非法指令錯誤。
我想要實作的目標:使用函式將陣列及其大小作為指標傳遞,然后使用這些指標從另一個函式列印其元素。
請問,有人可以幫我嗎?謝謝!
uj5u.com熱心網友回復:
幾件事情錯了,
scanf("%ld", &*size),這兩個運算子&*相互抵消,所以&*size只是多余的。scanf("%ld", size)足夠的。(如果允許使用,標準格式說明符size_t是%zu)。- 您還需要檢查回傳值,
scanf因為您正在使用該變數為陣列分配空間,因此轉換成功至關重要。 - 您永遠不會為
array. - 您正在嘗試將 an 存盤
int到int *變數中。
由于arrayis 型別int **,單個取消參考array[index]會產生一個指向 int 的指標
array[index] --> *(array index)
*array[index]由于運算子優先級,也不起作用
([] 運算子將首先被評估,結果將被取消參考)
你想要的是(*array)[index] = rand() % 100;
或者array[0][index] = rand() % 100;
甚至*(*array index) = rand() % 100;
int createArray(
int **array,
size_t *size)
{
// first make sure you didn't get NULL pointers
if (array == NULL || size == NULL) {
fprintf(stderr, "createArray: null pointer\n");
return 1;
}
// Initiate pointers of array and array size
printf("What will be the size of the array?: ");
if (scanf("%zu", size) != 1) { // conversion failed
fprintf(stderr, "scanf: error reading size\n");
return 1;
}
*array = malloc(*size * sizeof(int)); // dereference array pointer and allocate memory
// alternatively, you can use array[0] = ... which is the same
if (*array == NULL) { // allocation failed
fprintf(stderr, "malloc: error allocating memory for array\n");
return 1;
}
for (size_t index = 0; index < *size; index ) // use size_t since size is size_t
{
// dereference first and assign value to array[index]
(*array)[index] = rand() % 100;
}
printf("\n --- Array created. Pointers: *size, *array --- \n");
return 0;
}
我還注意到它void *arrayElements(int array[], size_t size)回傳一個 void 指標。因為你實際上沒有回傳任何東西,
void arrayElements(int array[], size_t size)應該足夠了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/527279.html
標籤:数组C指针
