ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have not been programming in 'C' since University but it's coming back like a Bad Dream... fun figuring out these BUGS!
I am trying to determine the size of my character array inside a subroutine. Compile generates an error: GPX_split_file.c:52:17: error: invalid application of ‘sizeof’ to incomplete type ‘char[]’
exerpts from my project:
GPX.h
extern char tmp_str[];
extern int tmp_int;
void gpx_split_file();
GPX.c
#include "GPX.h"
char tmp_str[512]; // sizeof issue with this array
int tmp_int;
main()
{
GPX_split_file()
}
An array size does not work this way. What you should do is instead of using a literal value of 512, you should #define a constant as part of an include H file and use that definition:
Code:
#define TMP_ARRAY_LEN 512
// In source
char tmp_str[TMP_ARRAY_LEN];
Meanwhile, are you looking for the size of that array, regardless of it's contents? Meaning the maximum possible size? Or are you looking to determine if there is any information stored in that array? If it is a string, you can use a function call to get the string length, however if the data in that array can be any value from 0-255, then string length is not the best choice. Noticing that it is not an unsigned array, so this is why I'm asking about if you wish a string length.
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
As rtmistler has pointed out, you cannot use sizeof to get the size of an array, and the size may not be what you actually need anyway.
It would be helpful if you could provide a better description of what you are trying to test (size vs existence, value vs length?). Putting it into simplest words often highlights the objective and the solution.
It is my understanding – and good ol' WikiPedia supports me on this, that the sizeof() an array should be the amount of storage required to store the entire array ... and that, in some optimization situations, that might be larger than you expect. (The compiler might choose to use a cache-aligned storage layout.)
The size of an array element is as for any other scalar.
Last edited by sundialsvcs; 09-19-2017 at 07:53 AM.
so the size of tmp_str array is unknown in GPX_split_file.c (in compile time), because this type is currently inclomplete.
You could/may calculate the size runtime, but that is another function. http://en.cppreference.com/w/c/language/sizeof
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.