How do I find the sizeof text char array?Code:struct data { char text[16]; struct data *next; };
How do I find the sizeof text char array?Code:struct data { char text[16]; struct data *next; };
Do a sizeof(x.text), where x is a struct data.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You need to have an instance of the struct data, e.g.
Of course, another option is to use a constant, e.g.Code:struct data d; printf("sizeof text is %d\n", sizeof(d.text));
--Code:#define TEXTLEN 16 struct data { char text[TEXTLEN]; ... }
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
C99:Code:#include <stdio.h> struct st { char text[30]; }; int main() { printf ("sizeof x: %zu\n", sizeof((struct st){0}).text); return 0; }
C90?
Code:#include <stdio.h> struct data { char text[16]; struct data *next; }; int main() { printf ( "size = %lu\n", (long unsigned)sizeof ((struct data*)0)->text ); return 0; } /* my output size = 16 */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Nice one Dave.
Not mine -- just a memory. Took a while to jog it, though:
http://groups.google.com/group/comp....c6ee2af1cce57b
http://groups.google.com/group/comp....7503f767b1795b
[edit]There is some deal with offsetof that I think I'm confusing it with though.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Boy!
So many cryptic ways... thanks to all!
I find this one easier for me:
sizeof (<struct instance>.<element name>);
This is what I was trying to do, reading an unlimited length char array from an input stream:
See how simple and small C++ version is!Code:#ifdef C #include <stdio.h> struct data { char text[1024]; data *next; }; data *readData(FILE *in) { data *d, *tmp; d = tmp = new data; tmp->next = NULL; while (true) { int n = fread(tmp->text, 1, sizeof(tmp->text)-1, in); tmp->text[n] = '\0'; if (n < (sizeof(tmp->text)-1)) break; tmp->next = new data; tmp = tmp->next; tmp->next = NULL; } return d; } void writeData(data *d, FILE *out) { while (d) { fputs(d->text, out); d = d->next; } } int main() { printf("Enter a really long story please, press Ctrl-Z to end!\n"); data *d = readData(stdin); printf("Your text:\n"); writeData(d, stdout); return 0; } #else #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a really long story please, press Ctrl-Z to end!" << endl; string text; string line; while (getline(cin, line)) text += line + '\n'; cout << "Your text:" << endl << text; return 0; } #endif![]()
You see, two versions, two main()s of my program are in the same file, so to toggle the compile, I used this directive!
Side note: I know, my C version will not compile as C file, but it's almost identical to what I would do in C.
You should never return a local pointer! You can never be sure of the consequences.Code:#ifdef C #include <stdio.h> struct data { char text[1024]; data *next; }; data *readData(FILE *in) { data *d, *tmp; d = tmp = new data; tmp->next = NULL; while (true) { int n = fread(tmp->text, 1, sizeof(tmp->text)-1, in); tmp->text[n] = '\0'; if (n < (sizeof(tmp->text)-1)) break; tmp->next = new data; tmp = tmp->next; tmp->next = NULL; } return d; // That is called SUICIDE! }
I would love to change the world but they dont give me the source code!
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
zombiezparadize! wow! LOL!!!Ha Ha Ha!
Edit: Was that a little over the top? Sorry. But thanks for a really good laugh![]()
Last edited by manav; 05-07-2008 at 01:16 AM.
Edit2: Actually, I realize that zombie is somewhat right, i.e., never return a local pointer, which, contains a local address!
zombie, the point is do not return the address of local objects, pointer being local is ok, and also i am returning the address obtained from new, which will be available, even after, the function returns.
[I made this separate post, just to update properly and also my post was last, still, iMalc was listed as last post.]