* function parameters
@ 2023-11-21 2:30 André Albergaria Coelho
2023-11-21 6:05 ` Martin Uecker
2023-11-21 11:42 ` Jonathan Wakely
0 siblings, 2 replies; 3+ messages in thread
From: André Albergaria Coelho @ 2023-11-21 2:30 UTC (permalink / raw)
To: gcc
[-- Attachment #1: Type: text/plain, Size: 430 bytes --]
Hello
#include <stdio.h>
void func(char *ptr) {
printf("\n%i",sizeof(ptr));
}
int main(void) {
char arr[10];
printf("\n Sizeof arr %i",sizeof(arr));
func(arr);
return 0;
}
/* sizeof(arr) != sizeof(ptr), but they point to same thing. */
So problem. On main, arr has size 10, while on func, arr has size 8. But
they are equal.
--
André Albergaria Coelho
andrealbergaria@gmail.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: function parameters
2023-11-21 2:30 function parameters André Albergaria Coelho
@ 2023-11-21 6:05 ` Martin Uecker
2023-11-21 11:42 ` Jonathan Wakely
1 sibling, 0 replies; 3+ messages in thread
From: Martin Uecker @ 2023-11-21 6:05 UTC (permalink / raw)
To: André Albergaria Coelho, gcc
Am Dienstag, dem 21.11.2023 um 02:30 +0000 schrieb André Albergaria Coelho via Gcc:
> Hello
>
> #include <stdio.h>
>
> void func(char *ptr) {
> printf("\n%i",sizeof(ptr));
> }
>
> int main(void) {
> char arr[10];
> printf("\n Sizeof arr %i",sizeof(arr));
> func(arr);
>
> return 0;
> }
>
> /* sizeof(arr) != sizeof(ptr), but they point to same thing. */
>
>
> So problem. On main, arr has size 10, while on func, arr has size 8. But
> they are equal.
No problem. sizeof(ptr) is the size of the pointer object
itself, while arr is the size of the array.
In func(arr) the array is converted to a pointer to
its first element.
If you want to pass the address of the array itself and
then get its size in 'func' you could write it like this:
#include <stdio.h>
void func(char (*ptr)[10]) {
printf("\n%i",sizeof(*ptr)); // sizeof pointer target
}
int main(void) {
char arr[10];
printf("\n Sizeof arr %i",sizeof(arr));
func(&arr); // pass address to array
return 0;
}
Martin
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: function parameters
2023-11-21 2:30 function parameters André Albergaria Coelho
2023-11-21 6:05 ` Martin Uecker
@ 2023-11-21 11:42 ` Jonathan Wakely
1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2023-11-21 11:42 UTC (permalink / raw)
To: André Albergaria Coelho; +Cc: gcc
On Tue, 21 Nov 2023 at 02:31, André Albergaria Coelho via Gcc
<gcc@gcc.gnu.org> wrote:
>
> Hello
Hello,
This mailing list is for discussing the development of GCC itself, not
for help with basic C programming. Please use the gcc-help@gcc.gnu.org
list for this kind of thing, or a general C programming forum.
Thanks,
Jonathan
>
> #include <stdio.h>
>
> void func(char *ptr) {
> printf("\n%i",sizeof(ptr));
> }
>
> int main(void) {
> char arr[10];
> printf("\n Sizeof arr %i",sizeof(arr));
> func(arr);
>
> return 0;
> }
>
> /* sizeof(arr) != sizeof(ptr), but they point to same thing. */
>
>
> So problem. On main, arr has size 10, while on func, arr has size 8. But
> they are equal.
>
>
> --
> André Albergaria Coelho
> andrealbergaria@gmail.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-11-21 11:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-21 2:30 function parameters André Albergaria Coelho
2023-11-21 6:05 ` Martin Uecker
2023-11-21 11:42 ` Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).