I want to know what a struct type variable represents in C, I understand that a variable of type int is referring to an integer value in the memory and I can use that integer variable to do multiple sorts of things like add, subtract, etc..., And I understand that an array variable represents an address to the first element in that array.
But what about the struct type variable, what does it refer to (I know it is not a pointer and it is used to access struct members)?
To explain the problem in more detail, in the example below you can see that the address of the birthDate variable which is of type struct date is the same as the address of the first member in the birthDate variable.
But the variable itself seems to store some sort of address as you can see, my question is what does this value (might be an address) stored in the struct variable birthDate represent?
#include <stdio.h>
int main()
{
struct date
{
int day;
int month;
int year;
};
struct date birthDate = { 7, 12, 1999 };
printf("birthDate variable address => %p\n\n", (void*) &birthDate);
printf("birthDate variable first member address => %p\n\n", (void*) &birthDate.day);
printf("The struct type birthDate variable value ( which seems to be an address or something :\\ ) => %i (int), %0X (hex)\n\n", birthDate, birthDate);
int x = 2;
printf("The interger x variable value ( cool I understand this :) ) => %i\n", x);
return 0;
}
Output
birthDate variable address => 000000000061FE10
birthDate variable first member address => 000000000061FE10
The struct type birthDate variable value ( which seems to be an address or something :\ ) => 6422016 (int), 61FDF0 (hex)
The interger x variable value ( cool I understand this :) ) => 2
CodePudding user response:
A struct
represents a grouping of one or more objects of differing types. In this case struct date
contains 3 members of type int
.
What seems to be confusing you is that you're calling printf
in a way that you're not supposed to:
printf("... birthDate variable value ...=> %i (int), %0X (hex)\n\n",
birthDate, birthDate);
The %i
and %x
format specifiers expect an int
and unsigned int
respectively as their argument, but you're passing a struct date
for each. Not using the correct format specifier triggers undefined behavior. So the value being printed doesn't really have any meaning.
If you want to see the individual bytes that make up the struct (or any object for that matter), you can have an unsigned char *
point to the start of the object, loop through the bytes, and print each one:
int i;
unsigned char *p = (unsigned char *)&birthDate;
for (i = 0; i < sizeof birthDate; i ) {
printf("x ", p[i]);
}
printf("\n");
CodePudding user response:
And I understand that an array variable represents an address to the first element in that array.
That is not quite correct. In most situations, an array will decay to a pointer to the first element of the array. However, it does not always do this. An array is therefore not always equivalent to a pointer to the first element of the array. One such example is when using the sizeof
operator. When using that operator on an array, it will evaluate to the size of the array in bytes, whereas when using it on a pointer to the first element of an array, it will evaluate to the size of the pointer, not the size of the array.
A struct
on the other hand, will never decay. The value of the struct
will always be the values of all of its elements, and maybe the values of some padding bytes.
The line
printf("The struct type birthDate variable value ( which seems to be an address or something :\\ ) => %i (int), %0X (hex)\n\n", birthDate, birthDate)
is wrong. When printing a variable, you must always use the correct format specifier, so that it matches the type of the variable. However, there is no format specifier for user-defined types, such as struct date
.
Therefore, if you want to print the value of a struct
, you must decide yourself how you want to print it. You could, for example, print the values of all of its individual members, or you could print the values of its individual bytes:
//print individual members of struct
printf(
"The values of the data members of the object birthDate have\n"
"the following values:\n"
"day: %d\n"
"month: %d\n"
"year: %d\n"
"\n",
birthDate.day,
birthDate.month,
birthDate.year
);
//print values of individual bytes of struct
printf(
"The object birthDate consists of %zu bytes with the\n"
"following values:\n",
sizeof birthDate
);
for ( size_t i = 0; i < sizeof birthDate; i )
{
printf( "X ", ((unsigned char*)&birthDate)[i] );
}
printf( "\n\n" );
Here is a full working example based on the code in the question:
#include <stdio.h>
int main()
{
struct date
{
int day;
int month;
int year;
};
struct date birthDate = { 7, 12, 1999 };
//print individual members of struct
printf(
"The values of the data members of the object birthDate have\n"
"the following values:\n"
"day: %d\n"
"month: %d\n"
"year: %d\n"
"\n",
birthDate.day,
birthDate.month,
birthDate.year
);
//print values of individual bytes of struct
printf(
"The object birthDate consists of %zu bytes with the\n"
"following values:\n",
sizeof birthDate
);
for ( size_t i = 0; i < sizeof birthDate; i )
{
printf( "X ", ((unsigned char*)&birthDate)[i] );
}
printf( "\n\n" );
return 0;
}
This program has the following output:
The values of the data members of the object birthDate have
the following values:
day: 7
month: 12
year: 1999
The object birthDate consists of 12 bytes with the
following values:
07 00 00 00 0C 00 00 00 CF 07 00 00
The byte values 07 00 00 00
represent day
, which has the decimal value 7
(stored in little-endian byte ordering). The byte values 0C 00 00 00
represent month
, which has the decimal value 12
(which is 0x0C
in hexadecimal). The byte values CF 07 00 00
represent year
, which has the decimal value 1999
(which is 0x7CF
in hexadecimal).
CodePudding user response:
Struct: represents the "structure" type. That is to say that it is you who will create your own "type" of variable in C.
For structures as for variables, arrays and pointers, it is strongly advised to initialize them as soon as they are created to prevent them from containing anything.
I'm afraid I don't quite understand your question. But from my point of view, structs are very similar to a class in object-oriented programming.
CodePudding user response:
In C you can get address of any type of variable, including struct type of variable, that you get with & and printf %p. The value of struct variable is defenition of struct itself, but in C stdlib no possibility print complete structure with print-like function (no necessary for this format specifier);
and Incomplete struct types in C
typedef struct date
{
int day;
int month;
int year;
struct date *lol;
} DATE;
struct date bzDate = {6, 139, -6};
DATE bzd = { 7, 12, 1999 , &bzDate};
//printf ("bzDate= %d \n", bzd.lol->lol->year);
then see individual bytes like a pro :)