Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "int *a[5]" and int(*a)[5]"? [duplicate]

Would they work differently on C and C++?

like image 491
Raghav Sharma Avatar asked Sep 11 '14 14:09

Raghav Sharma


People also ask

What is the difference between int * A and int A 5 and int *[ 5 ]?

4 Answers. Show activity on this post. of type integer; Each member of the array can hold the address of an integer. int (*a)[5] - Here "a" is a pointer to the array of 5 integers, in other words "a" points to an array that holds 5 integers.

What is the difference between a 5 and a == 5 when do we use in Python programming?

a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left. There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5 .

What is the difference between a 5 and a =- 5 in C ++?

So it's cleared now, ,both are not same, = is an Assignment Operator it is used to assign the value of variable or expression, while ==is an Equal to Operator and it is a relation operator used for comparison (to compare value of both left and right side operands).


4 Answers

  1. int *a[5] - It means that "a" is an array of pointers i.e. each member in the array "a" is a pointer
    of type integer; Each member of the array can hold the address of an integer.

  2. int (*a)[5] - Here "a" is a pointer to the array of 5 integers, in other words "a" points to an array that holds 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;
   }
like image 124
Abhishek Choubey Avatar answered Oct 18 '22 10:10

Abhishek Choubey


Yes, theres a huge difference. Let see some code:

#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
}   

What does that mean?

First case (int *a[5]) is array of 5 pointers to int

and second (int(*b)[5]) is pointer to array of 5 ints.

The difference makes here ().

And yes, the behavior is same in C and C++.

like image 36
Dawid Avatar answered Oct 18 '22 10:10

Dawid


int* a[5] declare a as array 5 of pointer to int

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

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

like image 38
Sunil Bojanapally Avatar answered Oct 18 '22 10:10

Sunil Bojanapally


This is an array of 5 pointers to int:

int* a[5];

This is a pointer to an array of 5 ints:

int (*a)[5];

Here's an example of how you could initialize the pointer or the elements of the array of pointers:

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;

Note that although I have used C++ in the example, this applies to C and C++.

like image 44
juanchopanza Avatar answered Oct 18 '22 08:10

juanchopanza