0

I am allocating memory as follows. Also trying to update it in another function.

int main() {
 int* ptr = (int*)malloc(sizeof(int)*3);
 ptr[0] = 0;
 ptr[1] = 1;
 ptr[2] = 2;
 modifyArr(&ptr, 2);
}
void modifyArr(int** arr, int arrLen)
{
 printf("Before modified\n");
 printArray(*arr, arrLen);
 for (int i = arrLen; i >= 0; i--)
 {
 *arr[i] = i; // here is error
 }
 printf("After modified\n");
 printArray(*arr, arrLen);
}

So how can I modify this array inside another function?

In case my array would be fixed array as:

int arr[] = { 0,1,2 };

How can I update it in another function?

lurker
58.5k9 gold badges74 silver badges108 bronze badges
asked Mar 9, 2018 at 17:43
4
  • Do you want to modify the array contents, or do you want to reallocate the array (so you can change its size)? You are passing pointer to pointer, which allows you to do the latter (change value ptr to point to reallocated buffer). If you just want to change array contents, it's enough to pass the pointer value. Commented Mar 9, 2018 at 17:45
  • 1
    *arr[i] doesn’t do what you want. Use (*arr)[i], probably. Commented Mar 9, 2018 at 17:47
  • You have one more level of pointer than you need. Just call modifyArr(ptr, 2); and declare modifyArr(int *arr, int arrLen) and write printArray(arr, arrLen), arr[i] = i, etc... You'd need to change printArray declaration/definition in that case as well. Commented Mar 9, 2018 at 17:48
  • ptr is already a pointer, you don't need to make a pointer to it, just pass it directly. Then you don't have to worry about the operator precedence. Commented Mar 9, 2018 at 17:51

2 Answers 2

1

The array subscript operator [] has higher precedence than the pointer dereference operator *. See the order of precedence of operators in C. As a result, this:

*arr[i] = i;

Really means:

*(arr[i]) = i;

Which means you're treating arr as an array of pointers instead of a pointer to an array. As a result, you end up writing to the wrong place and invoke undefined behavior.

You need to put parenthesis around *arr to get what you want:

(*arr)[i] = i;

However, since you're only updating the memory the pointer points to and not actually modifying the pointer, there's no need to pass the pointer's address. Just pass it in directly:

void modifyArr(int* arr, int arrLen)
{
 printf("Before modified\n");
 printArray(arr, arrLen);
 for (int i = arrLen; i >= 0; i--)
 {
 arr[i] = i;
 }
 printf("After modified\n");
 printArray(arr, arrLen);
}

And call it like this:

modifyArr(ptr, 2);

You probably also want to modify printArray to do the same.

answered Mar 9, 2018 at 17:48
Sign up to request clarification or add additional context in comments.

1 Comment

But you don't really need the extra pointer level at all. Just make arr a simple int *, and pass ptr to it, and just use arr[i]. You don't need to change the pointer value itself, just the stuff it's pointing to.
0

The things with pointers are pretty simple when you remember two things - what you want to get and how do you get it?

Here you want to get the consecutive elements memory of which you have allocated. And how do you get it? Yes you are so right using [] and * but then again there is one thing known as precedence. Precedence of [] is higher than *. So you wrote this *(arr[i]) where as you want (*arr)[i]. First get the pointer then get the elements using [] on it.

Here you have achieved what is known as undefined behavior. Because you have accessed something which is not allocated by you.

answered Mar 9, 2018 at 17:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.