can not use sizeof() here?

C

c.lang.myself

whenever we pass an array to function, we have to also pass its
size.e.g if i am making a soritng function then

void sort(int b[],int size);

Now I thought about using sizeof operator(macro?) inside sort function
on b and get size of b i.e by
sizeof(b)/sizeof (b[0])

but i am not getting correct result.....
 
I

Ian Collins

whenever we pass an array to function, we have to also pass its
size.e.g if i am making a soritng function then

void sort(int b[],int size);

Now I thought about using sizeof operator(macro?) inside sort function
on b and get size of b i.e by
sizeof(b)/sizeof (b[0])

but i am not getting correct result.....
You just answered your own question, you have to pass the size. Arrays
do not have any size information.

You can use sizeof in the same scope as the array declaration, where the
compiler knows the size of the array, but not elsewhere.
 
M

Martin Ambuhl

whenever we pass an array to function, we have to also pass its
size.e.g if i am making a soritng function then

void sort(int b[],int size);

Now I thought about using sizeof operator(macro?) inside sort function
on b and get size of b i.e by
sizeof(b)/sizeof (b[0])

but i am not getting correct result.....

Yes, you are getting the correct result.
sizeof b is the size of a pointer-to-int
sizeof b[0] (or sizeof b) is the size of an int.

sizeof(b) / sizeof (b[0])
and
sizeof b / sizeof *b
yield the correct result, which is
sizeof(int *)/sizeof(int).
 
H

Hallvard B Furuseth

Martin said:
void sort(int b[],int size);
(...)
sizeof(b)/sizeof (b[0])

Yes, you are getting the correct result.
No.

sizeof b is the size of a pointer-to-int

Exactly, since a function parameter int b[] is like int *b. What he
needs is the size of the original array, not the size of a pointer.
 
I

Ian Collins

Hallvard said:
Martin said:
void sort(int b[],int size);
(...)
sizeof(b)/sizeof (b[0])
Yes, you are getting the correct result.
No.

sizeof b is the size of a pointer-to-int

Exactly, since a function parameter int b[] is like int *b. What he
needs is the size of the original array, not the size of a pointer.
He is getting the correct result. He isn't getting his expected result.
 
J

James Kuyper

whenever we pass an array to function, we have to also pass its
size.e.g if i am making a soritng function then

void sort(int b[],int size);

Now I thought about using sizeof operator(macro?) inside sort function
on b and get size of b i.e by
sizeof(b)/sizeof (b[0])

but i am not getting correct result.....

While it looks like you've declared sort() as taking as it's first
argument an array of int, this is just an illusion. Whenever a function
parameter is declared as if it were an array, that declaration is
adjusted by changing it into a pointer to the element type of that
array. In this case, int b[] gets adjust to "int *b".

As a result, inside sort(), "sizeof b" gives you the same result as
"sizeof(int*)" - it contains no information about the size of the array
that was passed to sort(). That's why sort() needs a second argument
that does indicate the size of the array.
 
C

c.lang.myself

While it looks like you've declared sort() as taking as it's first
argument an array of int, this is just an illusion. Whenever a function
parameter is declared as if it were an array, that declaration is
adjusted by changing it into a pointer to the element type of that
array. In this case, int b[] gets adjust to "int *b".

As a result, inside sort(), "sizeof b" gives you the same result as
"sizeof(int*)" - it contains no information about the size of the array
that was passed to sort(). That's why sort() needs a second argument
that does indicate the size of the array.
It is clear now......
Inside sort function sizeof(b)=2 which is just sizeof(int*)

Now how does sizeof() operator gets sizes of array e.g.
{
int a[3]={1,2,3};
printf("%d",sizeof(a));
}

It gives correct result 6...when sizeof(int)=2

but how does sizeof operator know what is size of array...as there is
not any array end marker as in case of string('\0') ..
 
J

James Kuyper

While it looks like you've declared sort() as taking as it's first
argument an array of int, this is just an illusion. Whenever a function
parameter is declared as if it were an array, that declaration is
adjusted by changing it into a pointer to the element type of that
array. In this case, int b[] gets adjust to "int *b".

As a result, inside sort(), "sizeof b" gives you the same result as
"sizeof(int*)" - it contains no information about the size of the array
that was passed to sort(). That's why sort() needs a second argument
that does indicate the size of the array.
It is clear now......
Inside sort function sizeof(b)=2 which is just sizeof(int*)

Keep in mind that sizeof(int*) is different on different systems; on
most of the systems I use, sizeof(int*) is 4.
Now how does sizeof() operator gets sizes of array e.g.
{
int a[3]={1,2,3};
printf("%d",sizeof(a));
}

It gives correct result 6...when sizeof(int)=2

but how does sizeof operator know what is size of array...as there is
not any array end marker as in case of string('\0') ..

Unlike most other operators, the sizeof operator doesn't evaluate the
value of of it's argument, it only pays attention to the type of it's
argument. That type is known at compile time. There's a tricky exception
that involves VLAs, but I don't think you need to go into that right now.

In most cases, the name of an array decays into a pointer to the first
element of that array. However, there are a few exceptions, and sizeof
is one of them. As a result, the type of 'a' is "int[3]", not "int*",
and that type contains all of the information sizeof needs to determine
the correct size for the array.
 
B

Barry Schwarz

While it looks like you've declared sort() as taking as it's first
argument an array of int, this is just an illusion. Whenever a function
parameter is declared as if it were an array, that declaration is
adjusted by changing it into a pointer to the element type of that
array. In this case, int b[] gets adjust to "int *b".

As a result, inside sort(), "sizeof b" gives you the same result as
"sizeof(int*)" - it contains no information about the size of the array
that was passed to sort(). That's why sort() needs a second argument
that does indicate the size of the array.
It is clear now......
Inside sort function sizeof(b)=2 which is just sizeof(int*)

Now how does sizeof() operator gets sizes of array e.g.
{
int a[3]={1,2,3};
printf("%d",sizeof(a));

Not the issue you raised but just for completeness: sizeof evaluates
to a size_t, not an int. %d expects an int. If you want to print out
the value of a size_t in C90, you should cast it to one of the
standard types (unsigned long is usually recommended) and use the
appropriate format for that type (e.g., %lu in the case of unsigned
long).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top