Typedef Structure Question

I am currently using an older sorting code for data and I am trying to modify it to read different data types. The code is actually a sequence of several executables, built from several .c files that then reference some 20+ .h files. I have spent several days on this and have finally found the section of code that opens the data binary data files and reads them. However, there is header information inside the data files, and the code was written for one particular lab's data structure, so a lot of the information regarding skipping header file data is specific to that lab's data structure. As such I am trying to understand how the program skips the header information, so that I can tailor it to my lab's format.

I have run into an issue however, with one particular variable, because I don't fully understand what's going on.

At the top of one of the .h files, among other variable declarations, appears the following:

1
2
3
4
5
6
typedef struct
{
 unsigned short zed[8192]
}
zeddd;
zeddd zedd;


From what I understand, this is creating a new type called zeddd, and that zedd is of type zeddd. Furthermore, the entry for zedd is an unsighed short named zed, which is an array of length 8192.

Now, know this wording is confusing, but this code works as it is so I don't want to change how anything is declared, I'm simply trying to understand it.

Now, for the part I'm stuck at.

Also at the top of the frame is the following:

1
2
int zRt = 0; zi =0
u_short *zbuf


Then much lower in the code appears the following:

1
2
3
4
5
6
7
8
9
10
zRt = read(zinf, &zedd,sizeof(zedd));

if(zRt < 16384)
 {
  break;
 }
 for(zi=0;zi<8192;zi++)
 {
  zbuf[zi] = zedd.zed[zi];
 }


Okay, so, zinf is a Boolean passed to this .h file from another executable, and in this case the value is 1. The value comes from the following:

zinf = open(name, O_RDONLY, 0)

where name is the full path of a datafile. The program only reaches the other .h file if zinf = 1, zinf = 0 causes the loop to break and it outputs an error.

My issue comes in understanding the bit of code regarding read(zinf, &zedd,sizeof(zedd)) and the zedd.zed[zi] entry.

For the first bit, I thought read needed a file designation. The file opened by zinf = open(name, O_RDONLY, 0) is still open, but why does the "fildes" that read() requires work if its equal to 1? also, what exactly is the &zedd part doing?

And secondly, what is zedd.zed[zi] doing? I thought zed was an array of length 8192, is this re-declaring zed to be an array of size zi? and if so, what is it doing to the variable zedd?

Any help with this would be appreciated, and I'm sorry for the confusing code, it's 2nd hand and I don't have access to the originals nor whom I received it from.
Last edited on
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Edited the code tags in, but any actual advice would be helpful.
From what I understand, this is creating a new type called zeddd, and that zedd is of type zeddd. Furthermore, the entry for zedd is an unsighed short named zed, which is an array of length 8192.

Not quite sure what you mean by "the entry for zedd". zedd is a structure. The structure contains a single member (which makes me wonder why it's a structure in the first place), which is an array of 8192 shorts.

Okay, so, zinf is a Boolean passed to this .h file from another executable, and in this case the value is 1. The value comes from the following:

zinf = open(name, O_RDONLY, 0)

How is the value passed in from another executable? As a command-line argument?

And I don't understand how it can be a boolean, when it's clearly being used as a file descriptor. Or is this some custom type called "Boolean" that's actually really an int? Because for it to hold the return value from open(), and be passed into read(), it needs to be an int.

And secondly, what is zedd.zed[zi] doing? I thought zed was an array of length 8192, is this re-declaring zed to be an array of size zi? and if so, what is it doing to the variable zedd?


zedd.zed is an array. zi is the index of an element in that array. So the line:

zbuf[zi] = zedd.zed[zi];

is just taking the value of the element with index zi, and copying it to the element of zbuf with the same index. There's nothing unusual about it, assuming you understand arrays.

And if you don't understand arrays, you really ought to be going back to your textbook to learn about them, rather than asking piecemeal questions. Arrays (and other containers) are a fundamental part of C and C++ programming, so it's essential to understand them.
By "entry of zedd" I was referring to what it contained, so what you said is identical, I just worded it poorly I guess.

I misunderstood how the read/open functions were working though, and I understand the answer to the question about zRt myself now.

What confused me with the final part was the syntax zedd.zed, I had never seen that before, and after several searches online, through a textbook, and conversations with 2 other grad students and a professor, we were all left bit perplexed. I had assumed the basic idea that you wrote, but I was unsure if the zi was being used as an index or if it was redefining the array size of the member zed, inside zedd, to be zi instead of 8192.

So essentially the notation zedd.zed[zi] is referring to the zi'th entry of zedd, where the entry is some unsigned short array of length 8192, correct?

Sorry if this is redundant given your answer, but coding is not my specialty, and time constraints prevent us from doing an in-depth fundamental study of this, we are attempting to make a quick patchwork, rather then rebuild it from the ground up.
So essentially the notation zedd.zed[zi] is referring to the zi'th entry of zedd, where the entry is some unsigned short array of length 8192, correct?


No. It's referring to the zi'th element of zedd.zed.

zedd is an instance of a structure. It has a single member, zedd.zed. That member is an array. That array has 8192 elements.

Actually, it's not the zi'th element - it's the (zi - 1)'th element. In C and C++, array indexes start at 0. So zedd.zed[0] is the first element, zedd.zed[1] is the second element, and so on.

EDIT: This would be easier if these things didn't have such silly names. Who on earth decided that it was sensible to have things called zed, zedd and zeddd in the same bit of code?

EDIT 2: I really recommend you read a tutorial or textbook to be clear about what a structure is, and what an array is.
Last edited on
The answer to your first edit: Some foreign postdoc who is now long gone.

And thanks for all of your help clarifying things, I appreciate it.
You're welcome :)
Topic archived. No new replies allowed.