Sign in to follow this  

C++ nooby confused with Pointer syntax and arrays

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

void arrayCopy(int *arr1 , int array1Size , int *arr2 , int arr2size){
	for(int i = 0 ; i < array1Size - 1 && i < arr2size - 1 ; i++){
		arr1 + i = arr2 + i;// problem here
	}
	
}

The function just copies the array from arr2 pointer to the array pointed to by the arr1 pointer. I am confused about the syntax for this and I've tried a couple of ways but none have pleased the compiler. I know that my logic is probably pretty screwed up but my goal is just to get this to compile and then I can work on that later. Could someone tell me what the proper syntax for manipulating arrays and pointers like this is. Thanks all.

Share this post


Link to post
Share on other sites
Quote:

for(int i = 0 ; i < array1Size - 1 && i < arr2size - 1 ; i++)


You don't want or need to subtract 1 from the array sizes here. Array indices are zero based, which means valid indices are from 0 to arraySize - 1, but checking that i is strictly less than arraySize will accommodate for that.

Also:

std::copy (arr2, arr2 + arr2Size, arr1); //this works is arr1size >= arr2Size


Share this post


Link to post
Share on other sites
I think I see you confusion arrays and pointers work in much the same way as they point to a set size of memory so you can set a pointer to to point to an array.

the mistake your making is there is no such varible as arr1 but there is an arr[x] where the x points to a single value in the array now you can manipulate x to retrieve the element you want or you can set a pointer to the array that can be manipulated.

The point:
arr[x] in this form you need to change the value of x.
*arr in this form you can manipulate *arr.

the best one for your problem is arr1[i] = arr2[i]

I tend to stick to arr[] format and use passing by reference over actual pointer notation (*arr) as I find it easier to work with. if you struggle you can always change the scope of a variable so it does not need to be passed to the function but this is not good practice and is a habit you then need to go through thee trouble of breaking once you become more proficient.

Share this post


Link to post
Share on other sites
Quote:
Original post by jdub
*** Source Snippet Removed ***

The function just copies the array from arr2 pointer to the array pointed to by the arr1 pointer. I am confused about the syntax for this and I've tried a couple of ways but none have pleased the compiler. I know that my logic is probably pretty screwed up but my goal is just to get this to compile and then I can work on that later. Could someone tell me what the proper syntax for manipulating arrays and pointers like this is. Thanks all.


Adding to a pointer gives you a pointer. Assigning to a pointer just changes where it points, just like assigning to any other value. To copy data in memory, you need to assign the pointed-at data, which implies dereferencing the pointers.

However, pointer arithmetic is equivalent to array indexing, and it's usual to use whichever form is more readable at the moment (usually the array index). Note that pointers and arrays, themselves, are totally different things.

Share this post


Link to post
Share on other sites

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this