What happens when you add an integer value to an array? [duplicate] - c++

This question already has answers here:
C/C++: Pointer Arithmetic
(7 answers)
Closed 3 years ago.
In many posts I have seen answers like
std::sort(arr1, arr1 + size1);
I do not understand what is happening at arr1 + size1 where an array is being added with an integer, is it concatenation, or is it adding size1 to each element of the array? Neither of them seem like a logical thing required to sort an array. I tried printing the result but, it isn't possible to print arrays in C++. Could someone please explain what is going on here?

It's pointer arithmetic. arr1 is a pointer to the beginning of the array, and arr1 + size1 points to just beyond the end of the array.

It's adding the integer to the address of the array (the address of the first element). Yielding an address (if the arithmetic is correct) to an element in the array.

Related

Dynamic memory allocaion in c++ [duplicate]

This question already has answers here:
What does "new int(100)" do?
(4 answers)
Closed 1 year ago.
I came across two similar statements but could not exactly find the diference between them. The statements are:
int *p = new int(75);
int *p = new int[75];
Can anyone help me in knowing the difference between the above two statements.
The first returns a pointer to the integer with value of 75, but the second returns a pointer to the array of 75 integers, which don't have a value.

pArray = pArray + 1; Can someone explain to me how this increments the array in C++? [duplicate]

This question already has answers here:
Why a pointer + 1 add 4 actually
(6 answers)
Closed 5 years ago.
I have been learning about using pointers with arrays. If I was to write this simple code:
string Array[3] = {"one", "two", "three"};
string *pArray = Array;
pArray = pArray + 1;
std::cout << *pArray << std::endl;
The output is 'two'
Can someone explain to me the workings of this? Why does the +1 change the position of the pointer to where "two" is in the array? I'd expect the +1 to be concatenated onto the end of the string pointer so I'd end up with something like '0x61feb01'.
How does the compiler know to increment the array and not just add a 1 on to the end of the pointer string memory location?
Why is adding an int to a string different here?
Thanks.
I think when you add "1" to your pointer, this "1" is not treated as an integer number in the process of addition. Notice that pointer is an address, now, when you add 1 to your pointer it becomes: the address it currently is+ONE COMPLETE SIZE of a string type. So for example if your string type takes 8 Bytes you're moving 8 Bytes forward in the memory and not 1 Bytes!
As to why "two" printed you should know that array elements are held in continuous groups of bytes in the memory thus when you add to the address of one of the elements you can get to the other elements. Array names are nothing but pointers themselves except that they are fixed pointers(you can't change their addresses). E.g. In (int myArr[10]) the name "myArr" is a pointer that points to the(has the address of the) first part in the ram that holds the first element and then using pointer arithmetic just as you've done in the above example of yours you can access the rest of the elements either.
As an ending note, these two are equal in this example array: (myArr[i]==*(myArr+i)), if you put 0 here instead of i, you get (myArr[0]==*myArr) which is exactly what I said earlier. I hope it helps.

C++ - Arrays of the integer type [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 8 years ago.
I was just curious to know how to find the number of elements of an array of integers.
For character arrays we can loop thorough the array till we reach the null character,but how can I do it for integer array?
#include <iostream.h>
void main()
{
int a[] = {1,2,3,4};
for ( k = 0 ; a[k] ; k++)
cout<<k<<endl;
}
The above code counts from 0 to 8.
-A C++ noob with an open mind
A char array is terminated by 0 by convention. Such an array is called a C-style string, because it's used as a string of characters.
For integers, there is no termination value by convention, and you need to know the length by some other means. If it's your own array, store the length in a variable. If you receive the array from an API, there will be typically be a parameter receiving the length of the array, that you can use.
If the array is a global, static, or automatic variable (int array[10];), then sizeof(array)/sizeof(array[0]) works.
If it is a dynamically allocated array (int* array = malloc(sizeof(int)*10);) or passed as a function argument (void f(int array[])), then you cannot find its size at run-time. You will have to store the size somewhere.
Note that sizeof(array)/sizeof(array[0]) compiles just fine even for the second case, but it will silently produce the wrong result.

sizeof works unusual at c++ after array creation [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
I am creating an array by using following code
float *A;
A = (float *) malloc(100*sizeof(float));
float *B;
B = (float *) malloc(100*sizeof(float));
but after these when I type an print the size of the A and B by the following, I get 2 as a result as I expect to see 100.
sizeof(A)/sizeof(float)
Your expectation is wrong. A is a float*, so its size will be sizeof(float*), regardless of how you actually allocate it.
If you had a static array - i.e. float A[100], then this would work.
Since this is C++, use std::array or std::vector.
Worst case, use new[]. Definitely don't use malloc.
This works only for static arrays, defined in the current scope.
All you get in your example is the size of a pointer to float divided by the size of float.

length of dynamic array in c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
I declared a dynamic array like this:
int *arr = new int[n]; //n is entered by user
Then used this to find length of array:
int len = sizeof(arr)/sizeof(int);
It gives len as 1 instead of n . Why is it so?
Because sizeof does not work for dynamic arrays. It gives you the size of pointer, since int *arr is a pointer
You should store the size of allocated array or better use std::vector
Because arr is not an array, but a pointer, and you are running on an architecture where size of pointer is equal to the size of int.
Andrew is right.
You have to save n somewhere (depends on where do you use it). Or if you are using .NET you could use Array or List...