Well kinda, but no, this is incorrect in some ways.
A struct adds labels to a group of related data in C. The group of data usually corresponds to a single record of structured data of some type, such as:
Code:
struct customer {
int account_number;
character *first_name;
character *last_name;
} ;
It is important to realize that the definition of a structure is often different than the allocation of memory to use it. In the example above, the customer record above is defined by a structure but the allocation of memory to that structure is a separate step. No memory was allocated to 'customer' in the struct statement and usually this is done with an automatic array, a single automatic variable or a call to malloc().
This following statements creates a 3 integer struct and creates the associated memory (in this case on the stack)
Code:
struct point_s {
int x;
int y;
int z;
} ; /* define the record */
struct point_s point; /* one element of that record */
struct point_s *PteToPoint = &point; /* a pointer pointing to that record */
/* set x, y, z of point in 3 different ways: */
point.x=1;
(*PtrToPoints).y=2;
PtrToPoints->z=3; /* idiomatic C pointer to a struct member */
In this case, the data structure for a point in 3d space is created. Next, the memory for 1 element is created in one step. You can create an array of points similarly.
The example you have of one->two->three would be used in the case of a structure containing a pointer. Each application of '->' simply means 'what that points to' So you data structure would need to be:
Code:
struct s1 {
[whatever data type] three;
};
struct s2 {
[whatever other data];
struct s1*two;
};
So one->two->three would be the data in a struct defined by s1 pointed to by a pointer inside s2...
The cycle for using a struct is usually along these lines:
Code:
1) Define the record (using struct statements, union, etc);
2) Allocate memory for the data (by a named variable, call to malloc, creation of an array);
3) Fill the records (using C statements);
4) Access the data (using var.member directly or (*var).member or var->member with pointers).
I have many headers with huge amount of structures in them, typical one looks like this:
$ cat a.h
struct Rec1 {
int f1;
int f2;
};
struct Rec2 {
char r1;
char r2;
};
struct Rec3 {
int f1;
float k1;
float ... (6 Replies)
Hi,
I have received an application that stores some properties in a file. The existing struct looks like this:
struct TData
{
UINT uSizeIncludingStrings;
// copy of Telnet data struct
UINT uSize;
// basic properties:
TCHAR szHost; //defined in Sshconfig
UINT iPortNr;
TCHAR... (2 Replies)
in C i am using this code to get the c time or a time or m time
struct dirent *dir;
struct stat my;
stat(what, &my);
thetime = my.st_ctime;
How can i check if i have permission to check the c time of the file? (1 Reply)
Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function... (2 Replies)
Hi,
I am working on gcov.Meaning, analysing the functionality of gcov. There is one structure called "struct bb". I am not sure, how struct bb members are getting assigned values. If anyone knows how it is happening pls let me know.
Thanks in advance.
--Vichu (0 Replies)
in my .c file i have a struct atop of the program defined as follows:
#define MAX 10
int curtab;
static struct tab {
int count;
int use;
} tab;
with the initial function following it like so:
int tab_create(int init_count)
{
int i;
for(i=0; i < MAX; i++)
{... (1 Reply)
I receive an integer as argument for a function.
within function definition i want it to be of type struct tm.
eg..
main()
{
int a;
......
}
function(...,..,a,..)
int a;
{
struct tm tm;
if(!a)
^ time(&a);
^ ... (4 Replies)
Hi
We are using a code generator for initializing structures with the #define macro. Compiling it with the GCC 2.8.1 (with -ansi) it OK. But when we are using the SUN C 5.0 compiler it screams.
Following is a code sample:
#include <stdlib.h>
#include <stdio.h>
typedef struct TEST3 {... (4 Replies)