How do I find the sizeof text char array?Code:struct data {
char text[16];
struct data *next;
};
Printable View
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.
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
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
*/
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.
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!
}
zombiezparadize! wow! LOL!!! :D Ha Ha Ha!
Edit: Was that a little over the top? Sorry. But thanks for a really good laugh :)
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.]
yeah and I had stupidly ignored exactly that.
:)Code:d = tmp = new data;
And I never claimed to know C++ well...I have always written code in C.
I have been introducing myself to C++ lately.
But the blasting did help me learn! So thanks!