(arr + 2) is equivalent to *(arr + 2) . How?
I am studying how to display elements of 2D array with the help of pointers. Here is the code I tried:
#include<stdio.h>
int main()
int arr[3][2] =
7, 8,
6,3,
3,4
;
printf("%un", (arr + 2));
printf("%un", *(arr + 2));
Output:
6487616
6487616
I am expecting output of *(arr + 2)
to be 3
. How is it the same as (arr + 2)
?
c arrays pointers
|
show 5 more comments
I am studying how to display elements of 2D array with the help of pointers. Here is the code I tried:
#include<stdio.h>
int main()
int arr[3][2] =
7, 8,
6,3,
3,4
;
printf("%un", (arr + 2));
printf("%un", *(arr + 2));
Output:
6487616
6487616
I am expecting output of *(arr + 2)
to be 3
. How is it the same as (arr + 2)
?
c arrays pointers
3
You need to dereference it twice, since it's a 2D array:*(*(arr+2))
– Govind Parmar
Aug 28 '18 at 16:42
4
arr[2]
and*(arr + 2)
are the same by definition in the language (C11 §6.5.2.1 Array subscripting ¶2). And you get odd results because you're printing an address, not the value of a cell in the array. That's officially undefined behaviour; you pass an address toprintf()
but tell it that it's got an unsigned integer to work on. You might be OK on a 32-bit platform, sort of, more or less. Results are not remotely reliable on a 64-bit platform.
– Jonathan Leffler
Aug 28 '18 at 16:45
1
technically you're getting UB since%u
is not for printing pointers. You have to use%zu
– phuclv
Aug 28 '18 at 16:50
7
@phuclv: You're right that%u
is UB, but so is using%zu
(that's for printingsize_t
). You should either use%p
and cast the pointer to avoid *
, or use<inttypes.h>
andPRIuPTR
(orPRIXPTR
for upper-case hex) plus a cast touintptr_t
.
– Jonathan Leffler
Aug 28 '18 at 16:52
1
@phuclv : False.%zu
requires asize_t
argument. Any incompatible type will trigger undefined behavior. Pointer arguments will trigger undefined behavior.
– AnT
Aug 28 '18 at 17:04
|
show 5 more comments
I am studying how to display elements of 2D array with the help of pointers. Here is the code I tried:
#include<stdio.h>
int main()
int arr[3][2] =
7, 8,
6,3,
3,4
;
printf("%un", (arr + 2));
printf("%un", *(arr + 2));
Output:
6487616
6487616
I am expecting output of *(arr + 2)
to be 3
. How is it the same as (arr + 2)
?
c arrays pointers
I am studying how to display elements of 2D array with the help of pointers. Here is the code I tried:
#include<stdio.h>
int main()
int arr[3][2] =
7, 8,
6,3,
3,4
;
printf("%un", (arr + 2));
printf("%un", *(arr + 2));
Output:
6487616
6487616
I am expecting output of *(arr + 2)
to be 3
. How is it the same as (arr + 2)
?
c arrays pointers
c arrays pointers
edited Aug 28 '18 at 16:42
John Kugelman
247k54406460
247k54406460
asked Aug 28 '18 at 16:40
davedave
896
896
3
You need to dereference it twice, since it's a 2D array:*(*(arr+2))
– Govind Parmar
Aug 28 '18 at 16:42
4
arr[2]
and*(arr + 2)
are the same by definition in the language (C11 §6.5.2.1 Array subscripting ¶2). And you get odd results because you're printing an address, not the value of a cell in the array. That's officially undefined behaviour; you pass an address toprintf()
but tell it that it's got an unsigned integer to work on. You might be OK on a 32-bit platform, sort of, more or less. Results are not remotely reliable on a 64-bit platform.
– Jonathan Leffler
Aug 28 '18 at 16:45
1
technically you're getting UB since%u
is not for printing pointers. You have to use%zu
– phuclv
Aug 28 '18 at 16:50
7
@phuclv: You're right that%u
is UB, but so is using%zu
(that's for printingsize_t
). You should either use%p
and cast the pointer to avoid *
, or use<inttypes.h>
andPRIuPTR
(orPRIXPTR
for upper-case hex) plus a cast touintptr_t
.
– Jonathan Leffler
Aug 28 '18 at 16:52
1
@phuclv : False.%zu
requires asize_t
argument. Any incompatible type will trigger undefined behavior. Pointer arguments will trigger undefined behavior.
– AnT
Aug 28 '18 at 17:04
|
show 5 more comments
3
You need to dereference it twice, since it's a 2D array:*(*(arr+2))
– Govind Parmar
Aug 28 '18 at 16:42
4
arr[2]
and*(arr + 2)
are the same by definition in the language (C11 §6.5.2.1 Array subscripting ¶2). And you get odd results because you're printing an address, not the value of a cell in the array. That's officially undefined behaviour; you pass an address toprintf()
but tell it that it's got an unsigned integer to work on. You might be OK on a 32-bit platform, sort of, more or less. Results are not remotely reliable on a 64-bit platform.
– Jonathan Leffler
Aug 28 '18 at 16:45
1
technically you're getting UB since%u
is not for printing pointers. You have to use%zu
– phuclv
Aug 28 '18 at 16:50
7
@phuclv: You're right that%u
is UB, but so is using%zu
(that's for printingsize_t
). You should either use%p
and cast the pointer to avoid *
, or use<inttypes.h>
andPRIuPTR
(orPRIXPTR
for upper-case hex) plus a cast touintptr_t
.
– Jonathan Leffler
Aug 28 '18 at 16:52
1
@phuclv : False.%zu
requires asize_t
argument. Any incompatible type will trigger undefined behavior. Pointer arguments will trigger undefined behavior.
– AnT
Aug 28 '18 at 17:04
3
3
You need to dereference it twice, since it's a 2D array:
*(*(arr+2))
– Govind Parmar
Aug 28 '18 at 16:42
You need to dereference it twice, since it's a 2D array:
*(*(arr+2))
– Govind Parmar
Aug 28 '18 at 16:42
4
4
arr[2]
and *(arr + 2)
are the same by definition in the language (C11 §6.5.2.1 Array subscripting ¶2). And you get odd results because you're printing an address, not the value of a cell in the array. That's officially undefined behaviour; you pass an address to printf()
but tell it that it's got an unsigned integer to work on. You might be OK on a 32-bit platform, sort of, more or less. Results are not remotely reliable on a 64-bit platform.– Jonathan Leffler
Aug 28 '18 at 16:45
arr[2]
and *(arr + 2)
are the same by definition in the language (C11 §6.5.2.1 Array subscripting ¶2). And you get odd results because you're printing an address, not the value of a cell in the array. That's officially undefined behaviour; you pass an address to printf()
but tell it that it's got an unsigned integer to work on. You might be OK on a 32-bit platform, sort of, more or less. Results are not remotely reliable on a 64-bit platform.– Jonathan Leffler
Aug 28 '18 at 16:45
1
1
technically you're getting UB since
%u
is not for printing pointers. You have to use %zu
– phuclv
Aug 28 '18 at 16:50
technically you're getting UB since
%u
is not for printing pointers. You have to use %zu
– phuclv
Aug 28 '18 at 16:50
7
7
@phuclv: You're right that
%u
is UB, but so is using %zu
(that's for printing size_t
). You should either use %p
and cast the pointer to a void *
, or use <inttypes.h>
and PRIuPTR
(or PRIXPTR
for upper-case hex) plus a cast to uintptr_t
.– Jonathan Leffler
Aug 28 '18 at 16:52
@phuclv: You're right that
%u
is UB, but so is using %zu
(that's for printing size_t
). You should either use %p
and cast the pointer to a void *
, or use <inttypes.h>
and PRIuPTR
(or PRIXPTR
for upper-case hex) plus a cast to uintptr_t
.– Jonathan Leffler
Aug 28 '18 at 16:52
1
1
@phuclv : False.
%zu
requires a size_t
argument. Any incompatible type will trigger undefined behavior. Pointer arguments will trigger undefined behavior.– AnT
Aug 28 '18 at 17:04
@phuclv : False.
%zu
requires a size_t
argument. Any incompatible type will trigger undefined behavior. Pointer arguments will trigger undefined behavior.– AnT
Aug 28 '18 at 17:04
|
show 5 more comments
3 Answers
3
active
oldest
votes
A 2D array is really an array of arrays.
The expression arr + 2
has type int (*)[2]
, while *(arr + 2)
has type int [2]
. When printing the former, you have a pointer so that value of the pointer is printed. In the latter case, you have an array which decays to a pointer to the first element. So *(arr + 2)
decays into arr + 2
, which is the same as the first expression.
Going into more detail on arr + 2
, arr
has type int [3][2]
. When you add an integer value to it, it decays to a pointer to the first member, so arr
decays to type int (*)[2]
, and arr + 2
also has that type, and points to the subarray containing 3, 4
.
Also note that pointers should be printed with the %p
format specifier, and that the pointer must be casted to void *
, otherwise you invoke undefined behavior. In this case you were "lucky" that they happened to print the same thing.
To get the output of 3 you were expecting, you need to dereference one more time:
*(*(arr + 2))
*(*(arr + 2));
throws[Error] expected ')' before ';' token
but*(*(arr + 1) + 1));
prints3
– dave
Aug 29 '18 at 5:18
1
@dave*(*(arr + 2))
works properly for me. You're probably missing a closing brace for yourprintf
call. Also,*(*(arr + 1) + 1))
is choosing a different array element which happens to have the same value as the one you're attempting to print.arr[2][0]
andarr[1][1]
both contain the value 3.
– dbush
Aug 29 '18 at 11:21
add a comment |
arr
is an array of arrays of int
. On almost any use an array is converted to a pointer to its first element. So arr
gets converted to a pointer to an array of int
.
OK, arr
gets converted to a pointer to an array of int
, so (arr+2)
is of the same type, that is, a pointer to an array of int
.
Now *(arr+2)
is the thing (arr+2)
points to. That is, an array of int
.
Now since it's an array of int
, it gets converted to a pointer to its first element. So *(arr+2)
gets converted to a pointer to int
. Note it is not an int
and is unlikely to be equal to 3
.
Now how come (arr+2)
and *(arr+2)
dosplay the same? They are a pointer to an array and a pointer to its first element. Although these pointets are of different types, they represent the same address, because the address of any array is the same as the address of its first element.
add a comment |
arr
is the pointer to the first array of the type int[2]
.(arr + 2)
is the pointer to the third such array.
Whereas *(arr + 2)
is the pointer to the first element of the (arr +2)
array.
Both of these will hence have the same address since they are pointing to the same position.The only difference being in their type.(arr+2)
is of the type int(*)[2]
whereas *(arr + 2)
is of the type int *
.
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52062706%2farr-2-is-equivalent-to-arr-2-how%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
A 2D array is really an array of arrays.
The expression arr + 2
has type int (*)[2]
, while *(arr + 2)
has type int [2]
. When printing the former, you have a pointer so that value of the pointer is printed. In the latter case, you have an array which decays to a pointer to the first element. So *(arr + 2)
decays into arr + 2
, which is the same as the first expression.
Going into more detail on arr + 2
, arr
has type int [3][2]
. When you add an integer value to it, it decays to a pointer to the first member, so arr
decays to type int (*)[2]
, and arr + 2
also has that type, and points to the subarray containing 3, 4
.
Also note that pointers should be printed with the %p
format specifier, and that the pointer must be casted to void *
, otherwise you invoke undefined behavior. In this case you were "lucky" that they happened to print the same thing.
To get the output of 3 you were expecting, you need to dereference one more time:
*(*(arr + 2))
*(*(arr + 2));
throws[Error] expected ')' before ';' token
but*(*(arr + 1) + 1));
prints3
– dave
Aug 29 '18 at 5:18
1
@dave*(*(arr + 2))
works properly for me. You're probably missing a closing brace for yourprintf
call. Also,*(*(arr + 1) + 1))
is choosing a different array element which happens to have the same value as the one you're attempting to print.arr[2][0]
andarr[1][1]
both contain the value 3.
– dbush
Aug 29 '18 at 11:21
add a comment |
A 2D array is really an array of arrays.
The expression arr + 2
has type int (*)[2]
, while *(arr + 2)
has type int [2]
. When printing the former, you have a pointer so that value of the pointer is printed. In the latter case, you have an array which decays to a pointer to the first element. So *(arr + 2)
decays into arr + 2
, which is the same as the first expression.
Going into more detail on arr + 2
, arr
has type int [3][2]
. When you add an integer value to it, it decays to a pointer to the first member, so arr
decays to type int (*)[2]
, and arr + 2
also has that type, and points to the subarray containing 3, 4
.
Also note that pointers should be printed with the %p
format specifier, and that the pointer must be casted to void *
, otherwise you invoke undefined behavior. In this case you were "lucky" that they happened to print the same thing.
To get the output of 3 you were expecting, you need to dereference one more time:
*(*(arr + 2))
*(*(arr + 2));
throws[Error] expected ')' before ';' token
but*(*(arr + 1) + 1));
prints3
– dave
Aug 29 '18 at 5:18
1
@dave*(*(arr + 2))
works properly for me. You're probably missing a closing brace for yourprintf
call. Also,*(*(arr + 1) + 1))
is choosing a different array element which happens to have the same value as the one you're attempting to print.arr[2][0]
andarr[1][1]
both contain the value 3.
– dbush
Aug 29 '18 at 11:21
add a comment |
A 2D array is really an array of arrays.
The expression arr + 2
has type int (*)[2]
, while *(arr + 2)
has type int [2]
. When printing the former, you have a pointer so that value of the pointer is printed. In the latter case, you have an array which decays to a pointer to the first element. So *(arr + 2)
decays into arr + 2
, which is the same as the first expression.
Going into more detail on arr + 2
, arr
has type int [3][2]
. When you add an integer value to it, it decays to a pointer to the first member, so arr
decays to type int (*)[2]
, and arr + 2
also has that type, and points to the subarray containing 3, 4
.
Also note that pointers should be printed with the %p
format specifier, and that the pointer must be casted to void *
, otherwise you invoke undefined behavior. In this case you were "lucky" that they happened to print the same thing.
To get the output of 3 you were expecting, you need to dereference one more time:
*(*(arr + 2))
A 2D array is really an array of arrays.
The expression arr + 2
has type int (*)[2]
, while *(arr + 2)
has type int [2]
. When printing the former, you have a pointer so that value of the pointer is printed. In the latter case, you have an array which decays to a pointer to the first element. So *(arr + 2)
decays into arr + 2
, which is the same as the first expression.
Going into more detail on arr + 2
, arr
has type int [3][2]
. When you add an integer value to it, it decays to a pointer to the first member, so arr
decays to type int (*)[2]
, and arr + 2
also has that type, and points to the subarray containing 3, 4
.
Also note that pointers should be printed with the %p
format specifier, and that the pointer must be casted to void *
, otherwise you invoke undefined behavior. In this case you were "lucky" that they happened to print the same thing.
To get the output of 3 you were expecting, you need to dereference one more time:
*(*(arr + 2))
edited Aug 28 '18 at 17:06
answered Aug 28 '18 at 16:48
dbushdbush
103k13108145
103k13108145
*(*(arr + 2));
throws[Error] expected ')' before ';' token
but*(*(arr + 1) + 1));
prints3
– dave
Aug 29 '18 at 5:18
1
@dave*(*(arr + 2))
works properly for me. You're probably missing a closing brace for yourprintf
call. Also,*(*(arr + 1) + 1))
is choosing a different array element which happens to have the same value as the one you're attempting to print.arr[2][0]
andarr[1][1]
both contain the value 3.
– dbush
Aug 29 '18 at 11:21
add a comment |
*(*(arr + 2));
throws[Error] expected ')' before ';' token
but*(*(arr + 1) + 1));
prints3
– dave
Aug 29 '18 at 5:18
1
@dave*(*(arr + 2))
works properly for me. You're probably missing a closing brace for yourprintf
call. Also,*(*(arr + 1) + 1))
is choosing a different array element which happens to have the same value as the one you're attempting to print.arr[2][0]
andarr[1][1]
both contain the value 3.
– dbush
Aug 29 '18 at 11:21
*(*(arr + 2));
throws [Error] expected ')' before ';' token
but *(*(arr + 1) + 1));
prints 3
– dave
Aug 29 '18 at 5:18
*(*(arr + 2));
throws [Error] expected ')' before ';' token
but *(*(arr + 1) + 1));
prints 3
– dave
Aug 29 '18 at 5:18
1
1
@dave
*(*(arr + 2))
works properly for me. You're probably missing a closing brace for your printf
call. Also, *(*(arr + 1) + 1))
is choosing a different array element which happens to have the same value as the one you're attempting to print. arr[2][0]
and arr[1][1]
both contain the value 3.– dbush
Aug 29 '18 at 11:21
@dave
*(*(arr + 2))
works properly for me. You're probably missing a closing brace for your printf
call. Also, *(*(arr + 1) + 1))
is choosing a different array element which happens to have the same value as the one you're attempting to print. arr[2][0]
and arr[1][1]
both contain the value 3.– dbush
Aug 29 '18 at 11:21
add a comment |
arr
is an array of arrays of int
. On almost any use an array is converted to a pointer to its first element. So arr
gets converted to a pointer to an array of int
.
OK, arr
gets converted to a pointer to an array of int
, so (arr+2)
is of the same type, that is, a pointer to an array of int
.
Now *(arr+2)
is the thing (arr+2)
points to. That is, an array of int
.
Now since it's an array of int
, it gets converted to a pointer to its first element. So *(arr+2)
gets converted to a pointer to int
. Note it is not an int
and is unlikely to be equal to 3
.
Now how come (arr+2)
and *(arr+2)
dosplay the same? They are a pointer to an array and a pointer to its first element. Although these pointets are of different types, they represent the same address, because the address of any array is the same as the address of its first element.
add a comment |
arr
is an array of arrays of int
. On almost any use an array is converted to a pointer to its first element. So arr
gets converted to a pointer to an array of int
.
OK, arr
gets converted to a pointer to an array of int
, so (arr+2)
is of the same type, that is, a pointer to an array of int
.
Now *(arr+2)
is the thing (arr+2)
points to. That is, an array of int
.
Now since it's an array of int
, it gets converted to a pointer to its first element. So *(arr+2)
gets converted to a pointer to int
. Note it is not an int
and is unlikely to be equal to 3
.
Now how come (arr+2)
and *(arr+2)
dosplay the same? They are a pointer to an array and a pointer to its first element. Although these pointets are of different types, they represent the same address, because the address of any array is the same as the address of its first element.
add a comment |
arr
is an array of arrays of int
. On almost any use an array is converted to a pointer to its first element. So arr
gets converted to a pointer to an array of int
.
OK, arr
gets converted to a pointer to an array of int
, so (arr+2)
is of the same type, that is, a pointer to an array of int
.
Now *(arr+2)
is the thing (arr+2)
points to. That is, an array of int
.
Now since it's an array of int
, it gets converted to a pointer to its first element. So *(arr+2)
gets converted to a pointer to int
. Note it is not an int
and is unlikely to be equal to 3
.
Now how come (arr+2)
and *(arr+2)
dosplay the same? They are a pointer to an array and a pointer to its first element. Although these pointets are of different types, they represent the same address, because the address of any array is the same as the address of its first element.
arr
is an array of arrays of int
. On almost any use an array is converted to a pointer to its first element. So arr
gets converted to a pointer to an array of int
.
OK, arr
gets converted to a pointer to an array of int
, so (arr+2)
is of the same type, that is, a pointer to an array of int
.
Now *(arr+2)
is the thing (arr+2)
points to. That is, an array of int
.
Now since it's an array of int
, it gets converted to a pointer to its first element. So *(arr+2)
gets converted to a pointer to int
. Note it is not an int
and is unlikely to be equal to 3
.
Now how come (arr+2)
and *(arr+2)
dosplay the same? They are a pointer to an array and a pointer to its first element. Although these pointets are of different types, they represent the same address, because the address of any array is the same as the address of its first element.
answered Aug 28 '18 at 16:54
n.m.n.m.
73.8k885172
73.8k885172
add a comment |
add a comment |
arr
is the pointer to the first array of the type int[2]
.(arr + 2)
is the pointer to the third such array.
Whereas *(arr + 2)
is the pointer to the first element of the (arr +2)
array.
Both of these will hence have the same address since they are pointing to the same position.The only difference being in their type.(arr+2)
is of the type int(*)[2]
whereas *(arr + 2)
is of the type int *
.
add a comment |
arr
is the pointer to the first array of the type int[2]
.(arr + 2)
is the pointer to the third such array.
Whereas *(arr + 2)
is the pointer to the first element of the (arr +2)
array.
Both of these will hence have the same address since they are pointing to the same position.The only difference being in their type.(arr+2)
is of the type int(*)[2]
whereas *(arr + 2)
is of the type int *
.
add a comment |
arr
is the pointer to the first array of the type int[2]
.(arr + 2)
is the pointer to the third such array.
Whereas *(arr + 2)
is the pointer to the first element of the (arr +2)
array.
Both of these will hence have the same address since they are pointing to the same position.The only difference being in their type.(arr+2)
is of the type int(*)[2]
whereas *(arr + 2)
is of the type int *
.
arr
is the pointer to the first array of the type int[2]
.(arr + 2)
is the pointer to the third such array.
Whereas *(arr + 2)
is the pointer to the first element of the (arr +2)
array.
Both of these will hence have the same address since they are pointing to the same position.The only difference being in their type.(arr+2)
is of the type int(*)[2]
whereas *(arr + 2)
is of the type int *
.
answered Aug 28 '18 at 17:00
AliAli
192116
192116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52062706%2farr-2-is-equivalent-to-arr-2-how%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
You need to dereference it twice, since it's a 2D array:
*(*(arr+2))
– Govind Parmar
Aug 28 '18 at 16:42
4
arr[2]
and*(arr + 2)
are the same by definition in the language (C11 §6.5.2.1 Array subscripting ¶2). And you get odd results because you're printing an address, not the value of a cell in the array. That's officially undefined behaviour; you pass an address toprintf()
but tell it that it's got an unsigned integer to work on. You might be OK on a 32-bit platform, sort of, more or less. Results are not remotely reliable on a 64-bit platform.– Jonathan Leffler
Aug 28 '18 at 16:45
1
technically you're getting UB since
%u
is not for printing pointers. You have to use%zu
– phuclv
Aug 28 '18 at 16:50
7
@phuclv: You're right that
%u
is UB, but so is using%zu
(that's for printingsize_t
). You should either use%p
and cast the pointer to avoid *
, or use<inttypes.h>
andPRIuPTR
(orPRIXPTR
for upper-case hex) plus a cast touintptr_t
.– Jonathan Leffler
Aug 28 '18 at 16:52
1
@phuclv : False.
%zu
requires asize_t
argument. Any incompatible type will trigger undefined behavior. Pointer arguments will trigger undefined behavior.– AnT
Aug 28 '18 at 17:04