Is int a pointer to an array of ints?

I gave some examples declaring int pointer

int *myInt; 

and then turn this pointer into an array

 myInt = (int*)malloc(1024); 

it checks

 myInt[0] = 5; cout << myInt[0]; // prints 5 myInt[1] = 7; cout << myInt[1]; // prints 7 

I thought the int pointer was a pointer to an int and never again anything. I know that pointers to strings just point to the first character of the string, but it looks like something similar happens here with an int array. But then, if we want, this is an array of ints, why not just create an array of int instead of a pointer to int?

By the way, I am wondering how this works in C not C ++. This is in the C ++ file, but the corresponding code is in C.

+4
source share
5 answers

There is an alternative syntax for accessing elements indicated by a pointer - square brackets. This syntax allows you to access data through pointers as if the pointer were an array (of course, pointers are not arrays). The expression a[i] is just an alternative form of notation *(a+i) * .

When you allocate dynamic storage and assign it to myInt , you can use the pointer as a dynamic array that can resize at runtime:

 myInt = malloc(1024*sizeof(int)); // You do not need a cast in C, only in C++ for (int i = 0 ; i != 1024 ; i++) { myInt[i] = i; // Use square bracket syntax } for (int i = 0 ; i != 1024 ; i++) { printf("%d ", *(myInt+i)); // Use the equivalent pointer syntax } 

<h / "> * By the way, commutativity + allows you to write 4[array] instead of array[4] ; do not do this!

+3
source

Is int an array pointer from ints?

No.

I thought the int pointer was a pointer to an int and never anything else

It is right. Pointers are pointers, arrays are arrays.

What confuses you is that pointers can point to the first element of arrays, and arrays can decay to pointers to their first element. And what's even more confusing: pointers have the same syntax for dereferencing and arithmetic of pointers, which are used to index arrays. Namely,

 ptr[i] 

equivalently

 *(ptr + i) 

If ptr - a pointer. Of course, similarly, arr[i] is the i th element of the arr array. The similarity arises from the general nature of pointers and arrays: they are both used for indirect access (potentially blocks) of memory.

The consequence of this strong relationship is that in some situations (and with some restrictions ) arrays and pointers can be used as if they were interchangeable. This still does not mean that they are the same, but they have fairly common properties, so that their use is often "identical."

+10
source

Sort, and technically not. The int pointer points to int. But the ints array is contiguous in memory, so the next int can be referenced using *(myInt+1) . The designation of the array myInt[1] equivalent to the fact that it uses the myInt pointer, adds 1 unit to it (int size) and refers to this new address.

So, in general, this is true:

 myInt[i] == *(myint + i) 

That way you can use the int pointer to access the array. Just be careful watching the '\0' character and stop.

+1
source

The int pointer is not an int array. But your bigger question, apparently, is the reason that both arrays and pointers are needed.

An array represents the actual storage in the data memory. Once this storage is allocated, it does not make a significant difference whether you are referencing data stored using array notation or pointer notation.

However, this storage can also be allocated without using array entries, which means that arrays are not necessary. The main advantage of arrays is the convenient distribution of small blocks of memory, i.e. int x[20] and a slightly more convenient notation is array[i] , not *(array+i) . Fortunately, this more convenient notation can be used regardless of whether the array came from an array declaration or just a pointer. (In fact, as soon as the array was allocated, its variable name from this point does not differ from the pointer, which was assigned to indicate the memory location of the first value in the array.)

Note that the compiler will complain if you try to directly allocate an oversized block of memory in the array.

Arrays:

  • represent the actual memory that is allocated
  • the name of the array variable is the same as the pointer that refers to the point in memory where the array begins (and the name of the variable + 1 matches the pointer that refers to the point in memory where the second element is an array (if it exists), etc. )
  • values ​​in an array can be accessed using an array entry of type array[i]

Pointers

  • is a place to store the location of something in memory.
  • may refer to memory allocated in the array
  • or it may refer to memory that was allocated to functions such as malloc
  • the value stored in the memory pointed to by the pointer can be obtained by dereferencing the pointer, i.e. *pointer .
  • since the name of the array is also a pointer, the first element of the array can be accessed through *array , the second element - * (array + 1), etc.
  • an integer can be added or subtracted to the pointer to create a new pointer pointing to other values ​​in the same block of memory that was allocated by your program. For example, array + 5 indicates the place in memory where the array of values ​​is stored [5].
  • the pointer may increase or decrease, indicating other values ​​with the same memory block.

In many situations, one notation will be more convenient than the other, so it’s very useful that both notations are available and are so easily interchangeable with each other.

+1
source

They do not match. Here is the apparent difference.

 int array[10]; int *pointer; printf ("Size of array = %d\nSize of pointer = %d\n", sizeof (array), sizeof (pointer)); 

The result is

 Size of array = 40 Size of pointer = 4 

If you make "array + 1", the resulting address will be the address of the array [0] + 40. If you make "pointer + 1", the resulting address will be the address of the pointer [0] + 4.

Declaring an array allocates compile-time memory. Declaring a pointer does not allocate compile-time memory, and dynamic allocation is required using calloc () or malloc ()

When you perform the following assignment, it is actually an implicit type of casting an integer array to an integer pointer.

 pointer = array; 
+1
source

Source: https://habr.com/ru/post/1487527/


All Articles