![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Calculating quantiles in SQL | figaro | UNIX for Dummies Questions & Answers | 3 | 11-20-2007 05:55 PM |
| Calculating the average | kekanap | Shell Programming and Scripting | 2 | 01-26-2007 09:36 AM |
| Scripts for calculating size and remaining space of a directory automatically. | leonall | Shell Programming and Scripting | 1 | 12-13-2005 02:30 PM |
| calculating a check digit | jim9418 | UNIX for Dummies Questions & Answers | 1 | 02-16-2005 03:14 PM |
| Calculating the day of the week | ligs | UNIX for Dummies Questions & Answers | 4 | 10-27-2001 09:28 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
This code works correctly on 3 different platforms. I think your co-worker is mis-informed somehow. The problem with this is that it is run-time only, not compile-time like sizeof().
Code:
#include <stdlib.h>
/* struct example change it for each struct */
typedef struct
{
char a[10];
int b;
} mystruct_t;
size_t dbl_size=0;
size_t int_size=0;
/* ISO C has CHAR_BIT fixed to eight bits. If it's an 8 bit per byte machine, char = 1 byte */
size_t char_size=0;
size_t mystruct_t_size=0;
unsigned long diff(const void *a, const void *b)
{
unsigned long diff=((unsigned char *)b - (unsigned char *)a);
return diff;
}
void set_sizes(void)
{
unsigned char *p=NULL;
int arr1[2];
double arr2[2];
mystruct_t arr3[2];
char arr4[2];
dbl_size=diff(&arr2[0],&arr2[1]);
int_size=diff(&arr1[0],&arr1[1]);
mystruct_t_size=diff(&arr3[0],&arr3[1]);
char_size=diff(&arr4[0],&arr4[1]);
}
int main()
{
set_sizes();
printf("dbl_size= %u\n",dbl_size);
printf("int_size= %u\n",int_size);
printf("char_size= %u\n",char_size);
printf("mystruct_t_size= %u\n",mystruct_t_size);
return 0;
}
|
|
|||||
|
Code:
#include <stdio.h>
int main(int argc, char **argv) {
int i;
int j = (int)(&i+1)-(int)&i;
printf ("(int)(&i+1)-(int)&i= %d\n", j);
printf ("sizeof(i)= %d\n", sizeof(i));
return 0;
}
Code:
(int)(&i+1)-(int)&i= 4 sizeof(i)= 4 |
|
|||||
|
Quote:
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|