1
void m() {
    char a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    char(*c)[3][3] = (char (*)[3][3])a;
    printf("%d\n", *c[0][0]);
}

For instance, in this function the variable a points to a location in memory with 9 integers in a row.

But what about c? Does c point to a location in memory which points to a location in memory that holds 9 integers in a row?

So, technically, is c a single layer pointer or a double layer pointer?

Shouldn't what I said above be true? How come when I execute the below function:

void m() {
    char a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    char(*c)[3][3] = (char (*)[3][3])a;
    printf("%d\n", *c[0][0]);
    printf("a...%p\nc...%p\n", a, c);
}

a and c both point to the same locations? Shouldn't c be a double layer pointer, and a be a pointer to a location in memory?

16
  • Oh, so then I shouldn't use it? Commented Aug 27, 2014 at 2:49
  • 2
    We're not saying you shouldn't use it. We're saying it's undefined behavior. If you're ok with it doing anything then you can use it. Just be prepared for it to crash, or order pizza, or give your credit card data to Russian hackers, or seemingly work correctly except it leaves a vulnerability silently sitting in your code for several years until someone discovers it and uses it to remove a few million dollars from your business, etc etc. You decide if it's worth it. We think it's probably not, and you should probably avoid it. Commented Aug 27, 2014 at 2:53
  • printf("%d\n", *c[0][0]); should be printf("%d\n", (*c)[0][0]); Commented Aug 27, 2014 at 3:16
  • Actually, UB understates things: it's actually wrong too. Dereferencing *c => 1, and evaluating 1[0][0] will most likely segfault. Commented Aug 27, 2014 at 5:43
  • 2
    @BLUEPIXY: You're right of course, but my problem is that I'm finding both the comments here and the accepted answer very unsatisfactory. The standard says little about conversions between array and pointer to array, and I can find nothing to say this is undefined behaviour. Care to comment (or write a better answer)? Commented Aug 27, 2014 at 11:25

2 Answers 2

2

the variable a points to a location in memory with 9 integers in a row.

No. The variable a is a location in memory with 9 integers in a row. a is just a name for that location.

Does c point to a location in memory which points to a location in memory that holds 9 integers in a row?

No. c is a location in memory which points to a location in memory that holds 9 integers in a row. c is a name for the location containing the pointer.

So, technically, is c a single layer pointer or a double layer pointer?

Single.

Sign up to request clarification or add additional context in comments.

2 Comments

The type of c is pointer to array size 3 of array size 3 of char. Its value is the same as the pointer to array size 9 of char that array a decays into. Casting from one to the other is safe, but is said to be UB. No ints in sight. Care to extend your answer?
@david.pfx by "its value is the same" you mean that they store the same memory address, however they have different types
0

Take a look of these type definitions:

typedef char9[9];
typedef char3x3[3][3];

If we check the sizes:

cout<<sizeof(char9); 

result will be always 9, there is no alignments in char array.

cout<<sizeof(char3x3);

if it is equal to 9, it is safe to say the bytes ordered the same as the one dimension array. if it is larger than 9, I would say there is alignment between rows, so there are holes between the lines, and then you can't map a pointer of one type upon the other.

1 Comment

An array type describes a contiguously allocated nonempty set of objects with a particular member object type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.