For (* ptr) [], why is printf (& ldquo;% p & rdquo ;, (void *) ptr + 1)...
source link: https://www.codesd.com/item/for-ptr-why-is-printf-p-void-ptr-1-but-not-printf-p-ptr-1.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
For (* ptr) [], why is printf (& ldquo;% p & rdquo ;, (void *) ptr + 1) but not printf (& ldquo;% p & rdquo; ptr + 1)?
This question already has an answer here:
- Pointer arithmetic for void pointer in C 7 answers
I just fail to understand the error in the following program.What I have done is I've assigned the address of an array of size 5 to a pointer ptr of type (*)[]
.There is no type mismatch and it's ok as far as that goes.But then I want to print the value ptr+1.As I expected, it shows an error about unspecified bounds.But it just misses me how the same ptr+1
works fine when I cast it to type void*
.
Casting is fine, but before it comes to that,how can the program calculate ptr+1
as it simply doesn't know the bounds? How would it know whether to move 5 elements ahead, or 8 elements ahead or any elements ahead?Why doesn't (void*)ptr+1
show the same error as ptr+1
?
To better highlight the whole thing, I have also used a pointer ctr
which is explicitly declared to be of type (*)[5]
instead of (*)[]
. Please give me the technical reasons behind this.Thank you.
#include<stdio.h>
int main(void)
{
int ar1[5]={1,5,3,8,9};
int (*ptr)[]=&ar1,(*ctr)[5]=&ar1;
printf("%p\n",ptr+1); //ERROR about unspecified bounds
printf("%p\n",(void*)ptr+1); //It's ok
printf("%p\n",ctr+1); //It's ok
}
PSST!! The last two correct printf()
s don't produce the same value.If I comment out the incorrect printf("%p\n",ptr+1);
line, this is the output.
0023FF25
0023FF38
PSST!!
I checked it again, int the ptr+1
part of (void*)ptr+1
, a 1
is simply being added to the numeric value of ptr
.What's going on?
Please give me the technical reasons behind this.
The technical reason is that when adding one, your compiler tries to jump to the next instance of that type.
Example: adding one to an int
pointer will increment it by four because an int
is 4 bytes long and it will then point to the next int.
In your example, with ptr you never specified the size. It's impossible to jump to the next element because the size of this element is not known. Once you specified the size by making it point to an array of exactly 5 elements, it worked again.
When casting ptr to void, it became easy again to go to the next element. The compiler just added one byte.
In your run:
ptr
is 0023FF24
((void*)ptr) + 1
is 0023FF25
(the base address plus 1 byte for one element of "void")
ctr
is 0023FF24
(same as ptr)
ctr + 1
is 0023FF38
(the base address, plus 20 (5*sizeof(int)
) bytes for one element of int (*)[5]
)
Recommend
-
16
Magento: & ldquo; Image does not exist & rdquo; advertisements I'm importing a CSV file in Magento (version 1.9). I receive...
-
11
& ldquo; azure & rdquo; not recognized in Azece powershell advertisements For the first time I'm trying out Azure Powershell. I'm sele...
-
9
can not find the class of symbols & ldquo; Builder & rdquo; advertisements I have recently downloaded Android Studio, I t...
-
9
How to know when all promises are resolved in a dynamic & ldquo; iterable & rdquo; parameter? advertisements My problem is that I don't kno...
-
10
VHDL: signals and ports on which side of & ldquo; arrow & rdquo; = & gt; advertisements very basic question: How do I know...
-
12
1004 error & ldquo; Is it possible that it has been moved, renamed or deleted? & Rdquo; advertisements My goal: make Excel save a copy...
-
9
How to use & ldquo; Consistency & rdquo; Java api parameter when setting elastic search data advertisements I have used "consistency"...
-
29
Why do I need to & ldquo; C & rdquo; In front of the class name in C ++ advertisements Possible Duplicate:
-
5
HTML5: Input additional characters with type = & ldquo; Number & rdquo; advertisements In our app, we have many inputs that are used t...
-
9
Why do I get this warning in & ldquo; If (fd = fopen (fileName, & rdquo; r & ldquo;) == NULL) & rdquo; advertisements ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK