0

I literally have no idea how to do this for the data types char, float or double.

The extent of my knowledge in this is putting a variable next to [10] and that's it.

Another question on the worksheet: For the declaration float data[12]; with the compiler assigning a starting address of 0005 in decimal. What would be the address of the last byte?

blueくん
  • 19
  • 5
  • Another question on the worksheet: For the declaration float data[12]; with the compiler assigning a starting address of 0005 in decimal. What would be the address of the last byte? – blueくん May 21 '20 at 18:30
  • Typically, it would be preferable to add _Another question_ by editing your post to add the new content. :) – ryyker May 21 '20 at 18:38
  • Thanks for the heads-up! Will do :D I'm still kinda new to the community & interface on this site, so please bare with me :3 – blueくん May 21 '20 at 18:44
  • I'm sorry but I don't get rid of the feeling that you are just trying to have the SO community do your homework. In order for you to learn something out of that exercise task, you should really pick a book and look up the syntax how to define array variables of differnt types (and what this really means). In the meantime, I'm voting to close this thread. – HelpingHand May 21 '20 at 20:24

3 Answers3

1

sizeof arrayName will return the size of the entire array. sizeof arrayName[0] will return the size of a single element of the array.

You can use any index in the second expression, it doesn't have to be a valid element of the array. The size is determined at compile time from the type, the contents are not relevant.

Note that you can only get the size of the array using an actual array variable, not a pointer.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

sizeof is a built-in compile-time operator, so values are computed at compile-time, not during run-time. (except if used to compute the value of a VLA.) Note, parenthesis are optional, with this operator, (with a few exceptions) although often used by some as a readability preference.

int intvar[] = {1,2,3,4,5,6,7,8,9,0);
size_t size = sizeof(intvar);

char strvar[] = {"this is 10"};
size = sizeof(strvar);

float fltvar[] = {1.2, 2.1, 3.4, 5.6, 7.5, 8.9, 3.7, 5.2, 5.89, 54.0};
size = sizeof(fltvar);

If you would like to know how many elements in say an an array of strings, eg such as

char str_array[][80] = {"this", "is", "a" "string", "array"};  

divide the size of the array by the size of one of its elements as shown here:

size_t string_count = sizeof(str_array)/sizeof(str_array[0]);

in this case resulting in 5 strings. Note that for an array of strings such as this, the memory for each string must be the same, in this case 81 (80 + 1 for NULL terminator)

Edit
Question in comments: For the declaration float data[12]; with the compiler assigning a starting address of 0005 in decimal. What would be the address of the last byte?

First, when creating float data[12];, the OS provides the address, based on among other things what is the first location with a block contiguous memory big enough to contain 12*32 bytes of memory. So it is arbitrary the we can set the location to 5 (which is equivalent to 0x0005)

float data[12];
printf("Address of last element in data when memory starts at 0x0005 is %p\n, &data[11] - &data[0] + 0x0005);

 float data[12];
 float *loc_0 = &data[0];
 float *loc_12 = &data[11];

 // memory location can now be determined by the simple expression:
 // loc_12 - loc_0  + 0x0005

 printf("Address of last element in data when memory starts at 0x0005 is 0x%.4p\n", (void *)(loc_12 - loc_0  + 0x0005));  

(For the record though, starting at address == 0005 is unnatural as memory addresses are given by the OS without consulting the programmer first, so there is little that can be done to actually start at address == 0005.)

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Thanks for responding so fast, but one last thing; I need clarification regarding the "32" in that equation, could you please enlighten me as to how it got there and what it is? Sorry if I'm pestering you at this point; I'm still a major noob when it comes to programming and I really appreciate everyone's help for replying to this thread. – blueくん May 21 '20 at 19:09
-1

You can use the sizeof operator on either an entire array or on a specific element of an array. Also, note that: (a) there are two forms of the sizeof operator - with or without the brackets; and (2) using the dereference operator (*) on an array variable will refer to that array's first element.

This working example should illustrate the concepts:

#include <stdio.h>

int main()
{
    char   carray[10]; size_t csize = sizeof(carray); // # bytes for whole array
    int    iarray[10]; size_t isize = sizeof(iarray); // <ditto>
    float  farray[10]; size_t fsize = sizeof(farray); // ...
    double darray[10]; size_t dsize = sizeof(darray); 
    printf("Array sizes: %zu, %zu, %zu, %zu.\n", csize, isize, fsize, dsize);

    size_t celem = sizeof(carray[0]); // Size of a single element (the first)
    size_t ielem = sizeof(iarray[3]); // ... or any other element!
    size_t felem = sizeof(*farray);   // We can even use the array as a pointer!
    size_t delem = sizeof *darray ;   // An example of the other form of sizeof.
    printf("Element sizes: %zu, %zu, %zu, %zu.\n", celem, ielem, felem, delem);

    return 0;
}

Another connected use of the sizeof operator allows you to quickly determine the number of elements in an array (of any type):

typedef int MyType; // We can change this WITHOUT changing the subsequent code!
#define NUMELEMENTS 42; // And this!
// Code somewhere else...
    MyType MyArray[NUMELEMENTS];
    size_t MySize = sizeof(MyArray) / sizeof(*MyArray); // Will give number of elements
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83