C – SizeOF pointer

char c[] = {'a','b','c'};
int* p = &c[0];
printf("%i ", sizeof(*p)); //Prints out 4
printf("%i ", sizeof(*c)); //Prints out 1

I am very confused about this part of the code. Both p and c represent the address of the array c at the 0th index. But why does sizeof(* p) print 4? Shouldn’t it be 1?

Because the type of p is int *, the type of * p is int, in your implementation The above is obviously 4 bytes wide.

If you don’t want the program to call undefined behavior, please use %zu to print size_t (yield size).

< /p>

char c[] = {'a','b','c'};
int* p = &c[0];
printf("%i ", sizeof(*p)); //Prints out 4
printf("%i ", sizeof(*c)); //Prints out 1

I am This part of the code is very confusing. Both p and c represent the address of the array c at the 0th index. But why does sizeof(*p) print 4? Shouldn’t it be 1?

Because the type of p is int*, the type of *p is int, which is obviously 4 bytes wide in your implementation.

If you don’t want the program to call undefined behavior, please use %zu to print size_t (output size).

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 374 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.