简体   繁体   中英

What is meant by: int (*b)[100]

What is meant by: int (*b)[100] , and what is the clarification of this code to produce this result?

#include <iostream>
void foo(int  a[100])
{
    a [1]= 30 ;
    std::cout<<a[1]<< std::endl;
}

void boo(int a[100])
{ 
    std::cout<<a[1]<< std::endl; 
}

int main()
{
    int (*b)[100] = (int (*) [100]) malloc(100 * sizeof(int));
    *b[1] = 20;
    std::cout<< *b[1] << "\t" << b[1] << std::endl;
    foo(*b);
    boo(*b);
    std::cout<< *b[1] << "\t" << b[1] << std::endl;
}

The above code outputs:

20  0x44ba430
30
30
20  0x44ba430
  • int (*b)[100] is an array pointer, a pointer that can point to an array of 100 int.
  • *b[1] = 20; is a severe operator precedence bug which reads beyond the bounds of the array. It should have been (*b)[1] .

int (*b)[100] is a pointer to an int array of 100 elements. The variable b points to allocated memory, which is allocated by malloc: malloc(100 * sizeof(int)); , but this allocates only one row.

The problem is in the next line: *b[1] = 20; , this is identical to b[1][0] , which is out of bounds of the array. This causes undefined behavior in the program.

int foo[100]; declares foo as an array of 100 ints . The type of foo is int [100] (just remove foo from its declaration).

int (*b)[100]; declares *b as an array of 100 ints . And that implies that b is a pointer to such array. Thus, you can write b = &foo; for example. The type of b is int (*)[100] (again, just remove b from its declaration).

But just pointing to an array like in the example above is not very interesting because a pointer to the first element of foo (ie a pointer to int) is sufficient to access foo . No need for a pointer to array. Instead, b is interesting to dynamically allocate an array of int [100] . For example:

b = (int (*) [100]) malloc(25 * sizeof(int [100])); // allocates an array of 25 int [100]
b[0][99] = 99; // correct
...
b[24][99] = 24099; // correct
b[24][100] = 24100; // incorrect because each b[i] is an array of 100 integers
b[25][0] = 25000; // incorrect because b is an array of 25 elements (an each element is an array)

Thus, your program is incorrect because you have allocated an array of one int [100] only (indeed, 100 * sizeof(int) == sizeof(int [100]) ). So you can write those instructions, which are identical:

b[0][0] = 0;
*(b[0]) = 0;
*b[0] = 0;

But *b[1] is incorrect.

There is a difference between int (*b)[100]; and int *b[100];

int (*b)[100]; // A pointer to an array of 100 integers.

int *b[100]; // An array of 100 int pointers.

int (*b)[100] declares b to be a pointer to an array of 100 integers.

The line with malloc allocates the memory to which pointer b points to. *b dereferences the pointer and thus gives us the recently allocated memory. b[1] is just an address, it can be rewritten as b + 1 . That address is 100 integers further from what b points to.

foo takes a pointer to int and assigns the value 30 to the second slot beginning from that pointer (read address). Both foo and boo just output what resides in that second slot from that pointer.

Try changing the last line to

std::cout<< *b[1] << "\\t" << b[0] << std::endl;

and see the difference of what address b[0] is and what address b[1] is, you will find they are separated by 100 integers. On my compiler, i see a difference of 0x190 that is 400 bytes (integer is 4 bytes and 100 integers make 400 bytes).

I prefer understanding weird statements by first typedef'ing them and then trying to relate and see.

For example,

typedef unsigned char uint8_t; // 8 bit memory
typedef uint8_t b[100]; // create an array b having 100 elements,
                        // where each element occupies 8 bit memory
typedef uint8_t *e;//create a pointer that points to a 8 bit memory

main()
{
 b *c; //create a pointer c pointing to a type b-"array having 100 elements
       // & each element having 8 bit memory". This is equivalent to what 
       // you said - (*b)[100]. Like just moving the *
 b d;//create an array d having 100 elements of 8 bit memory
 e f[100];//create an array of 100 pointers pointing to an 8 bit memory
} 

This might also help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM