I have to write function to read the array length and the array and make that array able to be used in other functions.
I have read many things about pointers but I didn`t find an answer to my question. Here is my code.
int readArray(int *ar, int *pointer){
int i, length;
scanf("%d", &length);
ar = (int *) malloc(length * sizeof(int));
for(i = 0; i < length; i++){
scanf("%d", pointer + i);
}
return length;
}
void printArray(int *pointer, int length){
int i;
for(i = 0; i < length; i++){
printf("%d ", *(pointer + i));
}
}
int main(){
int *pointer, *ar, length;
pointer = ar[0];//Here I get the warnings.
length = readArray(ar, pointer);
printArray(pointer, length);
return 0;
}
The warnings in codeblocks:
warning: assignment makes pointer from integer without a cast
and
warning: 'ar' is used uninitialized in this function [-Wuninitialized]|
.
This question is different from this Dynamic memory access only works inside function because i have to read the array length in the readArray function. And for me, as a begginer, only little difference is a big difference.