-1

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.

Timʘtei
  • 753
  • 1
  • 8
  • 21

2 Answers2

2

I would consider with 2 options. One returns pointer to array and saves length as pointer and another returns length but accepts pointer to pointer as parameter.

Option 1: Return pointer of array and pass pointer to length parameter

This option will return pointer to allocated and filled array and length will be saved to pointer passed as parameter

int* readArray(int* length) {
    int* arr;
    //Scan for length
    scanf("%d", length);
    //Allocate memory
    arr = (int *)malloc(...);
    //Fill data
    ...
    return arr;
}

and I will use it like this:

int length;
int *pointer;

pointer = readArray(&length);
printArray(pointer, length);

Option 2: Pass pointer to pointer to array and return length of array

int readArray(int ** ar) {
    int length;
    ....
    *ar = (int *)malloc(...);
    ...
    return length;
}

Usage:

int* ar;
int length;

length = readArray(&ar);
printArray(ar, length);

I would go with this option.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
1

You actually want this:

int readArray(int **ar) {
  int i, length;
  scanf("%d", &length);
  *ar = (int *)malloc(length * sizeof(int));
  for (i = 0; i < length; i++) {
    scanf("%d", &(*ar)[i]);
  }
  return length;
}

void printArray(int *pointer, int length) {
  int i;
  for (i = 0; i < length; i++) {
    printf("%d ", *(pointer + i));
  }
}

int main() {
  int *ar, length;
  length = readArray(&ar);
  printArray(ar, length);
  return 0;
}

Variables are passed by value in C, included pointers. With your solution:

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

ar is a local variable to readArray and pointer will never get modified in main.

Consider this:

void foo(int bar)
{
  bar = 123;
}

...
int a = 1;
foo(a);
// a won't contain 123 here
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • This `&(*ar)[i]` confused me. I do not understand this. – Timʘtei May 10 '17 at 13:45
  • 1
    @Timotei `ar` is a pointer to a pointer to `int`. `*ar` is a pointer to `int`, actually here it is the pointer to the `length` `int`s allocated by `malloc`. `(*ar)[n]` is the nth `int` of the `int`s mentioned before. `&(*ar)[n]` is the _pointer_ to the nth `int`. – Jabberwocky May 10 '17 at 13:52
  • How would you write a function to read the array in the exactly the same way you did in the above comment and to print the array(in the same function). – Timʘtei May 10 '17 at 20:01
  • @Timotei not quite sure I understood your question. Maybe you should post another question. – Jabberwocky May 11 '17 at 06:49
  • I mean how would the function void printArray(int **pointer, int length) look? Note de the **pointer not *pointer. – Timʘtei May 11 '17 at 08:36