sizeof a/sizeof *a 
Author Message
 sizeof a/sizeof *a

I was writing a program to count how many random numbers come up in a set of
ranges. I'm sure I have seen people use this trick on c.l.c before, but I'm
getting an error.  What am I missing?
Tom
--
20:45 ~/Test$ cat sizeof.c
#include <stdio.h>

int main(void)
{
    int arr[100];
    int i;
    int count;
    double x;

    x = 0.34755;

    count = sizeof arr / sizeof *arr;
    for (i = 0; i < count; i++)
    {
        arr[i] = 0;
    }
    /* This is what gives me an error */
    arr[x * sizeof arr / sizeof *arr]++;

    for (i = 0; i < count; i++)
    {
        printf("%d:\t%d\n", i, arr[i]);
    }

    return 0;

Quote:
}

20:46 ~/Test$ gcc -Wall -ansi -pedantic -o sizeof sizeof.c
sizeof.c: In function `main':
sizeof.c:18: array subscript is not an integer
20:46 ~/Test$
--
(I know I could use arr[x * count]++, but that's not as elegant.)


Sun, 23 Mar 2003 03:00:00 GMT  
 sizeof a/sizeof *a

[x is a "double".]

Quote:
>     /* This is what gives me an error */
>     arr[x * sizeof arr / sizeof *arr]++;

Insert a cast to int (or size_t, or some other integer type).
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield


Sun, 23 Mar 2003 03:00:00 GMT  
 sizeof a/sizeof *a

Quote:
>I was writing a program to count how many random numbers come up in a set of
>ranges. I'm sure I have seen people use this trick on c.l.c before, but I'm
>getting an error.  What am I missing?
>Tom
>--
>20:45 ~/Test$ cat sizeof.c
>#include <stdio.h>

>int main(void)
>{
>    int arr[100];
>    int i;
>    int count;
>    double x;

>    x = 0.34755;

>    count = sizeof arr / sizeof *arr;
>    for (i = 0; i < count; i++)
>    {
>        arr[i] = 0;
>    }
>    /* This is what gives me an error */
>    arr[x * sizeof arr / sizeof *arr]++;

This is scary, but try seeing what the index
results in via:

x = 0.34755 * (double) (sizeof arr / sizeof *arr);
i = (int) x;

see what 'i' is. Is it less than 100? If so then
you should be okay incrementing that element.

--

Warmest regards.

Optimize only if it runs too slowly or does not fit, spaces instead of tabs.



Sun, 23 Mar 2003 03:00:00 GMT  
 sizeof a/sizeof *a

Quote:
>I was writing a program to count how many random numbers come up in >
double x;
>    /* This is what gives me an error */
>    arr[x * sizeof arr / sizeof *arr]++;
>sizeof.c: In function `main':
>sizeof.c:18: array subscript is not an integer
>20:46 ~/Test$

Sounds clear enough.

"x" is a double
"sizeof arr / sizeof *arr" is a size_t.

How "x * sizeof arr / sizeof *arr" could be an integer ? What kind of magic?
You need a (int) typecast, but the accuracy might be poor...

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc
"Abracadabrantesque !" -- Jacques Chirac



Sun, 23 Mar 2003 03:00:00 GMT  
 sizeof a/sizeof *a
On Wed, 4 Oct 2000 20:49:55 +0100, "Tom Huckstep"

Quote:

>I was writing a program to count how many random numbers come up in a set of
>ranges. I'm sure I have seen people use this trick on c.l.c before, but I'm
>getting an error.  What am I missing?
>Tom
>--
>20:45 ~/Test$ cat sizeof.c
>#include <stdio.h>

>int main(void)
>{
>    int arr[100];
>    int i;
>    int count;
>    double x;

>    x = 0.34755;

>    count = sizeof arr / sizeof *arr;
>    for (i = 0; i < count; i++)
>    {
>        arr[i] = 0;
>    }
>    /* This is what gives me an error */
>    arr[x * sizeof arr / sizeof *arr]++;

>    for (i = 0; i < count; i++)
>    {
>        printf("%d:\t%d\n", i, arr[i]);
>    }

>    return 0;
>}
>20:46 ~/Test$ gcc -Wall -ansi -pedantic -o sizeof sizeof.c
>sizeof.c: In function `main':
>sizeof.c:18: array subscript is not an integer
>20:46 ~/Test$
>--
>(I know I could use arr[x * count]++, but that's not as elegant.)

arr[x*count] will give you the same error.  arr[(int)(x*count)] will
work, but it produces the same output as:

main(){int i;for(i=0;i<100;i++)printf("%d\t%d\n",i,i==34);return 0;}

and what on earth is the purpose of that?

-ck



Tue, 25 Mar 2003 11:09:52 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. relation between sizeof(void*), sizeof(int)

2. sizeof without sizeof

3. sizeof(int) sizeof(long) etc.

4. sizeof (struct) ! = sizeof struct elements

5. sizeof(foo) -1 or sizeof(foo)

6. sizeof(void*) versus sizeof(foo*)

7. coding style advice for sizeof (long) < sizeof (size_t)?

8. sizeof (pointer to array)/sizeof (p[0]) how to use ?

9. sizeof a char * [] passed to function

10. sizeof() problem

11. /Wp64 and sizeof int question

12. VC++ 7.0 sizeof struct issue with 1 byte packing

 

 
Powered by phpBB® Forum Software