structures why

D

Darklight

In the book i am using it gives three ways to access a structure member.
on the 3rd printf statement i get the output:- 3 1 (null)
why is this could some one please explain

/* POINTERS AND ARRAYS OF STRUCTURES */
#include<stdio.h>

struct part{
int number;
char name[10];
}data[1] = {{1,"herron"}};

/* OR COULD USE: strcut part data[1] = {{1, "Herron"}}; */
struct part *p_part; /* DECLARE A POINTER TO TYPE PART */

int main(void)
{

p_part = &data[0]; /* OR COULD USE p_part = data */
/* THREE WAYS TO ACCESS A STRUCTURE MEMBER */

/* USE THE INDIRECT MEMBERSHIP OPERATOR */
/*1*/ printf("1 %d %s\n",p_part->number,p_part->name);

/* USE THE IDIRECTION OPERATOR */
/*2*/ printf("2 %d %s\n",(*p_part).number,(*p_part).name);

/* USE THE STRUCTURE NAME */
/*3*/ printf("3 %d %S\n",data[0].number, data[0].name[10]);

return 0;
}
 
M

Martin Dickopp

Darklight said:
In the book i am using it gives three ways to access a structure member.
on the 3rd printf statement i get the output:- 3 1 (null)
why is this could some one please explain

/* POINTERS AND ARRAYS OF STRUCTURES */
#include<stdio.h>

struct part{
int number;
char name[10];
}data[1] = {{1,"herron"}};

/* OR COULD USE: strcut part data[1] = {{1, "Herron"}}; */
struct part *p_part; /* DECLARE A POINTER TO TYPE PART */

int main(void)
{

p_part = &data[0]; /* OR COULD USE p_part = data */
/* THREE WAYS TO ACCESS A STRUCTURE MEMBER */

/* USE THE INDIRECT MEMBERSHIP OPERATOR */
/*1*/ printf("1 %d %s\n",p_part->number,p_part->name);

/* USE THE IDIRECTION OPERATOR */
/*2*/ printf("2 %d %s\n",(*p_part).number,(*p_part).name);

/* USE THE STRUCTURE NAME */
/*3*/ printf("3 %d %S\n",data[0].number, data[0].name[10]);

return 0;
}

First of all, "%S" is not a valid printf specifier. Your compiler might
support it as an extension, but I have no idea what it means. For the
sake of this discussion, I will assume that it is equivalent to "%s".

`data[0].name[10]' has type `char', not type `char *'. If you really
want to print a single character, use the "%c" specifier.

Additionally, the last element of the array `data[0].name' is
`data[0].name[9]' (valid indices go from 0 to 9 inclusive), so you're
invoking undefined behavior by accessing `data[0].name[10]'.

If you want to print the string stored in the array `data[0].name'
(as opposed to one character from it), use the function call

printf ("3 %d %s\n", data[0].number, data[0].name);

Martin
 
A

Arthur J. O'Dwyer

In the book i am using it gives three ways to access a structure member.
on the 3rd printf statement i get the output:- 3 1 (null)
why is this could some one please explain

Because you invoked undefined behavior in the printf call.
/*1*/ printf("1 %d %s\n",p_part->number,p_part->name);
/*2*/ printf("2 %d %s\n",(*p_part).number,(*p_part).name);
/*3*/ printf("3 %d %S\n",data[0].number, data[0].name[10]);

Notice the radical differences between this line and the first
two lines. The equivalent code would be

/*3*/ printf("3 %d %s\n",data[0].number,data[0].name);

See how I've replaced '(*p_part)' with 'data[0]'?
The reason your third line didn't work was twofold: first, you
tried to use "%S" as a printf format specifier, which obviously
doesn't work (although your compiler apparently treats %S similarly
to %s), and then you tried to evaluate 'data[0].name[10]', which
not only was not initialized by your "Herron" string, it doesn't
even exist! ('name' has only 10 elements, numbered 0 through 9.)

I recommend you read the c.l.c FAQ [Google "c.l.c FAQ"], and
then practice some more with arrays and I/O before going on to
structs and other more "advanced" concepts.

HTH,
-Arthur
 
M

Martin Ambuhl

Darklight said:
In the book i am using it gives three ways to access a structure member.
on the 3rd printf statement i get the output:- 3 1 (null)
why is this could some one please explain

See the commented out section (#if 0 ... #endif):

#include <stdio.h>

struct part
{
int number;
char name[10];
} data[1] = { {
1, "herron"}};

struct part *p_part;

int main(void)
{
p_part = &data[0];
printf("1 %d %s\n", p_part->number, p_part->name);
printf("2 %d %s\n", (*p_part).number, (*p_part).name);
#if 0
/* mha: %S is not a supported specifier */
data[0].name[10] is a char, not a string or pointer to char */
printf("3 %d %S\n", data[0].number, data[0].name[10]);
#endif
printf("3 %d %s\n", data[0].number, data[0].name);
return 0;
}


1 1 herron
2 1 herron
3 1 herron
 
D

Darklight

/* USE THE STRUCTURE NAME */
/*3*/ printf("3 %d /**%S **/ \n",data[0].number, data[0].name[10]);
Thanks for that i did not see that i will be more observant next time
thanks for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top