473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Declaring an array using sizeof

I have the following code:

Expand|Select|Wrap|Line Numbers
  1. struct mystruct {
  2.    char fieldA[5];
  3.    char fieldB[7];
  4. };
  5.  
  6. void dosomething(struct mystruct* pms) {
  7.    char content[sizeof(pms->fieldB)];
  8.  
  9.    /* some code... */
  10.  
  11.    memcpy(content, pms->fieldB, sizeof(pms->fieldB));
  12.  
  13.    /* some code... */
  14. }
  15.  
  16. int main(int argc, char** argv) {
  17.    struct mystruct ms;
  18.    char array[sizeof(ms->fieldA)];
  19.  
  20.    /* some code... */
  21.  
  22.    memcpy(array, ms.fieldA, sizeof(ms.fieldA));
  23.  
  24.    /* some code... */
  25. }
  26.  
Is there any problem doing that, even for a C89/90 compiler?


Thanks!
Oct 29 '13 #1
3 1823
weaknessforcats
9,208 Expert Mod 8TB
I have never liked using sizeof for arrays. The reason is that sizeof is used to determine the size of a type and an array is not a type on the stack. An array is a collection of a type.

sizeof gives you the size of the object on the stack. If you define the array on the stack you get the size of the array. However, if you pass the array to a function, what you pass is the address of the array and not the array itself. Do a sizeof the array from inside that function and you get the sizeof the pointer.

I use defensive programming and always keep the number of array elements in a separate struct member.

That said, the code you posted does give the correct array size.
Oct 29 '13 #2
I understood. Thank you for the answer!

One more question: that code is OK because that sizeof is evaluated at compile time and its value never changes... right? I mean, "sizeof(ms.fieldA)" and "sizeof(pms->fieldA)" will always be 5 (the same idea can be applied for "fieldB").

I have to correct a little piece of that code. In the main function, obviously I made a little mistake. Instead of "sizeof(ms->fieldA)", that should be "sizeof(ms.fieldA)". Certainly you have noticed that.
Oct 30 '13 #3
donbock
2,426 Expert 2GB
It also works because you are working with char arrays; and sizeof(char) is 1 by definition.

For example, let's assume sizeof(short) is 2.
Expand|Select|Wrap|Line Numbers
  1. short array1[10];
  2. short array2[sizeof(array1)];
This doesn't do what you want because sizeof(array1) = 10*2 = 20. A common idiom is more like this:
Expand|Select|Wrap|Line Numbers
  1. short array1[10];
  2. short array2[sizeof(array1)/sizeof(array1[0])];
Notice that this idiom also works for char arrays.

Same comment as weaknessforcats offered: this silently gives the wrong answer if array1 is a pointer instead of an array.
Oct 30 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jim Hudon | last post by:
i need to create an array of a size determined by a non-const variable: int char sampleArray; why does the following not work, and what can i do: const int constArraySize = arraySize; int...
3
by: Charlie | last post by:
My Google Groups search on this one showed the endless debates and I did my best to look through them to see if my question was answered and didn't find it. So, here it goes.... From the GG...
15
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float...
11
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
31
by: arun | last post by:
suppose i have a pointer to an array of integers.can i initialize each member of the array using pointers?plz explain
1
by: geervani | last post by:
hello, Can anybody just tell me how to scan or input a 2d array using pointer i e indirect reference. I am allocating memory the array dynamically. the statement i am using is int *a;...
15
by: subramanian100in | last post by:
Is it possible to measure the size of an array without using the sizeof operator ?
3
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
8
by: anandmms | last post by:
Hello friend, my problem is to save more than one fingerprint images in one byte array and then store in database(oracle). i dont know how to solve it. But previously i had did image saving...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.