What is the difference between "int * a [5]" and int (* a) [5] "?

Will they work differently in C and C ++?

Ps My first question is, and I'm noob programming, so please try to keep a simple and easy way :)

Thank you in advance!

+4
source share
4 answers
  • int *a[5] - This means that “a” is an array of pointers, that is, each member in the array “a” is an integer pointer; Each member of the array may contain an integer address.

  • int (*a)[5] - Here "a" is a pointer to an array of 5 integers, in other words, "a" refers to an array containing 5 integers.

Example:

#include<stdio.h>
   int main()
   {
           int b = 3;
           int c = 4;
           int *a[2] = {&b, &c}; // is same as ---int *a[] = {&b, &c} 
           printf("value pointed to by a[0] = %d, by a[1] = %d\n", *a[0], *a[1]);
           return 0;
   }
+9

, . :

#include <stdio.h>

int main()
{
    int *a[5]; // same as: int *(a[5]);
    int(*b)[5];

    printf("*a[5] vs (*b)[5] : %d vs %d", sizeof (a), sizeof(b));
    // *a[5] vs (*b)[5] : 20 vs 4
}   

?

(int * a [5]) - 5 int

(int (* b) [5]) 5 ints.

().

, C ++.

+5

5 int:

int* a[5];

5 ints:

int (*a)[5];

, :

int a[5] = {0, 1, 2, 3, 4};
int (*p)[5]; // pointer to array of 5 ints
int* b[5];   // array of 5 pointers to int

p = &a; // p points to a

for (int i = 0; i < 5; ++i)
  std::cout << (*p)[i] << " ";
std::cout << std::endl;

// make each element of b point to an element of a
for (int i = 0; i < 5; ++i)
  b[i] = &a[4-i];

for (int i = 0; i < 5; ++i)
  std::cout << b[i] << " ";
std::cout << std::endl;

, ++ , C ++.

+4
source

int* a[5]declare aas an array of 5 pointers to int

int (*a)[5]declare aas a pointer to array 5 of int

I suggest you use cdecl to understand such or any complex declarations.

+4
source

All Articles