LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2017, 12:20 PM   #1
Caltrop
Member
 
Registered: Jul 2017
Location: Indiana, US
Distribution: Salckware
Posts: 32

Rep: Reputation: Disabled
sizeof() issue


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()
}

GPX_split_file.c
#include "GPX.h"
void gpx_split_file()
{
tmp_int = sizeof(tmp_str) / sizeof (tmp_str[0]); // ERROR!
}
 
Old 09-18-2017, 12:26 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,722
Blog Entries: 12

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
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.
 
Old 09-18-2017, 12:40 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,278

Rep: Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994
@OP: And what is your question?
 
Old 09-18-2017, 01:46 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,482
Blog Entries: 6

Rep: Reputation: 2413Reputation: 2413Reputation: 2413Reputation: 2413Reputation: 2413Reputation: 2413Reputation: 2413Reputation: 2413Reputation: 2413Reputation: 2413Reputation: 2413
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.
 
Old 09-19-2017, 07:43 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,515
Blog Entries: 4

Rep: Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955
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.
 
Old 09-19-2017, 07:45 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,681

Rep: Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856
From the other hand I think the error message is correct.
You wrote in GPX.c:
Code:
...
char tmp_str[512]; // sizeof issue with this array
...
and you wrote in GPX_split_file.c:
Code:
#include "GPX.h"
tmp_int = sizeof(tmp_str) / sizeof (tmp_str[0]); // ERROR!
amd in GPX.h:
Code:
extern char tmp_str[];
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
 
1 members found this post helpful.
Old 09-25-2017, 11:45 AM   #7
Caltrop
Member
 
Registered: Jul 2017
Location: Indiana, US
Distribution: Salckware
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thumbs up Got It!

I rewrote the entire project
must have been a punctuation problem & other programmer errors
used some suggestions, thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
virtual and sizeof jackandking Programming 5 05-06-2009 11:37 AM
C and sizeof exvor Programming 5 08-11-2007 11:27 PM
sizeof problem ltordsen Programming 8 12-05-2005 09:13 AM
sizeof( ) in c++ question amoor757 Programming 6 01-26-2004 02:42 PM
sizeof() and C/C++ boku Programming 2 09-01-2002 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration