myStruct.c
#include <stdio.h> /* Define the struct before main. */ struct student { char name[15]; int id; }; // Give a synonym. Now struct student is the same with student. typedef struct student student; void input(student*, int); void output(student*, int); int main(void) { // Declare an array of 3 students. student stdArray[3]; input(stdArray, 3); output(stdArray, 3); return 0; } void input(student* stdArray, int N) { int i; for(i = 0 ; i < N ; i++) { printf("Input name of student, please.\n"); scanf("%15s", stdArray[i].name); printf("Input ID, please.\n"); scanf("%d", &stdArray[i].id); } } void output(student* stdArray, int N) { int i; for(i = 0 ; i < N ; i++) { printf("Name of %d-th student: %s\n", i, stdArray[i].name); printf("ID of %d-th student is: %d\n", i, stdArray[i].id); } }
This code was developed by me, G. Samaras.
Now we are going to demonstrate, with the help of 3 simple examples for beginners.
arrow0.c
// Example 1 // Referencing a structure member locally in "main()" with the "dot operator" #include <stdio.h> struct Test { int x; }; int main(void) { struct Test sTest; sTest.x = 2; printf("x = %d\n",sTest.x); return 0; }
arrow1.c
// Example 2 // Referencing a structure member in a function, using the "dot operator" // and standard pointer notation #include <stdio.h> struct Test { int x; }; void updateStruct(struct Test *y); int main(void) { struct Test sTest; sTest.x = 2; printf("x = %d\n",sTest.x); updateStruct(&sTest); printf("x = %d\n",sTest.x); return 0; } void updateStruct(struct Test *y) { (*y).x = 3; // line of interest }
arrow2.c
// Example 3 // Referencing a structure member in a function, using the "arrow operator" #include <stdio.h> struct Test { int x; }; void updateStruct(struct Test *y); int main(void) { struct Test sTest; sTest.x = 2; printf("x = %d\n",sTest.x); updateStruct(&sTest); printf("x = %d\n",sTest.x); return 0; } void updateStruct(struct Test *y) { y->x = 3; // line of interest }
Make sure that you understand what happened in the line of interest at arrow1.c and arrow2.c. These two lines are completely equivalent, but it is a handy way of writing the same thing. It is a shortcut that C provides to the programmer.
These three examples were developed by Matticus of C board. Thanks again Matt!
Notice that with structs, the arrow operator (->) is being used very often. For an example , click here.
In (ANSI) C99, you can use a designated initializer to initialize a structure:
MY_TYPE a = { .flag = true, .value = 123, .stuff = 0.456 };
Have questions about this code? Comments? Did you find a bug? Let me know! 😀
Page created by G. (George) Samaras (DIT)
Where exactly in this code is -> used in this code? What lines?
I guess you are referring at the link of List (c) (https://gsamaras.wordpress.com/code/list-c/). One can see the arrow operator at the following lines:
function: in @line: 70 and 73
function: n_th @line: 81 and 85
function: insert_at_start @line: 94 and 95
function: insert_at_end @line: 103 and 104
function: delete @line: 111 and 113
function: print @line: 125
function: free_list @line: 133
At one glance, I see them. Hope this helps.
Thank you for your comment.
I am referring to this page.
Oh sorry, I did not understand that. There is no code with the arrow operator here, but I see your point. There should be. For that reason, I asked for Matticus’s permission to borrow some code of his from C board, where he nicely shows how the arrow operator comes into play. I am going to upload them tonight (might modify them a bit – have the permission for this too). I would also like to thank Matticus, from here too, in advance.
Thanks also again Whiteflags for pointing that out.
The code of Matticus was very good, so I left it intact. Thanks again both.
Pingback: checking what the user has inputed
Pingback: Error from "invalid use of undefined type `struct '"
Pingback: Help with Struct and pointers
Pingback: Help??How to solve this error??
Pingback: Uninitialized variables in structure.
Pingback: Size of Linked List
Pingback: Array of structs(adding/deleting elements to a file)
Pingback: more on struct.
Pingback: Do I use struct correctly?