Im trying to create a array as a pointer using a function, and assigning random integers to that array.
But inside the for loop i am getting the following warning message, on equals sign:
Warning Here ↓↓↓ - Incompatible integer to pointer conversion.
array[index] = rand() % 100;
incompatible integer to pointer conversion assigning to 'int *' from 'int'
And when i execute, i get the following error:
signal: segmentation fault (core dumped)
Below is my code, with lots of comments to specify what is happening.
#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;
}
If i change array[index] = rand() % 100; to *array[index] = rand() % 100;, the warning goes away but i get illegal instruction error when executing.
What i want to achieve: Pass a array and its size as pointers using a function, then print its elements from another function using those pointers.
Please, can someone help me? Thank you!
CodePudding user response:
Couple of things wrong,
scanf("%ld", &*size)
, the two operators&*
cancel each other, so&*size
is just redundant.scanf("%ld", size)
is enough. (If you are allowed to use, the standard format specifier forsize_t
is%zu
).- You also need to check the return value of
scanf
since you are using the variable to allocate space for your array, it's crucial that the conversion succeeded. - You never allocate memory for
array
. - You are trying to store an
int
to anint *
variable.
Since array
is of type int **
a single dereference array[index]
yields a pointer to int
array[index] --> *(array index)
*array[index]
won't work either because of operator precedence
([] operator will be evaluated first and the result will be dereferenced)
What you want is (*array)[index] = rand() % 100;
or array[0][index] = rand() % 100;
or even *(*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;
}
I also noticed that void *arrayElements(int array[], size_t size)
returns a void pointer. Since you don't actually return anything,
void arrayElements(int array[], size_t size)
should be enough.