0

A bit fairly new to C, and was confused about this warning I'm receiving in this function:

void fillPrioritiesArr(int ** priorities, int arrSize) { 
  int currIdx = 0;
  while(currIdx < arrSize){
    priorities[currIdx]=malloc(sizeof(int));
    priorities[currIdx] = (int) rand() % 10;
    currIdx++;
  }
}

Specifically this line:

priorities[currIdx] = (int) rand() % 10;

Is it due because one is a pointer variable and the other is an int? Or is my understanding of this completely wrong?

1
  • Your understanding is correct, Commented Dec 17, 2018 at 12:07

3 Answers 3

0

try this

void fillPrioritiesArr(int ** priorities, int arrSize) { 
  int currIdx = 0;
  while(currIdx < arrSize){
    priorities[currIdx]=malloc(sizeof(int));
    *priorities[currIdx] = (int) rand() % 10;
    currIdx++;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the line

priorities[currIdx] = (int) rand() % 10;

you are trying to assign an int on the right-hand-side (rhs) of the the = to something that has type int* on the left-hand-side (lhs). The lhs is a variable that holds a pointer to a storage location where you can store an int. So, in order to write to that location, you first must dereference the pointer variable with *priorities[currIdx]. This tells the compiler: "Don't write to the variable itself but look up the location where it points and write there."

This is similar to what you do when you say priorities[currIdx] in the first place. The variable priorities is an int**. That's a pointer to a pointer to an int. You don't want to override the variable itself but you want to write the location it points to (which has type int*) with a fresh pointer. In that case you dereference saying priorities[currIdx] which is equivalent to *(priorities + currIdx).


However, allocating an extra int for each slot of the array is not only unneccessary and cumbersome, it also makes your code much slower. You can have an array of ints directly, by simply passing int* priorities. Then you can just drop the malloc line alltogether and write to the array as you do in your question originally. That is, without dereferencing:

priorities[currIdx] = (int) rand() % 10;

because priorities[currIdx] will then have type int.

Comments

-1

priorities [currIdx] is of integer pointer type. You can't assign int to address.

Do this:

int temp = (int) rand()%10;

memcpy(priorities [currIdx], &temp, sizeof(int);

5 Comments

Why not just *priorities[currIdx] = (int) rand() % 10?
I tried both *priorities[currIdx] = (int) rand() % 10 and priorities[currIdx] += (int) rand() % 10 , but both doens't give the values 0-10 instead a way larger number.
@Hyun how are you calling fillPrioritiesArr? Is it priorities is pointing to valid memory?
@kiranBiradar int ** priorities = malloc(sizeof(int *) * ARR_SIZE); fillPrioritiesArr(priorities, ARR_SIZE);
@kiranBiradar Ah, wow I feel like an idiot. Fixed it now (: Was outputting priorities[idx] which should've been *priorities[idx]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.