This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
length of array in function argument
Is there any method like the Java can .length from a C point array? Thank you.
No, given a C pointer you cannot determine it's length in a platform agnostic manner.
For an actual C array though see dirkgently's answer
You could get it using a macro:
#define sizeofa(array) sizeof array / sizeof array[ 0 ]
if and only if the array is automatic and you access it in the scope of its definition as:
#include <stdio.h>
int main() {
int x[] = { 1, 2, 3 };
printf("%zd\n", sizeofa( x ));
return 0;
}
However, if you only have a (decayed) pointer you cannot get the array length without resorting to some non-portable implementation specific hack.
If you use MSVC/MinGW there is a NONPORTABLE solution for a real C-pointer:
#include <malloc.h>
char *s=malloc(1234);
#ifdef __int64
printf( "%lu\n", _msize(s));
#else
#endif
For a real C-Array like
AnyType myarray[] = {...};
or
AnyType myarray[constVal];
see the other answer.
Related
This question already has answers here:
How to find the size of a variable without using sizeof
(12 answers)
Closed 2 years ago.
I want to implement a custom function similar to sizeof operator in C . As shown in below code I have a function which is returning size for int datatype.
How can I convert this function into generalize way so it can accept any datatype and return size for passed data type.
#include<stdio.h>
int sizeof_fun(int a)
{
return (char *)(&a+1) - (char *)(&a);
}
int main()
{
int test;
printf("Sizeof int -> %d \n", sizeof_fun(test));
return 0;
}
You cannot do this as a C function.
C function parameters are fixed, and thus you can't have a that dynamically returns different sizes based on the type of parameter. Once inside the function, you don't know the type of the original variable holding the parameter. You would need to either use:
The real sizeof (why do you need to reimplement that? you can't!)
Preprocessor macros
C++ generics/templates
Function overloading (again, C++)
This question already has answers here:
declaring a variable-length array as a global variable in C
(4 answers)
Closed 2 years ago.
I'm trying to understand the difference that the scoping makes:
//global scope
int size = 4;
int array[size]; // error: variably modified 'array' at file scope
int main(void) {
int buff[size]; // works!
}
how does using a variable as array size doesn't work globally but works inside main? It would work if I use a macro instead.
Also, does using const matter for size?
simply make it compile time integral constant expression, since array length must be specified at the compile time, with define
#define SIZE 6
int array[SIZE];
This question already has answers here:
Length of array in function argument
(9 answers)
Closed 6 years ago.
Let's provide an example. I have one main file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "otherfile.h"
char output[1024*10];
int main(){
writeSomething(output);
return 0;
}
and another file with the function:
void writeSomething(char *o){
printf("sizeof(o)=%d", sizeof(o));
//code
memset(o,0,sizeof(o));
// code
}
While compiling this, I got the warning:
argument to 'sizeof' in 'memset' call is the same expression as the
destination; did you mean to provide an explicit length?
What I want is to set the whole output array to 0 in my function, so all 1024*10 bytes. But because I pass it as a pointer, it will only set the first 8 characters to 0 (because sizeof(o)=8).
How can I memset the whole array trough a pointer? Is that even possible?
The problem is that when passed as the argument to the function, the array decays into a pointer and information about its size gets lost.
Redefine the function to
void writeSomething(char *o, size_t sz){
printf("sizeof(o)=%d", sizeof(char) * sz);
//code
memset(o, 0, sizeof(char) * sz);
// code
}
so that the call to it looks like writeSomething(output, 1024*10);.
As pointed out in the comments, the C99 standard requires that sizeof(char) be 1, so it's fine to omit the sizeof(char) in the code I provided. However, I prefer to leave it there for stylistic reasons.
This question already has answers here:
Why can't I create an array with size determined by a global variable?
(8 answers)
Closed 8 years ago.
I declared a constant global variable 'MEM_PRIMES' and I wanna use it in the struct below as the array elements' number but it errors saying "variably modified 'primes' at file scope.
/* global data */
const unsigned int MEM_PRIMES = 100;
struct{
char *filename;
FILE *pfile;
int nrec;
unsigned long long primes[MEM_PRIMES];
size_t index;
}global = {"D:\\C\\C files\\mytext4.bin", NULL, 0, {2ULL, 3ULL, 5ULL}, 3};
const doesn't really ensure that the storage cannot be modified; you can take the address, cast away the const and modify it, so I believe that is why you get the complaint.
#define MEM_PRIMES 100
will fix it and is the C way.
You just can't do this in C, like you can in C++. You'll have to:
#define MEM_PRIMES 100
or similar.
The actual error message you're getting arises from the fact that you are allowed to have variable length arrays in C99 and later - you're just not allowed to have them at file scope (or in structs at all, for that matter) because the size would need to be determined at compile time. So rather than chewing you out for using a const int instead of an actual constant, your compiler is actually thinking you want a VLA, and telling you that you can't have one here.
As others have said, a const int variable is deemed to be a variable in C and cannot be used in contexts where a compile time constant is required, such dimensions of a global array or an array embedded in a structure (nor in a case clause within a switch, nor …).
While #define MEM_PRIMES 100 (suggested by the other answers) will work, I'd use:
enum { MEM_PRIMES = 100 };
The reasons are detailed in static const vs #define.
This question already has an answer here:
What is the purpose of static keyword in array parameter of function like "char s[static 10]"?
(1 answer)
Closed 9 years ago.
void test(int x[static 10]);
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10,11};
test(a);
return 0;
}
void test(int x[static 10])
{
printf("%d",x[9]);
}
I was looking for bizarre C statements. I found this one, But could not understand what is the use of static 10 in that statement. Is it same as int x[10]?
Another thing, you can use volatile also, in place of static e.g int x[volatile 10]
Anybody knows what is the use of this kinda declaration?
PS: Compiled using GCC 4.6.3,
It's a hint for the compiler telling that the x pointer parameter points to the first element of an array of at least 10 elements.
For example:
test(NULL); // undefined behavior